score:0

are you looking for something like this:

 <xsl:template match="tag">
    <xsl:element name="genre">
      <xsl:value-of select="@name"/>           
    </xsl:element>    
  </xsl:template>

score:0

i had the same need in vb9 and wanted to go with linq instead of xsl. i found this: http://msdn.microsoft.com/en-us/library/bb669154.aspx and it worked really well.

score:1

you can match any node with node(), like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/xsl/transform">

<!-- uncomment to remove tags elements -->
<!-- <xsl:template match="tags" /> -->

<xsl:template match="genres">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
        <xsl:for-each select="../tags/tag">
            <xsl:element name="genre">
                <xsl:value-of select="@name" />
            </xsl:element>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

<!-- default rule: copy node and descend -->
<xsl:template match="@*|node()">
        <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
</xsl:template>

</xsl:stylesheet>

score:2

using one of the most fundamental and powerful xslt design patterns: overriding the identity template, one will write this very simple transformation to replace every "genres" element with a "topics" element:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/xsl/transform">

 <xsl:output omit-xml-declaration="yes"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="genres">
      <topics>
         <xsl:apply-templates select="node()|@*"/>
      </topics>
    </xsl:template>
</xsl:stylesheet>

when applied against the provided source xml document:

<collection>
    <dvd>
        <id>0000502461</id>
        <title>cirque du soleil: alegría</title>
        <released>2002-05-31</released>
        <runningtime>90</runningtime>
        <genres>
            <genre>family</genre>
            <genre>music</genre>
        </genres>
        <overview>what if anything were possible? what if ...    </overview>
        <notes/>
        <tags>
            <tag name="kids" fullname="kids"/>
        </tags>
    </dvd>
</collection>

the wanted result is produced:

<collection>
    <dvd>
        <id>0000502461</id>
        <title>cirque du soleil: alegría</title>
        <released>2002-05-31</released>
        <runningtime>90</runningtime>
        <topics>
            <genre>family</genre>
            <genre>music</genre>
        </topics>
        <overview>what if anything were possible? what if ...    </overview>
        <notes/>
        <tags>
            <tag name="kids" fullname="kids"/>
        </tags>
    </dvd>
</collection>

the first template in the stylesheet is the identity rule. it copies any matched node unchanged and recursively applies templates to its attributes or children. if no other template is present, this template creates identical copy of the source xml document, hence its name.

when there is a more specific template (specifying more specific details for the matched node, such as name and/or other conditions), it is said to "override" the more generic templates. this means that the more specific template is chosen for processing the node.

using this extremely powerful design pattern it is trivial to implementin just a few lines such processing as:

  1. delete all nodes that satisfy some condition.
  2. rename all nodes that satisfy some condition.
  3. modify the contents of all nodes that satisfy some condition

while copying all other nodes intact.

in our case, the second template is more specific and it gets selected for processing of every element named "genres". all it does is create an element named "topics" and inside it apply templates to all of the current node attributes and children.

finally, the following transformation will add a new "genre" element to the children of "genres", for each "tag" element:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/xsl/transform">

 <xsl:output omit-xml-declaration="yes"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="genres">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
         <xsl:apply-templates select="../tags/tag" mode="gen"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="tag" mode="gen">
      <genre>
        <xsl:value-of select="@name"/>
      </genre>
    </xsl:template>
</xsl:stylesheet>

the result is again as required:

<collection>
    <dvd>
        <id>0000502461</id>
        <title>cirque du soleil: alegría</title>
        <released>2002-05-31</released>
        <runningtime>90</runningtime>
        <genres>
            <genre>family</genre>
            <genre>music</genre>
        <genre>kids</genre>
        </genres>
        <overview>what if anything were possible? what if ...    </overview>
        <notes/>
        <tags>
            <tag name="kids" fullname="kids"/>
        </tags>
    </dvd>
</collection>

more code snippets using the "identity rule" pattern can be found here.


Related Query

More Query from same tag