XSLT filter nodes containing attributes with known values

Discussion in 'XML' started by wvxvw, Sep 26, 2009.

  1. wvxvw

    wvxvw

    Joined:
    Sep 26, 2009
    Messages:
    1
    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: Sep 26, 2009
    wvxvw, Sep 26, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. asd
    Replies:
    3
    Views:
    440
    Arnaud Berger
    May 23, 2005
  2. Jean-Christophe Michel

    param containing nodes and for-each

    Jean-Christophe Michel, Jul 12, 2003, in forum: XML
    Replies:
    4
    Views:
    1,806
    Jean-Christophe Michel
    Jul 13, 2003
  3. gavnosis
    Replies:
    0
    Views:
    524
    gavnosis
    Aug 2, 2003
  4. Kevin
    Replies:
    3
    Views:
    1,586
    Martin Honnen
    Jan 30, 2004
  5. aman.coe@gmail.com
    Replies:
    2
    Views:
    878
    aman.coe@gmail.com
    Oct 22, 2005
Loading...

Share This Page