wvxvw
09-26-2009, 02:43 PM
I'm trying to do the filtering using XSLT template.
I have this input:
<data>
<foo name="someName"/>
<bar name="someOtherName"/>
<qwerty name="someName">
<nested-node/>
</querty>
</data>
And i want to get this output:
<data>
<foo name="someName"/>
<qwerty name="someName">
<nested-node/>
</querty>
</data>
No luck so far...
Any help appreciated. And, please, I know where's the manual... I've tried to read it... but it's so much "water" in there and so difficult to understand what it really wants to deliver... so, just an example would help me much better..
TIA
EDIT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="data/*[@name='someName']">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I think this is the closest I could get, but this doesn't copy attributes into attributes and nested nodes are ignored...
EDIT:
Ok, I've got it working with something like this:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*">
<xsl:if test="@name='someName'">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:value-of select="."/>
<xsl:for-each select="./*">
<xsl:call-template name="copyAll"/>
</xsl:for-each>
</xsl:element>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>
<xsl:template name="copyAll">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:value-of select="."/>
<xsl:for-each select="./*">
<xsl:call-template name="copyAll"/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
However, it seems like very clumsy to me, so, if you have a better idea - please tell! :)
I have this input:
<data>
<foo name="someName"/>
<bar name="someOtherName"/>
<qwerty name="someName">
<nested-node/>
</querty>
</data>
And i want to get this output:
<data>
<foo name="someName"/>
<qwerty name="someName">
<nested-node/>
</querty>
</data>
No luck so far...
Any help appreciated. And, please, I know where's the manual... I've tried to read it... but it's so much "water" in there and so difficult to understand what it really wants to deliver... so, just an example would help me much better..
TIA
EDIT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="data/*[@name='someName']">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I think this is the closest I could get, but this doesn't copy attributes into attributes and nested nodes are ignored...
EDIT:
Ok, I've got it working with something like this:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*">
<xsl:if test="@name='someName'">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:value-of select="."/>
<xsl:for-each select="./*">
<xsl:call-template name="copyAll"/>
</xsl:for-each>
</xsl:element>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>
<xsl:template name="copyAll">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:value-of select="."/>
<xsl:for-each select="./*">
<xsl:call-template name="copyAll"/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
However, it seems like very clumsy to me, so, if you have a better idea - please tell! :)