xsl:for-each with special expression

C

cybernerdsx2

Hi,

I am facing a situation where my XML looks something like below:

<shop>
<group>
<properties>
<property name="something1" value="$50"/>
<property name="something2" value="$10"/>
<property name="something3" value="$15"/>
</properties>
</group>

<group>
<properties>
<property name="something4" value="$25"/>
<property name="something5" value="$1"/>
<property name="something6" value="$4"/>
</properties>
</group>
</shop>

Now, how do I loop through the XML to only pick up value from
"something1" and "something5" in my XSL template so that my HTML will
only display those 2 values?

TIA.

Mike
 
M

Martin Honnen

cybernerdsx2 wrote:

<shop>
<group>
<properties>
<property name="something1" value="$50"/>
<property name="something2" value="$10"/>
<property name="something3" value="$15"/>
</properties>
</group>

<group>
<properties>
<property name="something4" value="$25"/>
<property name="something5" value="$1"/>
<property name="something6" value="$4"/>
</properties>
</group>
</shop>

Now, how do I loop through the XML to only pick up value from
"something1" and "something5" in my XSL template so that my HTML will
only display those 2 values?

One way is
<xsl:template match="/">
<html>
<head>
<title>Example</title>
</head>
<body>
<xsl:for-each select="shop/group/properties/property[@name =
'something1' or @name = 'something5']">
<!-- do here what you want to do with those element e.g. -->
<p><xsl:value-of select="@name" /> has value
<xsl:value-of select="@value" />
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>

You simply write an XPath expression selecting a node set and then in
square brackets you can apply a predicate with a boolean expression to
filter that node set further.
 
P

Peter Flynn

Martin said:
cybernerdsx2 wrote:



One way is
[snip]

The other way is to stop thinking in procedural terms about loops
unless you need to process the information out of document sequence.

<xsl:template
match="property[@name='something1' or @name='something5']">
<blink>
<xsl:value-of select="@value"/>
</blink>
</xsl:template>

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top