xml - XSL transform grandchild node values to element attribute value -
given following xml:
<test id="s1-t2" name="lorem ipsum"> ... <tags> <tag>foo</tag> <tag>bar</tag> </tags> ... </test>
i'd append node value of each tag element in name attribute of test element. resulting xml should this:
<test id="s1-t2" name="lorem ipsum [foo][bar]"> ... <tags> <tag>foo</tag> <tag>bar</tag> </tags> ... </test>
the tags element (and content) can remain in place, not necessary.
so far i've tried this:
<xsl:template match="test"> <test> <xsl:copy-of select="@*"/> <xsl:attribute name="name"> <xsl:value-of select="tags/tag"/> </xsl:attribute> <xsl:apply-templates select="node()"/> </test> </xsl:template>
but not work. , if work, replace name attribute , 1 tag. it's been long since last wrote xslt.
i'd append node value of each tag element in name attribute of test element.
try way:
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="/test/@name"> <xsl:attribute name="name"> <xsl:value-of select="."/> <xsl:for-each select="../tags/tag"> <xsl:text>[</xsl:text> <xsl:value-of select="."/> <xsl:text>]</xsl:text> </xsl:for-each> </xsl:attribute> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment