xsl:for-each with special expression

Discussion in 'XML' started by cybernerdsx2, Dec 7, 2005.

  1. cybernerdsx2

    cybernerdsx2 Guest

    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
    cybernerdsx2, Dec 7, 2005
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Dec 7, 2005
    #2
    1. Advertising

  3. cybernerdsx2

    Peter Flynn Guest

    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/
    Peter Flynn, Dec 8, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    398
    Eliyahu Goldin
    Aug 3, 2005
  2. Sparko
    Replies:
    0
    Views:
    402
    Sparko
    Jul 9, 2004
  3. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    416
    Tjerk Wolterink
    Nov 3, 2004
  4. Replies:
    1
    Views:
    3,587
    A. Bolmarcich
    May 27, 2005
  5. Pat Maddox
    Replies:
    6
    Views:
    142
    Marcin Mielżyński
    Jan 20, 2006
Loading...

Share This Page