Select Node Using position or value of another node.

Discussion in 'XML' started by Eddy C, Oct 14, 2005.

  1. Eddy C

    Eddy C Guest

    Hi,

    I'm trying to get the value of another node using the position of
    another node or the name of the tag.
    Such that the current node is one of the contacts child nodes sec or
    prim
    and doing
    <xsl:value-of select="//cust[seq = position()]/name"/>


    Should return jon for the prim tag and peter for the sec tag, but
    fails as position() appears to resolve to the position of cust not the
    contacts node

    alternatively I would like to do something like

    <xsl:value-of select="//cust[seq = [name()='prim' then 0 else
    1]]/name"/>

    <custs>
    <cust>
    <seq>0</seq>
    <name>jon</seq>
    </cust>
    <cust>
    <seq>1</seq>
    <name>peter</seq>
    </cust>
    <contacts>
    <prim>
    <message>hello</message>
    </prim>
    <sec>
    <message>hello</message>
    </sec>
    </contacts>
    </custs>
     
    Eddy C, Oct 14, 2005
    #1
    1. Advertising

  2. Eddy C

    Peter Flynn Guest

    Eddy C wrote:

    > Hi,
    >
    > I'm trying to get the value of another node using the position of
    > another node or the name of the tag.


    For the difference between tags and elements, see the XML FAQ at
    http://xml.silmaril.ie/authors/makeup/

    > Such that the current node is one of the contacts child nodes sec or
    > prim
    > and doing
    > <xsl:value-of select="//cust[seq = position()]/name"/>
    >
    >
    > Should return jon for the prim tag and peter for the sec tag, but
    > fails as position() appears to resolve to the position of cust not the
    > contacts node


    In cust[...position()...], the value of position() refers to the position of
    the cust among its siblings, starting at 1.

    > alternatively I would like to do something like
    >
    > <xsl:value-of select="//cust[seq = [name()='prim' then 0 else
    > 1]]/name"/>


    This is not a well-formed document. I assume you meant to use </name>
    after jon and peter.

    If so, cust[position()=seq+1]/name will return jon and peter.

    > <custs>
    > <cust>
    > <seq>0</seq>
    > <name>jon</seq>
    > </cust>
    > <cust>
    > <seq>1</seq>
    > <name>peter</seq>
    > </cust>
    > <contacts>
    > <prim>
    > <message>hello</message>
    > </prim>
    > <sec>
    > <message>hello</message>
    > </sec>
    > </contacts>
    > </custs>


    I'm not clear what you want from this. Are you trying to access the
    message belonging to each name, or the name belonging to each message?

    ///Peter
     
    Peter Flynn, Oct 15, 2005
    #2
    1. Advertising

  3. Eddy C

    Eddy C Guest

    Sorry Peter, I'll try and be a little clearer. There was a mistake in
    the XML, which you correclty identified.

    The idea is to fetch the name from the cust node, where the seq equals
    the position-1 of the prim and sec nodes within the customers tag. i.e
    prim is at position 1 and cust is at position 2.

    Therefore the primary (prim) customer would have a name of Jon and the
    secondary (sec) customer would have a name of peter.


    <custs>
    <cust>
    <seq>0</seq>
    <name>jon</name>
    </cust>
    <cust>
    <seq>1</seq>
    <name>peter</name>
    </cust>
    <contacts>
    <prim>
    <message>hello</message>
    </prim>
    <sec>
    <message>hello</message>
    </sec>
    </contacts>
    </custs>
     
    Eddy C, Oct 15, 2005
    #3
  4. Eddy C

    Peter Flynn Guest

    Eddy C wrote:

    > Sorry Peter, I'll try and be a little clearer. There was a mistake in
    > the XML, which you correclty identified.
    >
    > The idea is to fetch the name from the cust node, where the seq equals
    > the position-1 of the prim and sec nodes within the customers tag. i.e
    > prim is at position 1 and cust is at position 2.
    >
    > Therefore the primary (prim) customer would have a name of Jon and the
    > secondary (sec) customer would have a name of peter.
    >
    >
    > <custs>
    > <cust>
    > <seq>0</seq>
    > <name>jon</name>
    > </cust>
    > <cust>
    > <seq>1</seq>
    > <name>peter</name>
    > </cust>
    > <contacts>
    > <prim>
    > <message>hello</message>
    > </prim>
    > <sec>
    > <message>hi</message>
    > </sec>
    > </contacts>
    > </custs>


    OK, but if you are already distinguishing between prim and sec, and you know
    they map directly to seq=0 and seq=1 respectively, I don't see the problem:

    <xsl:choose>
    <xsl:when test="name()='prim'">
    <xsl:value-of select="//cust[seq=0]/name"/>
    </xsl:when>
    <xsl:when test="name()='sec'">
    <xsl:value-of select="//cust[seq=1]/name"/>
    </xsl:when>
    </xsl:choose>

    The only reason for doing it by counting would be if you had far more
    (other) element content in contacts or cust than you are showing us.
    In which case the content of contacts would surely not be prim and sec
    but some other element which could be enumerated. Usually the reason for
    naming element types a certain way is because it describes *different*
    types of data, whereas here the element content of prim and sec is
    identical.

    This appears to be a peculiarly obtuse piece of design. Why not

    <customers>
    <customer n="0" type="primary" name="jon" message="hello"/>
    <customer n="1" type="secondary" name="peter" message="hello"/>
    </customers>

    By all means give the customer element subelement content if the character
    data content of name and message is more complex, but I see no value in
    segmenting the primary/secondary classification from the customer data.

    ///Peter
     
    Peter Flynn, Oct 16, 2005
    #4
  5. Eddy C

    Eddy C Guest

    Peter,

    Its not my design, I'm just the consumer and needed to find a way to
    come up with a single XPath query to get the associated bit of data.

    Also the example I gave is very stripped down. Looking at the Schema
    doc prim and sec have very different attributes but the record we are
    trying to join to has the same attributes for both.

    So it looks like its not going to be simple to do this in one single
    XPath query.
     
    Eddy C, Oct 17, 2005
    #5
  6. Eddy C

    Peter Flynn Guest

    Eddy C wrote:

    > Peter,
    >
    > Its not my design, I'm just the consumer and needed to find a way to
    > come up with a single XPath query to get the associated bit of data.
    >
    > Also the example I gave is very stripped down. Looking at the Schema
    > doc prim and sec have very different attributes but the record we are
    > trying to join to has the same attributes for both.
    >
    > So it looks like its not going to be simple to do this in one single
    > XPath query.


    OK, if it has to be done that way...
    If your current element is either prim or sec, then

    /custs/cust[seq=count(current()/preceding-sibling::*)]/name

    will return the name of the matching customer.

    ///Peter
     
    Peter Flynn, Oct 17, 2005
    #6
  7. Eddy C

    Eddy C Guest

    Very nice, I really appreciate your help.
     
    Eddy C, Oct 18, 2005
    #7
    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,496
  2. Replies:
    0
    Views:
    376
  3. CB
    Replies:
    1
    Views:
    594
    Johannes Koch
    Oct 2, 2003
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,443
    Dimitre Novatchev
    Aug 24, 2006
  5. palmiere
    Replies:
    1
    Views:
    418
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page