surveyer Posted April 4, 2006 Share Posted April 4, 2006 Hi there,I am new to XML and XSL, so please bear with me if I ask simple questions.At the moment I have a problem to check availability of a worker in a list of workers.Here is the code:<Workers> <Worker id="1"> <name>John</name> </Worker> <Worker id="2"> <name>Smith</name> </Worker></Workers>I want to check if there is a worker called 'Jane' in the XML above by using XSL.<employees> <employee> <name>Jane</name> </employee></employees>And if Jane is not in the XML, I want to return a notification that Jane is not in the XML.How could I do this?Please help me. Link to comment Share on other sites More sharing options...
Little Goat Posted April 4, 2006 Share Posted April 4, 2006 Ok, link:http://www.w3schools.com/xsl/el_when.aspif you still can't sort it out, ask again and I'll post code.LG Link to comment Share on other sites More sharing options...
surveyer Posted April 4, 2006 Author Share Posted April 4, 2006 Ok, link:http://www.w3schools.com/xsl/el_when.aspif you still can't sort it out, ask again and I'll post code.LG<{POST_SNAPBACK}> If I use 'if' or 'when' to find that Jane is in the Workers list, it can be done:<xsl:if test="$wname=$ename"><found><xsl:value-of select="$wname"></found></xsl:if>But if I use 'if' or 'when' to find that Jane should not be in the Workers list, that I cannot know how:<xsl:for-each blah blah blah><xsl:choose><xsl:when test="$wname!=$ename"><notfound><xsl:value-of select="$ename"></notfound></xsl:when></xsl:choose></xsl:for-each>This last code will give two redundant answers if 'printed' into HTML:<notfound>Jane</notfound><notfound>Jane</notfound> Link to comment Share on other sites More sharing options...
Little Goat Posted April 4, 2006 Share Posted April 4, 2006 what are all those variables coming from?code: <xsl:choose><xsl:when test="name='jane'">yes</xsl:when><xsl:otherwise>no</xsl:otherwise></xsl:choose> that should write yes if jane is there, no if not.LG Link to comment Share on other sites More sharing options...
surveyer Posted April 4, 2006 Author Share Posted April 4, 2006 what are all those variables coming from?code:<xsl:choose><xsl:when test="name='jane'">yes</xsl:when><xsl:otherwise>no</xsl:otherwise></xsl:choose> that should write yes if jane is there, no if not.LG <{POST_SNAPBACK}> This is the output of your code:nono I just want one 'no' since Jane is not in the list.Btw, this is my code that yields that 2 'no's <xsl:template match="Workers"> <xsl:apply-templates select="Worker"/></xsl:template><xsl:template match="Worker"><xsl:choose><xsl:when test="name='Jane'">yes</xsl:when><xsl:otherwise>no</xsl:otherwise></xsl:choose></xsl:template> Link to comment Share on other sites More sharing options...
Little Goat Posted April 4, 2006 Share Posted April 4, 2006 I'm sorry, my knowledge doesn't go that far.I'll keep looking.LG Link to comment Share on other sites More sharing options...
surveyer Posted April 4, 2006 Author Share Posted April 4, 2006 Anyone can help me?I want to check whether an element is in the list of tags. Link to comment Share on other sites More sharing options...
boen_robot Posted April 5, 2006 Share Posted April 5, 2006 Maybe using some predicates will do the trick? <xsl:template match="/"><xsl:variable name="name">Jane</xsl:variable><xsl:choose><xsl:when test="name=$name"><xsl:for each="Workers/Worker[name=$name]">Yes<br /></xsl:for-each></xsl:when><xsl:otherwise>No</xsl:otherwise></xsl:choose></xsl:template> If I am correct, the conditional will look into every name attribute in the document and compare it with the variable. If there are any matches, it will loop through all workers with the specified name and will output Yes for each one. If there's not any avaiable, there will be only one No.P.S. I haven't tested this.P.S.S. You'll need some DOM that would create the variable with the desired value from a form, if you want to make this application practical. I can't do that script though. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now