beginning xslt... filtering or xml data based on the value of a node

T

tamak

I am dabbling with xml and trying to learn to harness its power...
( should have been doing this years ago, admittedly!)

my xml file is structured like so...


<clientxyz>
<Events>
<EventListing ID="5829" Title="Excavating Egyptian Archaeology"></
EventListing>
<EventListing ID="7824" Title="Ladies Day on the Lake"></
EventListing>
<EventListing ID="7825" Title="A Taste of Newberry"></EventListing>
<EventListing ID="7826" Title="Brew At The Zoo"></EventListing>
<EventListing ID="7827" Title="Veterans Day"></EventListing>
</Events>
<EventSearch>
<FindEventID eID="5290">5290</FindEventID>
</EventSearch>
</clientxyz>



What Im trying to figure out in terms of XSLT filtering (using xsl:if,
if guess(???)) is how can I have a page loop through the <Events>
node and look for the 'match' of the <FindEventID>'s eID attribute?
(( or would this be easer if I had an <eID> node within the
<FindEventID> node?

basically Im looking for an example of what the structure of the xsl
if statement / line would look like.
 
N

none

tamak said:
basically Im looking for an example of what the structure of the xsl
if statement / line would look like.

Generally, what one does in XSLT is think in terms of XPaths and
template matching

For example you might write:

<xsl:template match="EventListing[@ID=../../EventSearch/FindEventID/@eID">
...
</xsl:template>

and that template would apply only to the EventListing whose ID
attribute matched the FindEventID's eID attribute. Alternatively, you
could write

<xsl:apply-template
select="EventListing[@ID=../../EventSearch/FindEventID/@eID"/>

(I'm assuming that this apply is being invoked within a template whose
current context is the <Events/> element; if it isn't, adjust the path
appropriately.)


Without knowing what task you're actually trying to accomplish, it's
hard to tell you much more without going into a full tutorial on XSLT
programming... and if that's what you need, I'd rather direct you to the
ones which already exist, such as the resources at http://www.ibm.com/xml.
 
P

Peter Flynn

tamak said:
I am dabbling with xml and trying to learn to harness its power...
( should have been doing this years ago, admittedly!)

my xml file is structured like so...


<clientxyz>

<Events>
<EventListing ID="5829" Title="Excavating Egyptian Archaeology"></
EventListing>

If there is no text content to the element, then just this will do:

<EventListing ID="5829" Title="Excavating Egyptian Archaeology"/>

(although the form you give is fine).
<EventListing ID="7824" Title="Ladies Day on the Lake"></
EventListing>
<EventListing ID="7825" Title="A Taste of Newberry"></EventListing>
<EventListing ID="7826" Title="Brew At The Zoo"></EventListing>
<EventListing ID="7827" Title="Veterans Day"></EventListing>
</Events>
<EventSearch>
<FindEventID eID="5290">5290</FindEventID>

Missing a >. And the same applies: use said:
</EventSearch>
</clientxyz>

What Im trying to figure out in terms of XSLT filtering (using xsl:if,
if guess(???)) is how can I have a page loop through the <Events>
node and look for the 'match' of the <FindEventID>'s eID attribute?
(( or would this be easer if I had an <eID> node within the
<FindEventID> node?

You don't want to "loop" through anything. Put all thoughts of loops out
of your head :) XSLT is a declarative language, not a procedural one,
so all you do is provide a template for what should occur when certain
nodes match the template. The XSLT processor takes care of applying the
templates to the document, and executing the matching ones.

There's no need to hard-code the client name, and no need to hard-code
the event ID (you can pass it in as a parameter). In fact, in your
example, there is no 5290, so nothing will match. Here's an example that
works:

Input:
<?xml version="1.0"?>
<client name="xyz">
<Events>
<EventListing ID="5829" Title="Excavating Egyptian Archaeology"/>
<EventListing ID="7824" Title="Ladies Day on the Lake"/>
<EventListing ID="7825" Title="A Taste of Newberry"/>
<EventListing ID="7826" Title="Brew At The Zoo"/>
<EventListing ID="7827" Title="Veterans Day"/>
</Events>
</client>

XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="xml" indent="yes"/>
<xsl:param name="eID"/>

<xsl:template match="client">
<client name="{@name}">
<xsl:apply-templates select="Events/EventListing[@ID=$eID]"/>
</client>
</xsl:template>

<xsl:template match="EventListing">
<EventListing ID="{@ID}" Title="{@Title}" matched="{$eID}"/>
</xsl:template>

</xsl:stylesheet>

Run your XSLT processor with a parameter eID set to (eg) 7825.

Output:
<?xml version="1.0" encoding="UTF-8"?>
<client name="xyz">
<EventListing ID="7825" Title="A Taste of Newberry" matched="7825"/>
</client>

If for some reason you are contrained to include the match parameter
value in the document, as you suggested:

<EventSearch>
<FindEventID eID="5290"/>
</EventSearch>

you could set it into a variable at the start, instead of declaring a
parameter:

<xsl:variable name="eID" select="/client/EventSearch/FindEvent/@eID"/>

///Peter
 
T

tamak

Thanks for the replies --- I have been continuing to work on my
understanding of it all and have successfully developed the xslt to
list my records the way I want them and have the EVENTTITLE node
serving as a link using the following syntax:

pagename.asp?eID=xxxxx


Now, what I want to try to accomplish now is have a new page
(pagename.asp) that uses xslt to display the individual record from
the xml data set that has the ID that matches the eID in the
querystring.


I've tried to use <xsl:param name="eID"> and then refer to $eID in
the xsl filter but I get no results / returns (when Im positive that
that individual ID exists in the data.

So my question now is how to dynamically generate (based on a
querystring variable) the 'ID' to use in the xsl filter where Im
looking for a particular / single record (i.e. the DETAIL view of the
record)?

MY XML:

<row>
<ID>5732</ID>
<EventTitle>Craftsman Spring Classic</EventTitle>
<EventDate>3/8/2008</EventDate>
<City>Metropolis</City>
<EventDetail>Features original designs and work from more than 300
talented artists and craftspeople from across America. See the
creative process in action as many exhibitors demonstrate throughout
the weekend. </EventDetail>
</row>
<row>
<ID>5733</ID>
<EventTitle>Sweeny Todd, The Demon Barber of Fleet Street</
EventTitle>
<EventDate>3/8/2008</EventDate>
<City>Metropolis</City>
<EventDetail>In this musical masterpiece, Sweeney Todd is the
unjustly exiled barber who returns to 19th century London seeking
revenge against the judge who framed him. Sophisticated, macabre,
visceral and uncompromising, Sweeney Todd also has a great sense of
fun, mixing intense drama with howlingly funny moments of dark humor.
</EventDetail>
</row>
<row>
<ID>5734</ID>
<EventTitle>Somethings Afoot</EventTitle>
<EventDate>3/8/2008</EventDate>
<City>Metropolis</City>
<EventDetail>Join ten people who are stranded behind the door in an
isolated English country mansion and there you have it - a musical
mystery murder comedy!</EventDetail>
</row>
<row>
<ID>5735</ID>
<EventTitle>Fishes and Loaves</EventTitle>
<EventDate>3/8/2008</EventDate>
<City>Metropolis</City>
<EventDetail>Step back in time, join the crowd on a Galilean
hillside, and watch in wonder as Jesus takes one boy's lunch, and
turns it into a feast for five thousand. Listen and learn from the
greatest storyteller the world has ever known.</EventDetail>
</row>



MY XSLT:

<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="http://
amaker.dns2go.com:999/leisurefun/site2/test2.asp" --><!DOCTYPE
xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:eek:utput method="html" encoding="utf-8"/>
<xsl:template match="/">
<table>
<xsl:for-each select="export/row">
<tr>
<Td style="padding-bottom: 9px;">
<a><xsl:attribute name="href">viewdetail.asp?eID=<xsl:value-of
select="ID"/></xsl:attribute><xsl:value-of select="EventTitle"/></a></
Td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
 
P

Peter Flynn

tamak said:
Thanks for the replies --- I have been continuing to work on my
understanding of it all and have successfully developed the xslt to
list my records the way I want them and have the EVENTTITLE node
serving as a link using the following syntax:

pagename.asp?eID=xxxxx

I don't use ASP, so I have no idea of what this implies, or if it will
work. It's certainly the right URI syntax.
I've tried to use <xsl:param name="eID"> and then refer to $eID in
the xsl filter but I get no results / returns (when Im positive that
that individual ID exists in the data.

That's the correct method. How are you passing the value into the XSLT?
If it's via the ASP URI above, you may need some extra programming to
make the value pass through...consult an ASP guru for this.

To test if the value is being passed, add in the relevant template:

<xsl:message>
<xsl:text>Got eID value "</xsl:text>
<xsl:value-of select="$eID"/>
<xsl:text>"</xsl:text>
So my question now

I'd get the value-passing working first.

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

Forum statistics

Threads
474,002
Messages
2,570,258
Members
46,857
Latest member
ArleenWill

Latest Threads

Top