Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   xsl:for-each with special expression (http://www.velocityreviews.com/forums/t170284-xsl-for-each-with-special-expression.html)

cybernerdsx2 12-07-2005 08:13 AM

xsl:for-each with special expression
 
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


Martin Honnen 12-07-2005 03:58 PM

Re: xsl:for-each with special expression
 


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Peter Flynn 12-08-2005 12:12 AM

Re: xsl:for-each with special expression
 
Martin Honnen wrote:

>
>
> 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

[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
--
XML FAQ: http://xml.silmaril.ie/


All times are GMT. The time now is 12:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.