locked
how to comapre two xml data in xsl RRS feed

  • Question

  • I have xml data like



        <ItemList>
            - <Item>
                  <ItemID>45</ItemlID>
                  <ItemNumber>1625</ItemNumber>
                  <ItemType>Solid</ItemType>
              </Item>
            - <Item>
                  <ItemID>56</ItemlID>
                  <ItemNumber>145</ItemNumber>
                  <ItemType>Liquid</ItemType>
              </Item>
        </ItemList>

    <ItemPlanList>
        <ItemPlan>
            <ItemID>45</ItemID>
            <ItemCreation>New</ItemCreation>
            <ItemCRDate>02/04/09</ItemCRDate>
            <ItemCrPerson>J C</ItemPerson>
        </ItemPlan>
        <ItemPlan>
            <ItemID>56</ItemID>
            <ItemCreation>Refurbished</ItemCreation>
            <ItemCRDate>07/04/09</ItemCRDate>
            <ItemCrPerson>B J</ItemPerson>
        </ItemPlan>
    </ItemPlanList>

    I am trying to display  each item and than its plan below it.
    Everything is fine. BUT

    I use


    <xsl:for-each select="//ItemList/Item">  to display each item
     and than another nested
     <xsl:for-each select="//ItemPlanList/ItemPlan">
     to display ItemPlan


    This will definitely display all the item plans under each item  but I want the plan only for one particular item being displayed above it.

    So i tried to use a xsl if test condition, but it is not giving me the desired result


    <xsl:if test = "//ItemPlanList/ItemPlan/ItemID =//ItemList/Item/ItemID ">

    But its not giving the desired result.

    How to make it work? how to compare two xml data .

    I know how to compare and xml data to another value like
    < xsl:if test =" //ItemPlanList/ItemPlan/ItemCreation[. = 'Refurbished'] " >

    But here I need to compare "//ItemPlanList/ItemPlan/ItemID =//ItemList/Item/ItemID "
    and only display the particular one

    How to compare and display only the item plan for the item displayed above it


    Thank you




    Friday, August 7, 2009 4:08 PM

Answers

  • I think you want

    <xsl:for-each select="//ItemList/Item">
       <xsl:for-each select="//ItemPlanList/ItemPlan[ItemID = current()/ItemID]">
         ...
       </xsl:for-each>
    </xsl:for-each>

    but you might want to post a well-formed XML sample to start with. Note also that // is expensive, you might want to use an exact path instead e.g. /root/ItemList/Item if there is a root element containing the ItemList elements.
    MVP XML My blog
    Friday, August 7, 2009 4:24 PM