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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -