Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding an specified attribute in XML document

Status
Not open for further replies.

Ciarak

Programmer
Oct 17, 2001
9
GB
Hi,
I want to find a attribute in an XML document. I have an XML document with many folder elements and I want to find the folder element with a given attribute.

How can I test for a specific attribute value.Can I use the <xsl:if test> or <xsl:if match> to do this and if so how do I specify that it's not an element I want to find but an attribute?

<xsl:template match=&quot;Folder&quot;>
<xsl:if match=?????
 
Hi Ciarak,

Yes you can use the xsl:if statement and for the evaluation syntax you have the full power of xpath to your disposal, so finding an attribute will be something like this:

Code:
xml:

<folder name=&quot;myfolder&quot;/>

xsl:
<xsl:for-each select=&quot;folder&quot;>
   <xsl:if test=&quot;@name='myfolder'&quot;>
      I found the attribute!!!
   </xsl:if>
</xsl:for-each select>

If you have the MSDN library look for XPath examples to find more code.

Good luck!!

Jordi Reineman
 
Hi,
thanks for your help.I've tried this code :

<xsl:for-each select=&quot;Folder&quot;>
<xsl:if test=&quot;@name='Camera'&quot;>Works</xsl:if>
</xsl:for-each>

and the browser keeps on giving me the following error:

Expected token 'eof' found '='. @name-->=<--'Camera'

What does this error mean?Is this &quot;eof&quot; the end of file and if so does this mean it can't find Camera in the file.
 
It means that I've forgot the brackets... :-(

Your code should be like:

Code:
<xsl:for-each select=&quot;Folder[@name='Camera']&quot;>
  It works
</xsl:for-each>

Good luck!

Jordi Reineman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top