我需要帮助来遵循xslt代码。我的输入为:
<book>
<book1>
<name>abc</name>
<revision>1</revision>
</book1>
<book2>
<name>pqr</name>
<author>def</author>
</book2>
</book>
我的预期输出为:
<book>
<item>
<name>book1</name>
<value>abc1</value>
</item>
<item>
<name>book2</name>
<value>pqrdef</value>
</item>
</book>
我尝试使用 */text() 为值节点获取值,但我只从第一个孩子那里得到文本。将来我有很多这样的子元素。
提前谢谢。
问候米纳克希
这个样式表会给你你想要的。即使boonK
元素的子元素增加,也不需要更改模板。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="book">
<xsl:copy>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book/*">
<item>
<name>
<xsl:value-of select="name()"/>
</name>
<value>
<xsl:for-each select="*">
<xsl:value-of select="."/>
</xsl:for-each>
</value>
</item>
</xsl:template>
</xsl:stylesheet>