XSLT Select nodes without text-node children whose names starts with specifix text

Discussion in 'XML' started by Michael Reiche, Feb 4, 2004.

  1. Question on XSL expression

    Got this XML:

    <Body>
    <Page>
    <Line no="9" detail="true">
    <onefield>onefieldstext</onefield>
    <twofield>twofieldstext</twofield>
    </Line>
    <Line no="10" detail="true">
    <onefield>onefieldstext</onefield>
    <fgman9>fgmanfieldstext</fgman9>
    <twofield>twofieldstext</twofield>
    </Line>
    <Line no="11" detail="true">
    <onefield>onefieldstext</onefield>
    <twofield>twofieldstext</twofield>
    </Line>
    <Line no="12" detail="true">
    <onefield>onefieldstext</onefield>
    <twofield>twofieldstext</twofield>
    </Line>
    <Line no="13" detail="true">
    <onefield>onefieldstext</onefield>
    <fgman5>fgmanfieldstext</fgman5>
    <twofield>twofieldstext</twofield>
    </Line>
    <Line no="14" detail="true">
    <onefield>onefieldstext</onefield>
    <twofield>twofieldstext</twofield>
    </Line>
    </Page>
    </Body>

    I would select the <Line/> nodes without text-node children whose names
    is starting with "fgman" - in this example it is all <Line/> _except_
    <fgman9/> and <fgman5/> in <Line/> with @no of 10 and 13.

    I know that this works:

    <xsl:for-each select="Body/Page/Line[@detail]">

    <xsl:if test="count(fgman9|fgman5) = 0">
    L<xsl:value-of select="@no"/>:<xsl:value-of select="onefield"/>
    </xsl:if>

    <xsl:if test="fgman9">
    <xsl:value-of select="@no"/> Ouuch</xsl:if>

    <xsl:if test="fgman5">
    <xsl:value-of select="@no"/> Whoops</xsl:if>

    </xsl:foreach>

    But as the <fgmanXXX/> nodes could be named fgman0 to fgman1000 I would
    prefer not to list everyone as arguments to the count() expression.

    /Reiche
    Michael Reiche, Feb 4, 2004
    #1
    1. Advertising

  2. Michael Reiche

    Markus Spath Guest

    Re: XSLT Select nodes without text-node children whose names startswith specifix text

    Michael Reiche wrote:

    > I know that this works:
    >
    > <xsl:for-each select="Body/Page/Line[@detail]">
    >
    > <xsl:if test="count(fgman9|fgman5) = 0">
    > L<xsl:value-of select="@no"/>:<xsl:value-of select="onefield"/>
    > </xsl:if>
    >
    > <xsl:if test="fgman9">
    > <xsl:value-of select="@no"/> Ouuch</xsl:if>
    >
    > <xsl:if test="fgman5">
    > <xsl:value-of select="@no"/> Whoops</xsl:if>
    >
    > </xsl:foreach>
    >


    within a xsl:choose instruction you could filter out all elements having
    unwanted children using starts-with(), eg:

    <xsl:choose>
    <xsl:when test="*[starts-with(name(),'fgman')]" />
    <xsl:eek:therwise>
    L<xsl:value-of select="@no"/>:<xsl:value-of select="onefield"/>
    </xsl:eek:therwise>
    </xsl:choose>

    markus
    Markus Spath, Feb 5, 2004
    #2
    1. Advertising

  3. "Michael Reiche" <> wrote in message
    news:p...
    > Question on XSL expression
    >
    > Got this XML:
    >
    > <Body>
    > <Page>
    > <Line no="9" detail="true">
    > <onefield>onefieldstext</onefield>
    > <twofield>twofieldstext</twofield>
    > </Line>
    > <Line no="10" detail="true">
    > <onefield>onefieldstext</onefield>
    > <fgman9>fgmanfieldstext</fgman9>
    > <twofield>twofieldstext</twofield>
    > </Line>
    > <Line no="11" detail="true">
    > <onefield>onefieldstext</onefield>
    > <twofield>twofieldstext</twofield>
    > </Line>
    > <Line no="12" detail="true">
    > <onefield>onefieldstext</onefield>
    > <twofield>twofieldstext</twofield>
    > </Line>
    > <Line no="13" detail="true">
    > <onefield>onefieldstext</onefield>
    > <fgman5>fgmanfieldstext</fgman5>
    > <twofield>twofieldstext</twofield>
    > </Line>
    > <Line no="14" detail="true">
    > <onefield>onefieldstext</onefield>
    > <twofield>twofieldstext</twofield>
    > </Line>
    > </Page>
    > </Body>
    >
    > I would select the <Line/> nodes without text-node children whose names


    A text node does not have a name -- probably you mean "whose parent's name"
    ..

    > is starting with "fgman" - in this example it is all <Line/> _except_
    > <fgman9/> and <fgman5/> in <Line/> with @no of 10 and 13.



    Use:

    /*/*/Line[not(*[starts-with(name(), 'fgman')])] [text()]


    Cheers,

    Dimitre Novatchev [XML MVP],
    FXSL developer, XML Insider,

    http://fxsl.sourceforge.net/ -- the home of FXSL
    Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
    Dimitre Novatchev [MVP], Feb 5, 2004
    #3
  4. On Thu, 05 Feb 2004 06:40:45 +0100, Dimitre Novatchev [MVP] wrote:

    >
    >
    > A text node does not have a name -- probably you mean "whose parent's name"
    > .


    Exactly!!!

    >
    >
    > Use:
    >
    > /*/*/Line[not(*[starts-with(name(), 'fgman')])] [text()]
    >
    >


    Well actually I found:

    <xsl:if test="not(*[starts-with(name(), 'fgman')])">

    to suit my needs.

    > Cheers,
    >
    > Dimitre Novatchev [XML MVP],
    > FXSL developer, XML Insider,
    >
    > http://fxsl.sourceforge.net/ -- the home of FXSL
    > Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html


    Thank you Dimitre.

    Regards,
    Michael Reiche
    Michael Reiche, Feb 5, 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. bearclaws
    Replies:
    4
    Views:
    4,657
    bearclaws
    Mar 1, 2005
  2. Replies:
    1
    Views:
    1,187
    Joe Kesselman
    Nov 1, 2006
  3. Zachary  Turner
    Replies:
    1
    Views:
    511
    Bjoern Hoehrmann
    May 30, 2007
  4. thecolour
    Replies:
    0
    Views:
    363
    thecolour
    Jun 26, 2007
  5. thecolour
    Replies:
    2
    Views:
    404
    thecolour
    Jun 27, 2007
Loading...

Share This Page