xsl-list
[Top] [All Lists]

RE: Some help with xsl transform and filter please

2004-03-25 15:25:54
An xsl:if is one way to do it, you might also want to learn more about 
predicates.
http://www.w3.org/TR/xpath#predicates

Using a predicate you can limit the selected nodes in the for-each to those 
that meet your condition:

<xsl:for-each select="
  
methodResponse/params/param/value/array/data/value/struct[member/value/string='Syndicated']">

This for-each will only select "struct" elements when the contents of the 
predicate evaluate to true. The context is set to each node that is found that 
matches the path, so we can start our paths using "struct" as the current node.

You can also embed a predicate inside of another predicate. For example, if you 
want to be more specific and look for a member with a name='status' and 
value='Syndicated' you could do this:

<xsl:for-each select="
  methodResponse/params/param/value/array/data/value/struct[
      member[name='status' and value/string='Syndicated']
  ]">


One good way to learn XSL is to keep reading this list, and try to solve some 
of the problems that people post. At first you might want to start by answering 
them for yourself and comparing your answer to what comes in from the others. 
Its a great way to not only flex your XSL skills in situations other than your 
current project, but also so how other people are thinking about similar 
problems.

Enjoy,
Josh

-----Original Message-----
From: Dylan Barber [mailto:dylan(_dot_)barber(_at_)earthlink(_dot_)net]
Sent: Thursday, March 25, 2004 12:32 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Some help with xsl transform and filter please


Josh I agree but the majority of info out there that I can find seems to be 
pointed at people who already have a grasp of the fundamentals of XMl and XSL.  
I dont have those (yet) so any resources you can send my way are great and 
thanks for the help.

okay so now can i include a <xsl:if> into it to get only the ones with a 
struct/member/value/string value of syndicated?
-----Original Message-----
From: Josh Canfield <Josh(_dot_)Canfield(_at_)plumtree(_dot_)com>
Sent: Mar 25, 2004 2:15 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Some help with xsl transform and filter please

No, your xsl won't work because entering the for-each element changes your 
context to the selected node. Your outer for-each is selecting the value 
element, and there are no methodResponse children of the value element.

I would recommend spending a little time going through a tutorial, or reading a 
book (search this list for past recommendations at 
http://www.biglist.com/lists/xsl-list/archives/). I don't intend this to be 
condescending, but anyone trying to use XSL needs to come to grips with some 
fundamental differences between it and other programming languages they are 
used to.

You might also want to take a look at the XPATH and XSL specs:
http://www.w3.org/TR/xpath
http://www.w3.org/TR/xslt

Keep a shortcut to them handy as they make a good reference.

You might also want to print the handy XSLT/XPATH quick reference from Mulberry 
and keep it handy
http://www.mulberrytech.com/quickref/index.html


With your given document, if you wanted to output all of the members of the 
struct then you could do this:

  <xsl:template match="/">
    <!-- match the struct element, remember that this could match more than one
         struct element, for instance if the array had multiple data elements 
-->
    <xsl:for-each 
select="methodResponse/params/param/value/array/data/value/struct">
      Struct:
      <!-- iterate over each member node, relative to the struct node -->
      <xsl:for-each select="member">
        <!-- name and value are relative to the member node -->
        <xsl:value-of select="name"/> : <xsl:value-of 
select="value/string"/>;<br/>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>


Josh

-----Original Message-----
From: Dylan Barber [mailto:dylan(_dot_)barber(_at_)earthlink(_dot_)net]
Sent: Thursday, March 25, 2004 11:26 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Some help with xsl transform and filter please


Okay so by your method then this would get me each member of the struct node

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="/">
        <xsl:for-each 
select="methodResponse/params/param/value/array/data/value/struct/member/value[string='Syndicated']">
                <xsl:for-each 
select="methodResponse/params/param/value/array/data/value/struct/member">
                        <xsl:value-of 
select="methodResponse/params/param/value/array/data/value/struct/member/name" 
/>
                        :
                        <xsl:value-of 
select="methodResponse/params/param/value/array/data/value/struct/member/value/string"
 />
                        ;<br />
                </xsl:for-each>
        </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

But do I really have to give the path for each item isnt there a way to say 
start at this path and go down or up?
-----Original Message-----
From: Josh Canfield <Josh(_dot_)Canfield(_at_)plumtree(_dot_)com>
Sent: Mar 25, 2004 10:33 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Some help with xsl transform and filter please

Your path is incomplete. 

<xsl:template match="/">
        <xsl:for-each select="struct/member/value[string='Syndicated']">

Your template match is putting your context at the root of the document so your 
for-each is looking for a "struct" node relative to the root, but there isn't 
one.

You can fully specify the path like this:
<xsl:for-each 
select="methodResponse/params/param/value/array/data/value/struct/member/value[string='Syndicated']">

This explicitly tells the xpath processor where to find the nodes.

Josh

[clipped.............]


Dylan Barber (CIW Professional, A+ Technician, Web Designer, Web Developer)

home phone-:- (785) 765-2664
work phone-:- (785) 539-3565 x1034
email-:- dylan(_dot_)barber(_at_)earthlink(_dot_)net
homesite-:- http://www.codegalaxy.com