selecting a node based on partial names

Discussion in 'XML' started by Amit, Aug 12, 2004.

  1. Amit

    Amit Guest

    I have a quick question. Given test XML:

    <root>
    <ns1:sub>1</ns1:sub>
    <ns2:sub>2</ns2:sub>
    <ns3:sub>3</ns3:sub>
    </root>

    is there a way to select all nodes that contain the word "sub". (Due
    to namespace constraints, I will be getting the subnodes as <ns1:sub>,
    <ns2:sub> and so on.)

    regards,
    Amit.
     
    Amit, Aug 12, 2004
    #1
    1. Advertising

  2. Amit wrote:
    > I have a quick question. Given test XML:
    >
    > <root>
    > <ns1:sub>1</ns1:sub>
    > <ns2:sub>2</ns2:sub>
    > <ns3:sub>3</ns3:sub>
    > </root>
    >
    > is there a way to select all nodes that contain the word "sub". (Due
    > to namespace constraints, I will be getting the subnodes as <ns1:sub>,
    > <ns2:sub> and so on.)
    >
    > regards,
    > Amit.


    You can use the XPath function 'contains' to search for 'sub' within the
    element names.

    For this XML:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="sub.xsl"?>
    <root>
    <ns1:sub>1</ns1:sub>
    <ns2:sub>2</ns2:sub>
    <ns3:sub>3</ns3:sub>
    <ns1:nomatch>1</ns1:nomatch>
    <ns2:nomatch>2</ns2:nomatch>
    <ns3:nomatch>3</ns3:nomatch>
    </root>

    The following XSL transformation selects those 'sub' elements:

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/root">
    <html><head/>
    <body>
    <xsl:for-each select="*">
    <xsl:if test="contains(name(.),'sub')">
    <h2><xsl:value-of select="name(.)"/></h2>
    </xsl:if>
    </xsl:for-each>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

    Hopefully that is what you were looking for. Have fun!
    -Scott
     
    Scott Stearns, Aug 12, 2004
    #2
    1. Advertising

  3. In article <>,
    Amit <> wrote:
    % I have a quick question. Given test XML:
    %
    % <root>
    % <ns1:sub>1</ns1:sub>
    % <ns2:sub>2</ns2:sub>
    % <ns3:sub>3</ns3:sub>
    % </root>
    %
    % is there a way to select all nodes that contain the word "sub". (Due
    % to namespace constraints, I will be getting the subnodes as <ns1:sub>,
    % <ns2:sub> and so on.)

    Assuming you use xpath to do the selection, the function local-name()
    returns the name of the element sans name-space prefix. In XSLT, you
    could have

    <xsl:template match='local-name(.) = "sub"'>
    ...
    </xsl>

    Question: if this is really what you want, why do you have name-spaces
    there at all?
    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Aug 15, 2004
    #3
  4. (Patrick TJ McPhee) wrote in message news:<>...
    > <xsl:template match='local-name(.) = "sub"'>


    Surely you mean <xsl:template match="*[local-name() = 'sub']">

    > Question: if this is really what you want, why do you have name-spaces
    > there at all?


    Maybe ns1:sub and ns2:sub are to be treated differently at some other point.
    --
    Robin Johnson
    Lead Developer, enCircle Solutions Ltd.
    first initial last name at encircle dot co dot uk
     
    Robin Johnson, Aug 16, 2004
    #4
    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. Replies:
    0
    Views:
    1,649
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,508
    Dimitre Novatchev
    Aug 24, 2006
  3. thecolour
    Replies:
    0
    Views:
    394
    thecolour
    Jun 26, 2007
  4. thecolour
    Replies:
    2
    Views:
    445
    thecolour
    Jun 27, 2007
  5. JL
    Replies:
    7
    Views:
    211
    Thomas 'PointedEars' Lahn
    Mar 10, 2008
Loading...

Share This Page