with the XML structure below:
<root foo1="bar1" foo2="bar2" foo3="bar3">
<foo1 foo1="bar1" />
<data>
<foo1>bar1</foo1>
<foo2>bar2</foo2>
<foo3>bar3</foo3>
</data>
</root>
I would like to copy this XML structure into another one with some exception on attributes and/or node() names and get the following
result using XSLT 1.0:
<root foo1="bar1" foo2="bar2">
<data>
<foo1>bar1</foo1>
<foo3>bar3</foo3>
</data>
</root>
My rules are:
1) Copy every root attributes except foo3
2) Copy every child nodes() unless the ones named foo1 and foo2
My actual XSL stylesheet. I managed to get the root attributes restriction working :
<xsl:template match="root">
<root>
<xsl:for-each select="./@*">
<xsl:variable name="name" select="name()" />
<xsl:if test="name() != 'foo3'">
<xsl:attribute name="{$name}"><xsl:value-of select="." /></xsl:attribute>
</xsl:if>
</xsl:for-each>
</root>
</xsl:template>
Also, one harder question:
What if I want to matches my attributes and nodes dynamically. I would like to specify server-side what
attributes and nodes() I would like to remove. It’s probably like generating a string that is then used in the <xsl:if>. I don’t know if that’s even possible.
Thank you.
Update from comments:
This stylesheet:
Output:
Note: Overwriting identity rule with empty templates
With the node names in parameter, this stylesheet:
Output:
Note: In XML, the element name refers to the schema, mostly defining the hierarchie position, but yours is not the case.
Edit: Just for fun, an XSLT 2.0 solution:
Edit 2: All the stylesheet following the same string pattern.