XSLT filter nodes containing attributes with known values

Joined
Sep 26, 2009
Messages
1
Reaction score
0
Hi all.
I'm very new to XSLT, was trying to make some very basic template and after a few hours of reading the docs and googling I'm here...

Hope for you this will be simple.

I have this XML:
Code:
<data>
  <foo name="someName"/>
  <bar name="someOtherName"/>
  <qwerty name="someName">
    <nested-node/>
  </querty>
</data>

What I want to get after filtering is like this:
Code:
<data>
  <foo name="someName"/>
  <qwerty name="someName">
    <nested-node/>
  </querty>
</data>

What I tried so far:
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="---namespace URI---">

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

<xsl:template match="//*[@name='someName']">
</xsl:template>

</xsl:stylesheet>
But it does something unpredictable... or, better say it doesn't do anything :)
I would really appreciate if someone could just make a working example, instead of sending me to read the documentation... XSLT isn't really something I'm doing for living... that's the only time I need that thing and will not touch it ever again... hopefully. So, please, have mercy... :)
TIA.

EDIT:

OK, finally I got something like this:

Code:
<xsl:stylesheet version="2.0" 
	xmlns:xsl="--- namespace URI ---">
<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>

And it seems to work... however it also looks very clumsy and convoluted to me... so, please, if you have a better idea about my task - don't hesitate to tell me...
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top