Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - XSLT filter nodes containing attributes with known values

 
Thread Tools Search this Thread
Old 09-26-2009, 02:17 PM   #1
Default XSLT filter nodes containing attributes with known values


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...


wvxvw

Last edited by wvxvw : 09-26-2009 at 04:07 PM.
wvxvw is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco QOS MIB values required manjunatht Hardware 1 06-13-2008 02:34 PM
Help on auto conversion from Matlab to vhdl on filter design hardheart Hardware 0 12-07-2007 09:19 AM
Values set by javascript are not reflected in serverside(.Net) rchimakurty Software 2 11-28-2007 10:07 AM
Checkbox values problem in gridview thanigaimani.thirumalai Software 0 11-09-2007 05:12 AM
Cisco QOS MIB values required manjunatht Software 0 09-11-2007 05:40 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46