selecting a node based on partial names

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amit

    #1

    selecting a node based on partial names

    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.
  • Scott Stearns

    #2
    Re: selecting a node based on partial names

    Amit wrote:[color=blue]
    > 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.[/color]

    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:styleshe et 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:valu e-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

    Comment

    • Patrick TJ McPhee

      #3
      Re: selecting a node based on partial names

      In article <769ade64.04081 11517.2c18c3e1@ posting.google. com>,
      Amit <amit_gt40@yaho o.com> 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
      ptjm@interlog.c om

      Comment

      • Robin Johnson

        #4
        Re: selecting a node based on partial names

        ptjm@interlog.c om (Patrick TJ McPhee) wrote in message news:<2o9lbhF88 7meU1@uni-berlin.de>...[color=blue]
        > <xsl:template match='local-name(.) = "sub"'>[/color]

        Surely you mean <xsl:template match="*[local-name() = 'sub']">
        [color=blue]
        > Question: if this is really what you want, why do you have name-spaces
        > there at all?[/color]

        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

        Comment

        Working...