Test if an element with a certain value exists

483 views
Skip to first unread message

bdbull

unread,
Nov 11, 2008, 5:57:05 PM11/11/08
to XSLT
I've come across a problem that seem simple, but I can't figure out
how to do this. I need to capture a value while processing a file,
then check if that value exists in another file. I'm able to capture
the value, but it's the other part that I'm hanging up on. For
example, say I capture the value 'charles' and I want to see if it
exists in the below xml:

external.xml
-------------------
<class>
<student>bob</student>
<student>mary</student>
<student>charles</student>
<student>roy</student>
</class>

I know I need to use the document() function to pull in the external
xml file, but I'm not sure how I test for the existence of an element
whose value is X.

Any help?

bdbull

unread,
Nov 11, 2008, 8:50:58 PM11/11/08
to XSLT
If need be, I can change the format of the external xml file to be
like the following:

external.xml
-----------------
<class>
<student name="bob"/>
<student name="mary"/>
<student name="charles"/>
<student name="roy"/>
<class>

I just can't seem to figure this out.

Mukul Gandhi

unread,
Nov 13, 2008, 12:04:28 PM11/13/08
to XSLT
you don't need to change the structure of external.xml.

for the previously posted external.xml, following will be the solution
for this,

Let's say you have already got some value in a XSLT variable, x
(xsl:variable name="x" ...).

The XSLT code for this problem will be,

<xsl:variable name="external" select="document('external.xml')" />

<xsl:if test="$external/class/student = $x">
... do something
</xsl:if>

Cheers,
Mukul
Reply all
Reply to author
Forward
0 new messages