Jump to content

List in XSL?


surveyer
 Share

Recommended Posts

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

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

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...