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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement in XSL for an attribute 3

Status
Not open for further replies.

Kai77

Programmer
Jun 7, 2004
77
0
0
NL
I am trying to make an if statement in my xsl for this xml doc, it only needs to show the value of a content when the typeid = 2:

<xml>
<contents>
<content typeid="1">aaaaa</content>
<content typeid="1">aaaaa</content>
<content typeid="1">aaaaa</content>
<content typeid="2">bbbbb</content>
<content typeid="1">aaaaa</content>
</contents>
</xml>

Code I am tryin to use:

<xsl:for-each select=/xml/contents/content">
<xsl:value-of select="@typeid"/>
<xsl:if test="string=2">
<xsl:value-of select="@typeid"/>
</xsl:if>
</xsl:for-each>

How can I resolve this?
 
<xsl:for-each select=/xml/contents/content[@typeid=2]">
<!-- only nodes with typeid = 2 -->
<xsl:value-of select="."/>
</xsl:for-each>

or:

<xsl:for-each select=/xml/contents/content">
<!-- all content-nodes -->
<xsl:if test="@typeid=2">
<!-- only nodes with typeid = 2 -->
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>

 
Is it possible to like select the third or fourth element of a particular statement? For example with this xml file:

<xml>
<contents>
<content typeid="1">aaaaa</content>
<content typeid="1">bbbbb</content>
<content typeid="2">zzzzz</content>
<content typeid="1">ccccc</content>
<content typeid="1">ddddd</content>
</contents>
</xml>

I'd like to select the third element that has a typeid 1, this case being <content typeid="1">ccccc</content>
 
<?xml version="1.0"?>
<xsl:stylesheet version = '1.0'
xmlns:xsl='<xsl:template match="/">
<xsl:apply-templates select="//xml/contents/content[@typeid=1]"/>
</xsl:template>

<xsl:template match="content">
<xsl:if test="position()=3">
<xsl:value-of select="."/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>
 
If you want better performance (not looping through each node):

<xsl:value-of select="/xml/contents/content[@typeid=1][3]"/>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top