Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT filter nodes containing attributes with known values

Reply
Thread Tools

XSLT filter nodes containing attributes with known values

 
 
wvxvw wvxvw is offline
Junior Member
Join Date: Sep 2009
Posts: 1
 
      09-26-2009
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 by wvxvw; 09-26-2009 at 03:07 PM..
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should Xpath select nodes containing default values?? aman.coe@gmail.com XML 2 10-22-2005 03:30 AM
Text nodes and element nodes query asd Java 3 05-23-2005 10:01 AM
XPath to nodes with containing single quotes Kevin XML 3 01-30-2004 04:32 PM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM
param containing nodes and for-each Jean-Christophe Michel XML 4 07-13-2003 12:13 PM



Advertisments