<root>
的主要元素
<item>
。我在文档中其他地方还有其他元素,其中包含我要添加为新的子元素的信息。我想转换的这些元素应该具有条件(code =“ code01”),对于其他代码,属性应成为带有前缀<item>
的新子标签。
there是我XML文件的简单示例:__
我想改变它,以使其看起来如下:
<root>
<item>
<ref>Initial Value</ref>
</item>
<extraData>
<constant code="code01">
<value>New Value</value>
<othertags>Random thing</othertags>
</constant>
<constant code="code02">
<value>New Value 2</value>
<othertags>Random thing 2</othertags>
</constant>
</extraData>
</root>
我如何使用XSLT转换来实现这一目标?
韦尔,一次一步。
从中获取
<root>
<item>
<ref>Initial Value</ref>
<newTag>New Value</newTag>
</item>
<extraData>
<constant>
<__code> code02 </__code>
<value>New Value 2</value>
<othertags>Random thing 2</othertags>
</constant>
</extraData>
</root>
<ref>Initial Value</ref>
到 </item>
`<item>`
<ref>Initial Value</ref>
<newTag>New Value</newTag>
您需要一个模板规则,例如
</item>
<xsl:template match="item">
<xsl:copy>
<xsl:copy-of select="*"/>
<newItem>
<xsl:value-of select="//extraData/constant[@code="code01"]/value"/>
</newItem>
</xsl:copy>
要删除Code01元素,您需要模板规则:
</xsl:template>
要转换其他
<xsl:template match="constant[@code='code01'/>
元素,您需要一个规则,例如:
constant
<xsl:template match="constant[@code != 'code01']">
<constant>
<__code><xsl:value-of select="@code"/></__code>
<xsl:copy-of select="*"/>
</constant>