XSL: NOT equal checking for node?

Discussion in 'XML' started by Piper707@hotmail.com, Dec 1, 2005.

  1. Guest

    Hi,

    I need to know how I can check to see if a particular node is NOT equal
    to a SET of values.

    i.e. a valid form of : <xsl:template match!="H" && match!="Y"&&
    match!="Z">

    I have an XML document of the foll format:

    <ROOT>
    <A>1</A>
    <B>2</B>
    <C>3</C>
    .....
    <H>4</H>
    ....
    </ROOT>

    I need to transform this into another format. Only the nodes <H>, <X>,
    <Z> need special handling, which i refer to, via a corresponding
    template calls.

    All the other nodes, need to be transformed like this:

    <A>1</A> becomes <A><Value>1</Value></A>

    My xsl looks something like this:

    <xsl:template match="/">
    <NEWFORM>
    <xsl:apply-templates/>
    </NEWFORM>
    </xsl:template>

    /****template calls for H, X, Z**********/

    <xsl:template match="H">
    //special handling for <H>
    </xsl:template>

    How do I say: "for all nodes that are NOT H, X and Z, do this:" ?

    something like:

    for each child of root
    not equal to X, H, Z
    do the following:

    Thanks for any help
    Rohit.
    , Dec 1, 2005
    #1
    1. Advertising

  2. Re: NOT equal checking for node?

    This is basics -- you need to read a good book.

    Use the XPath not() function and the "=" operator.


    Almost never use the "!=" operator, especially if you are not 100% sure you
    know what you're doing...


    Cheers,
    Dimitre Novatchev.


    <> wrote in message
    news:...
    > Hi,
    >
    > I need to know how I can check to see if a particular node is NOT equal
    > to a SET of values.
    >
    > i.e. a valid form of : <xsl:template match!="H" && match!="Y"&&
    > match!="Z">
    >
    > I have an XML document of the foll format:
    >
    > <ROOT>
    > <A>1</A>
    > <B>2</B>
    > <C>3</C>
    > ....
    > <H>4</H>
    > ...
    > </ROOT>
    >
    > I need to transform this into another format. Only the nodes <H>, <X>,
    > <Z> need special handling, which i refer to, via a corresponding
    > template calls.
    >
    > All the other nodes, need to be transformed like this:
    >
    > <A>1</A> becomes <A><Value>1</Value></A>
    >
    > My xsl looks something like this:
    >
    > <xsl:template match="/">
    > <NEWFORM>
    > <xsl:apply-templates/>
    > </NEWFORM>
    > </xsl:template>
    >
    > /****template calls for H, X, Z**********/
    >
    > <xsl:template match="H">
    > //special handling for <H>
    > </xsl:template>
    >
    > How do I say: "for all nodes that are NOT H, X and Z, do this:" ?
    >
    > something like:
    >
    > for each child of root
    > not equal to X, H, Z
    > do the following:
    >
    > Thanks for any help
    > Rohit.
    >
    Dimitre Novatchev, Dec 1, 2005
    #2
    1. Advertising

  3. Guest

    Re: NOT equal checking for node?

    Hi,

    Thanks for your reply.

    I just reread my post - i hope I didn't mislead you my saying
    "particular node is NOT equal to a SET of values."

    Just to clarify, i really want to be looking for all nodes other than
    X, Y, and Z, and not their values as I had posted.

    been struggling with this for a while, could you post a snippet plz?
    , Dec 1, 2005
    #3
  4. Re: NOT equal checking for node?

    wrote:
    > Just to clarify, i really want to be looking for all nodes other than
    > X, Y, and Z, and not their values as I had posted.


    *[not(self::X)][not(self::Y)][not(self::Z)]
    --
    Johannes Koch
    Spem in alium nunquam habui praeter in te, Deus Israel.
    (Thomas Tallis, 40-part motet)
    Johannes Koch, Dec 1, 2005
    #4
  5. Re: NOT equal checking for node?

    > How do I say: "for all nodes that are NOT H, X and Z, do this:" ?

    In XSLT one doesn't need to specifically write this.

    you will have two templates:

    <xsl:template match="*">
    <!-- Whatever general transformation necessary here -->

    </xsl:template>

    <xsl:template match="H | X | Z">
    <!-- A more specialized transformation here -->

    </xsl:template>

    The second template overrides the first for elements of type H, X, Z.

    Just try it :eek:)


    Cheers,
    Dimitre Novatchev

    <> wrote in message
    news:...
    > Hi,
    >
    > I need to know how I can check to see if a particular node is NOT equal
    > to a SET of values.
    >
    > i.e. a valid form of : <xsl:template match!="H" && match!="Y"&&
    > match!="Z">
    >
    > I have an XML document of the foll format:
    >
    > <ROOT>
    > <A>1</A>
    > <B>2</B>
    > <C>3</C>
    > ....
    > <H>4</H>
    > ...
    > </ROOT>
    >
    > I need to transform this into another format. Only the nodes <H>, <X>,
    > <Z> need special handling, which i refer to, via a corresponding
    > template calls.
    >
    > All the other nodes, need to be transformed like this:
    >
    > <A>1</A> becomes <A><Value>1</Value></A>
    >
    > My xsl looks something like this:
    >
    > <xsl:template match="/">
    > <NEWFORM>
    > <xsl:apply-templates/>
    > </NEWFORM>
    > </xsl:template>
    >
    > /****template calls for H, X, Z**********/
    >
    > <xsl:template match="H">
    > //special handling for <H>
    > </xsl:template>
    >
    > How do I say: "for all nodes that are NOT H, X and Z, do this:" ?
    >
    > something like:
    >
    > for each child of root
    > not equal to X, H, Z
    > do the following:
    >
    > Thanks for any help
    > Rohit.
    >
    Dimitre Novatchev, Dec 1, 2005
    #5
  6. Peter Flynn Guest

    Re: NOT equal checking for node?

    Johannes Koch wrote:

    > wrote:
    >> Just to clarify, i really want to be looking for all nodes other than
    >> X, Y, and Z, and not their values as I had posted.

    >
    > *[not(self::X)][not(self::Y)][not(self::Z)]


    Probably far easier is to declare the null template for the unwanted
    element types:

    <xsl:template match="X|Y|Z"/>

    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/
    Peter Flynn, Dec 1, 2005
    #6
  7. Guest

    Re: NOT equal checking for node?

    Thanks. much better. The <xsl:template match="*"> seems to fit my
    needs.

    I have run into another problem,

    1) how do I call the * template?

    I'm need to convert all tags (other than those with special handling)
    of the form
    <A>1234<A>

    to

    <TAG>
    <NAME>A</NAME>
    <VALUE>1234</VALUE>
    </TAG>

    <xsl:template match="INPUT">
    <NEW>
    <A><xsl:value-of select="A"/></A>
    <B><xsl:value-of select="B"/></B>
    <xsl:apply-templates select="* />
    <xsl:apply-templates select="SPECIAL" />
    </NEW>
    </xsl:template>
    -----------------------------------------------------------------------------------
    I must have the special handling tag after the * handling.

    2) The * template is included below. Is it correct to say node() to get
    the node name?
    -----------------------------------------------------------------------------------
    <xsl:template match="*">
    <xsl:variable name="tag_value">
    <xsl:value-of select="."/>
    </xsl:variable>
    <FEATURE>
    <MNEMONIC><xsl:value-of select='node()'/></MNEMONIC>
    <VALUE><xsl:value-of select="$tag_value"/></VALUE>
    </FEATURE>
    </xsl:template>
    ---------------------------------------------------------------------------------------------------------
    , Dec 2, 2005
    #7
  8. Peter Flynn Guest

    Re: NOT equal checking for node?

    wrote:

    > Thanks. much better. The <xsl:template match="*"> seems to fit my
    > needs.
    >
    > I have run into another problem,
    >
    > 1) how do I call the * template?


    You don't. It gets used by anything that is not otherwise matched by an
    existing template.

    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/
    Peter Flynn, Dec 3, 2005
    #8
    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,447
  2. CB
    Replies:
    1
    Views:
    591
    Johannes Koch
    Oct 2, 2003
  3. Replies:
    1
    Views:
    3,598
    A. Bolmarcich
    May 27, 2005
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,423
    Dimitre Novatchev
    Aug 24, 2006
  5. Replies:
    36
    Views:
    148
    Moritz Emanuel Beber
    Mar 31, 2014
Loading...

Share This Page