Thread: XSL question

    #1
  1. a lazy guy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Hong Kong
    Posts
    99
    Rep Power
    14

    XSL question


    In the following XML, there is a ID_LIST and a USER_LIST.
    The XSL below loop through the ID_LIST and try to find out whether the ID contains in the ID_LIST exist in USER_LIST or not.

    In my XSL, it can show the ID if it is exist, but it cannot show the ID if it is not exist in the USER_LIST.

    Anyone got a solution? Thanks.


    XML:

    <MyXML>
    <ID_LIST>
    <ID>1</ID>
    <ID>22</ID>
    <ID>5</ID>
    </ID_LIST>
    <USER_LIST>
    <UID>5</UID>
    <UID>17</UID>
    </USER_LIST>
    </MyXML>

    XSL:
    <xsl:for-each select="/MyXML/ID_LIST/ID">
    <xsl:variable name="MYID">
    <xsl:value-of select="./ID"/>
    </xsl:variable>
    <xsl:for-each select="/MyXML/USER_LIST/UID">
    <xsl:if test="$MYID = .">
    ID: <xsl:value-of select="$MYID"/> EXIST!
    </xsl:if>
    </xsl:for-each>
    </xsl:for-each>
  2. #2
  3. No Profile Picture
    Fries w/ That?
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Kansas
    Posts
    8
    Rep Power
    0
    SunnyChan--

    Try This:

    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
    <xsl:for-each select="/MyXML/ID_LIST/ID">
    <xsl:variable name="MYID">
    <xsl:value-of select="."/>
    </xsl:variable>
    <!--<xsl:for-each select="/MyXML/USER_LIST/UID">-->
    <xsl:choose>
    <xsl:when test="//USER_LIST/UID = $MYID">
    <!-- EXIST! -->
    <xsl:text>User ID exists: </xsl:text>
    <xsl:value-of select="$MYID"/>
    <xsl:text>
    </xsl:text>
    </xsl:when>
    <xsl:otherwise>
    <xsl:text>User ID DOES NOT exist: </xsl:text>
    <xsl:value-of select="$MYID"/>
    <xsl:text>
    </xsl:text>
    </xsl:otherwise>
    </xsl:choose>
    <!--</xsl:for-each>-->
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

    I commented out your nested loop and changed your <if> to a <choose> that just checks for existence of any UID with the current value. Attached is the xsl and text xml.

    Hope this helps.

    Chinnman
    Attached Files

IMN logo majestic logo threadwatch logo seochat tools logo