XSLT Question

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

    #1

    XSLT Question

    Hello,

    I have a following situation..
    XML Document contains
    <PurchaseOrde r>
    <LineItems>
    <LineItem>
    <Description> </Description>
    </LineItem>
    <LineItem>
    <Description> </Description>
    </LineItem>
    </LineItems>
    <Comments>
    <Comment>
    <NumberInCollec tion>1</NumberInCollect ion>
    </Comment>
    <Comment>
    <NumberInCollec tion>2</NumberInCollect ion>
    </Comment>
    </Comments>
    </PurchaseOrder>


    When I am looping through Each Line Item using XSLT - I need to also
    get corresponding comment for that line item - I have
    NumberInCollect ion which ties with appropriate LineItem but I dont
    know how to get this number when I am looping through Each LineItem.

    Any Help will be very much apprecieated!

    Thanks

    AZXML
  • Andy Dingley

    #2
    Re: XSLT Question

    shuchalle@hotma il.com (Just Curious) wrote in message news:<d2573fe0. 0406021016.2215 4962@posting.go ogle.com>...
    [color=blue]
    > I have
    > NumberInCollect ion which ties with appropriate LineItem but I dont
    > know how to get this number when I am looping through Each LineItem.[/color]

    This is a piece of poor DTD / Schema design - no wonder your XSLT is a
    problem.


    What connects these two nodes ? In your example, then there's nothing
    robust that does it.


    For a simple life with XML, then do it by containment:

    <PurchaseOrde r>
    <LineItems>
    <LineItem>
    <Description> </Description>
    <Comment></Comment>
    </LineItem>
    <LineItem>
    <Description> </Description>
    <Comment></Comment>
    </LineItem>
    </LineItems>
    </PurchaseOrder>


    If they _must_ be separated, then look at an Ordinal_Name / Reference
    scheme. XML does this (badly) with ID and IDREF type attributes. RDF
    does it in a more thought-through manner. Look at the RSS 1.0
    protocol for a simple example.


    In your particular case, then the XSLT is easy. However it's not
    robust, because the ordering of the <LineItems> collection is not
    reliable against being re-ordered. It would be much improved by some
    form of "item number" attribute on each, then referring to that.

    If you're stuck with the DTD as it stands, then investigate the
    count() and position() functions within XSLT.

    Comment

    • A. Bolmarcich

      #3
      Re: XSLT Question

      In article <d2573fe0.04060 21016.22154962@ posting.google. com>, Just Curious wrote:[color=blue]
      > Hello,
      >
      > I have a following situation..
      > XML Document contains
      > <PurchaseOrde r>
      > <LineItems>
      > <LineItem>
      > <Description> </Description>
      > </LineItem>
      > <LineItem>
      > <Description> </Description>
      > </LineItem>
      > </LineItems>
      > <Comments>
      > <Comment>
      > <NumberInCollec tion>1</NumberInCollect ion>
      > </Comment>
      > <Comment>
      > <NumberInCollec tion>2</NumberInCollect ion>
      > </Comment>
      > </Comments>
      > </PurchaseOrder>
      >
      >
      > When I am looping through Each Line Item using XSLT - I need to also
      > get corresponding comment for that line item - I have
      > NumberInCollect ion which ties with appropriate LineItem but I dont
      > know how to get this number when I am looping through Each LineItem.[/color]

      If I correctly understand the description, in the for-each on LineItem
      elements you can use

      <xsl:variable name="pos" select="positio n()"/>

      to get the position of the current LineItem element being processed
      by the for-each and use the XPATH expression

      ../following-sibling::Commen ts/Comment[NumberInCollect ion=$pos]

      to get the corresponding Comment element.

      Comment

      • Just Curious

        #4
        Re: XSLT Question

        Thank you that did it!

        I am hoping that xslt does processing of XML file in a linear fashion
        - meaning - while looping for:each - LineItem 1 gets processeed first,
        LineItem 2 gets processed second and so forth from top down

        Thank you again!
        aggedor@earl-grey.cloud9.net (A. Bolmarcich) wrote in message news:<slrncbuk2 s.t6r.aggedor@e arl-grey.cloud9.net >...[color=blue]
        > In article <d2573fe0.04060 21016.22154962@ posting.google. com>, Just Curious wrote:[color=green]
        > > Hello,
        > >
        > > I have a following situation..
        > > XML Document contains
        > > <PurchaseOrde r>
        > > <LineItems>
        > > <LineItem>
        > > <Description> </Description>
        > > </LineItem>
        > > <LineItem>
        > > <Description> </Description>
        > > </LineItem>
        > > </LineItems>
        > > <Comments>
        > > <Comment>
        > > <NumberInCollec tion>1</NumberInCollect ion>
        > > </Comment>
        > > <Comment>
        > > <NumberInCollec tion>2</NumberInCollect ion>
        > > </Comment>
        > > </Comments>
        > > </PurchaseOrder>
        > >
        > >
        > > When I am looping through Each Line Item using XSLT - I need to also
        > > get corresponding comment for that line item - I have
        > > NumberInCollect ion which ties with appropriate LineItem but I dont
        > > know how to get this number when I am looping through Each LineItem.[/color]
        >
        > If I correctly understand the description, in the for-each on LineItem
        > elements you can use
        >
        > <xsl:variable name="pos" select="positio n()"/>
        >
        > to get the position of the current LineItem element being processed
        > by the for-each and use the XPATH expression
        >
        > ../following-sibling::Commen ts/Comment[NumberInCollect ion=$pos]
        >
        > to get the corresponding Comment element.[/color]

        Comment

        Working...