Skip to main content
1-Visitor
April 24, 2012
Question

help with XPATH

  • April 24, 2012
  • 14 replies
  • 2857 views

how can I have a tag check its content? I can't use starts-with because I want the answer to be "1" and there is also a "10".


I am looking for when hazid = 1 or the first time hazid has content.


(And how would another tag check hazid's content?)



Thanks!

    14 replies

    1-Visitor
    April 24, 2012
    If you're "on" hazid, you could do: self::hazid[. = '1']
    or just: self::*[. = '1']

    If you're on, say, the parent of hazid: child::hazid[. = '1']
    or just: hazid[. = '1'] (since "child" is the default axis, or "direction
    of travel within the tree/graph/document")

    If you need to get to hazid from somewhere else, please describe it.

    By way of explanation, "." is short for "self::node()", a.k.a. "the current
    node". Comparing it with a string ('1') causes XPath to take the "string
    value" of the node, which is all of the text content (recursively), for an
    element.

    -Brandon 🙂


    On Mon, Apr 23, 2012 at 10:22 PM, Caroline Leccese <
    caroline@thecodesource.net> wrote:

    > how can I have a tag check its content? I can't use starts-with because I
    > want the answer to be "1" and there is also a "10".
    >
    > I am looking for when hazid = 1 or the first time hazid has content.
    >
    > (And how would another tag check hazid's content?)
    >
    >
    >
    > Thanks!
    >
    cleccese1-VisitorAuthor
    1-Visitor
    April 24, 2012

    Thanks Brandon! I want to check from <icon>


    <exp>


    <icon><title></title><hazid></hazid><hazdesc></hazdesc>


    <icon><title></title><hazid></hazid><hazdesc></hazdesc>


    <icon><title></title><hazid></hazid><hazdesc></hazdesc>


    </exp>

    cleccese1-VisitorAuthor
    1-Visitor
    April 26, 2012

    from icon: following-sibling::hazid[. = '1']


    is not right...it is coming up true when hazid is empty as well as when hazid = 1

    1-Visitor
    April 26, 2012
    you could try combining with thenormalize-space() function

    Tracey

    On Thu, Apr 26, 2012 at 1:38 PM, Caroline Leccese
    <caroline@thecodesource.net> wrote:
    >
    > following-sibling::hazid[. = '1']
    >
    > is not right...it is coming up true when hazid is empty as well as when hazid = 1
    >
    >
    > -----End Original Message-----
    1-Visitor
    April 26, 2012
    From your example, it seemed like you should be using the "child" axis (or
    none at all, since "child" is the default), rather than
    "following-sibling", since you indicated that your context was an element
    that appeared to be the parent of "hazid".

    Can you give us more details of the case where you're not getting the
    results you expect? A document fragment including both the hazid you're
    looking at, as well as the context element. Also, how are these XPath
    expressions being evaluated? In Styler, using ACL functions in Editor, in
    something completely different...?

    -Brandon 🙂


    On Wed, Apr 25, 2012 at 11:38 PM, Caroline Leccese <
    caroline@thecodesource.net> wrote:

    > following-sibling::hazid[. = '1']
    >
    > is not right...it is coming up true when hazid is empty as well as when
    > hazid = 1
    >
    cleccese1-VisitorAuthor
    1-Visitor
    April 26, 2012

    My mistake, <icon/> is a single tag, it is not the parent of hazid (as I understand it)


    Via styler 5.4, I am inserting a condition onto <icon/> using xpath: following-sibling::hazid[. = '1'] is true


    If I put test gentext "


    <exp>


    TRUE <icon><title>title</title> <hazid></hazid><hazdesc>text.</hazdesc>


    TRUE<icon><title>title</title><hazid>2</hazid><hazdesc>text</hazdesc>


    <icon><title>title</title><hazid>4</hazid><hazdesc>text</hazdesc>


    </exp>



    Thanks for any help!

    1-Visitor
    April 26, 2012
    As Tracey mentioned, use

    At icon element, use the following xpath:
    following-sibling::hazid[normalise-space(.) = '1']


    On Thu, Apr 26, 2012 at 4:58 PM, Caroline Leccese <
    caroline@thecodesource.net> wrote:

    > My mistake, <icon/> is a single tag, it is not the parent of hazid (as I
    > understand it)
    >
    > Via styler 5.4, I am inserting a condition onto <icon/> using xpath:
    > following-sibling::hazid[. = '1'] is true
    >
    > If I put test gentext "*TRUE*" before icon for the condition, this is
    > what happens:
    >
    > <exp>
    >
    > *TRUE*<icon><title>title</title>*<hazid></hazid>*<hazdesc>text.</hazdesc>
    >
    > *TRUE*<icon><title>title</title>*<hazid></hazid>*<hazdesc>text.</hazdesc>
    >
    > *TRUE*<icon><title>title</title>*<hazid></hazid>*<hazdesc>text.</hazdesc>
    >
    > *TRUE*<icon><title>title</title>*<hazid>1</hazid>*<hazdesc>text</hazdesc>
    >
    > <icon><title>title</title>*<hazid>2</hazid>*<hazdesc>text</hazdesc>
    >
    > <icon><title>title</title>*<hazid>3</hazid>*<hazdesc>text</hazdesc>
    >
    > <icon><title>title</title>*<hazid>4</hazid>*<hazdesc>text</hazdesc>
    >
    > </exp>
    >
    1-Visitor
    April 26, 2012
    Let's break the XPath down to see why this is happening. The first part,
    "following-sibling::hazid", returns a node-set consisting of all of the
    "hazid" elements that follow the context node and are children of the same
    parent element. In your example, this is every hazid until the closing
    "exp" tag.

    The second part, "[. = '1']", filters the set of nodes to just those for
    which the string-value of the node (".") is equal to the string "1".
    Because we're evaluating this is a boolean (true/false) condition, XPath
    then converts the node-set to a boolean value, for which the rule is to
    return false if the node-set is empty and true otherwise.

    Since the first four "icon" elements are all followed by a "hazid" whose
    content is equal to "1", they all match the condition. It seems that your
    real requirement is to match those "icon" elements for which the *next*
    "hazid" is equal to "1". For this, we can include a "positional"
    predicate, as follows:

    following-sibling::hazid[1][. = '1']

    Here, the "[1]" filters the node-set to just the first item within it, then
    applies the content-based filter, as before.

    For the record, the "normalize-space" function can be very useful in
    dealing with arbitrary data which may contain arbitrary white space, and I
    wouldn't disagree with using it here, in addition to the above change, to
    make the condition more robust.

    -Brandon 🙂


    On Thu, Apr 26, 2012 at 12:58 AM, Caroline Leccese <
    caroline@thecodesource.net> wrote:

    > My mistake, <icon/> is a single tag, it is not the parent of hazid (as I
    > understand it)
    >
    > Via styler 5.4, I am inserting a condition onto <icon/> using xpath:
    > following-sibling::hazid[. = '1'] is true
    >
    > If I put test gentext "*TRUE*" before icon for the condition, this is
    > what happens:
    >
    > <exp>
    >
    > *TRUE*<icon><title>title</title>*<hazid></hazid>*<hazdesc>text.</hazdesc>
    >
    > *TRUE*<icon><title>title</title>*<hazid></hazid>*<hazdesc>text.</hazdesc>
    >
    > *TRUE*<icon><title>title</title>*<hazid></hazid>*<hazdesc>text.</hazdesc>
    >
    > *TRUE*<icon><title>title</title>*<hazid>1</hazid>*<hazdesc>text</hazdesc>
    >
    > <icon><title>title</title>*<hazid>2</hazid>*<hazdesc>text</hazdesc>
    >
    > <icon><title>title</title>*<hazid>3</hazid>*<hazdesc>text</hazdesc>
    >
    > <icon><title>title</title>*<hazid>4</hazid>*<hazdesc>text</hazdesc>
    >
    > </exp>
    >
    cleccese1-VisitorAuthor
    1-Visitor
    April 26, 2012

    Thanks, everyone!


    following-sibling::hazid[1][normalize-space(.) = '1'] worked as I wished.


    Is there a way to write the condition in Styler so it doesn't convert to a Boolean and just checks the value?

    1-Visitor
    April 26, 2012
    Hello,

    Why don't you use the simple test :
    following-sibling::hazid[text()='1']

    does it also return true when hazid is empty ?

    Yves Deniard
    MBDA France