Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - beginning xslt... filtering or xml data based on the value of a node >Thread Next - Re: beginning xslt... filtering or xml data based on the value of a node Re: beginning xslt... filtering or xml data based on the value of a nodeTo: NULL Date: 4/3/2008 12:17:00 AM
tamak wrote:
> 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>
This looks suboptimal. <client name="xyz"> is more extensible. Unless I
have misunderstood something.
> <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 <FindEventID eID="5290"/>
> </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:output 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
--
XML FAQ: http://xml.silmaril.ie/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
