Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xsl:for-each with special expression

Reply
Thread Tools

xsl:for-each with special expression

 
 
cybernerdsx2
Guest
Posts: n/a
 
      12-07-2005
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-07-2005


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/
 
Reply With Quote
 
 
 
 
Peter Flynn
Guest
Posts: n/a
 
      12-08-2005
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Special Report: How special are you? Death from Above MCSE 2 03-19-2007 07:22 PM
Regular expression to exclude special characters RJN ASP .Net 2 02-25-2005 06:52 PM
Special editions and Deluxe special edition dvd question. Rclrk43 DVD Video 8 12-29-2004 07:32 PM



Advertisments