XSLT 1.0:搜索属性并添加到父元素

时间:2015-06-29 19:06:23

标签: xslt-1.0

XML输入1:

<wine grape="chardonnay">
    <product>Carneros</product>
    <year>1997</year>
    <price>10.99</price>
    <Field attributes="gccxml(msgid=237)"/>
    <Field id="5" attributes=""/>
    <Field id="6" attributes=""/>
    <Field line="19"/>
</wine>

XML输入2:

<wine grape="chardonnay">
    <product>Carneros</product>
    <year>1997</year>
    <price>10.99</price>
    <Method attributes="gccxml(msgid=237)">
        <Argument location="f0:13"/>
    </Method>
    <Field line="19" attributes=""/>
</wine>

XML输出:

<wine grape="chardonnay" msgid="237">
  <product>Carneros</product>
  <year>1997</year>
  <price>10.99</price>
  <Method attributes="gccxml(msgid=237)">
    <Argument location="f0:13"/>
  </Method>
  <Field line="19"/>
</wine>

任务逻辑如下:     grad第一个外观属性内容(即attributes="gccxml(msgid=237)")     并将其添加到父元素中。     请注意,attributes可以位于Field节点或Method节点中。     此外,从attributes重命名为msgid并将gccxml(msgid=237)转换为msgid="237"     总结一下:XML输出应该只用msgid =&#34; 237&#34;

更新父元素
Basically, it is quite easy to do 'hard coded' by grabing a specific attribute from a given node and copy to its parent element. 

In here, the complexity is to search thru the 'Field' and 'Method' nodes, to grad the first occurance where the 'attribute' is not null, to do some string manipulation and place it its the parent element.

基本上,很容易做到硬编码&#39;通过从给定节点中抓取特定属性并复制到其父元素。

In here, the complexity is to search thru the 'Field' and 'Method' nodes, to grad the first occurance where the 'attribute' is not null, to do some string manipulation and place it its the parent element.

在这里,复杂性是通过&#39; Field&#39;和&#39;方法&#39;节点,以渐变第一次出现的属性&#39;不是null,要做一些字符串操作并将它放在父元素上。

1 个答案:

答案 0 :(得分:0)

  

任务逻辑如下:grad第一个外观属性   内容(即attributes =&#34; gccxml(msgid = 237)&#34;)并将其添加到父级   元件。

这部分可以通过以下方式轻松完成:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/*">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:attribute name="msgid">
            <xsl:value-of select="//@attributes[string(.)][1]"/>
        </xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
  

另外,从属性重命名为msgid ...

我们已经做到了......

  

...并将gccxml(msgid = 237)转换为msgid =&#34; 237&#34;

这种转换的逻辑并不十分明确。

修改

gccxml(msgid=使用后提取数字:

<xsl:value-of select="substring-before(substring-after(//@attributes[string(.)][1], 'msgid='), ')')"/>
相关问题