Reputation: 17
I need help in following xslt code. I have input as:
<book>
<book1>
<name>abc</name>
<revision>1</revision>
</book1>
<book2>
<name>pqr</name>
<author>def</author>
</book2>
</book>
My expected output as:
<book>
<item>
<name>book1</name>
<value>abc1</value>
</item>
<item>
<name>book2</name>
<value>pqrdef</value>
</item>
</book>
I have tried fetching value for value node using */text() but i get text only from first child. In future I have many such child elements.
Thanks in advance.
Regards, Minakshi
Upvotes: 0
Views: 970
Reputation: 1304
This stylesheet will give you what you want. Even if the child of boonK
element increase, the template will not need to be change.
<?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>
Upvotes: 0