Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: Peter Flynn <peter.nosp@-.--------.-->
To: 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent