Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filtering xml with xslt

Status
Not open for further replies.

tonyamak

Programmer
Mar 30, 2008
2
US
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.
 
Some basic tutorials might help: XSLT Xpath

In general, one would not loop looking for something. The concept of looping comes from your procedural programming experience. While XSL has an xsl:for-each processing instruction, you are really declaring how to treat each node of a nodeset.

Instead, one would use an XPath predicate, much like you would use a WHERE clause in SQL rather than loop through all the rows in a table looking for the right row.

The form of an XPath expression for the requirement might look something like this:
Code:
clientxyz/Events/EventListing[@ID = //EventSearch/FindEventID]
This is evaluated as:
[ul][li]Find the nodeset of EventListing nodes subordinate to a Events node which in turn is subordinate to[/li][li]a clientxyz node, where[/li][li]the value of the ID attribute in the EventListing node(s) equals[/li][li]the value of a FindEventID node which is subordinate to [/li][li]an EventSearch node which is anywhere in the document[/li][/ul]
The expression inside the square brackets [] is the predicate which limits the set of EventListing nodes that are returned.

Tom Morrison
 
ahhhh... I think I get it.

Thanks.

Question so would the xsl 'statement' be something like:

Code:
<xsl:when clientxyz/Events/EventListing[@ID = //EventSearch/FindEventID]>

... whatver I want to show when a match is found for id...

</xsl:when>
 
[0] Your EventSearch has no match to EventListing in the example. You should at least add some which do have matches to see more clearly the outcome.

[1] When you conceive the approach involving xsl:when at all, you are using a pull approach (more closely analoguous to procedureal). This may be a slightly more advanced viewpoint than presently you need to see the thing. Besides, your xsl:when syntax is not correct.

[2] The intriguing thing is that the = in the predicate is a kind of called general comparison. It is not two-value logic. a=b maybe true, at the same time a!=b maybe true as well, (for some nodes of EventListing). But again this is a more difficult point.

[3] To help you see the point made by k5tm, this is a push kind of xslt using the exact one line of xpath.
[tt]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="text" />
<xsl:template match="/">
<xsl:apply-templates select="clientxyz/Events/EventListing" />
</xsl:template>
[blue]<xsl:template match="clientxyz/Events/EventListing[@ID = //EventSearch/FindEventID]">[/blue]
<xsl:value-of select="concat(@ID,':','&#x0d;&#x0a;')" />
<xsl:value-of select="concat('&#09;',@Title,'&#x0d;&#x0a;')" />
</xsl:template>
</xsl:stylesheet>
[/tt]
[3.1] There are equally valid substitutions with more or less clarity to the blue line above, depending of how you look at the problem.
[tt]
<xsl:template match="clientxyz/Events/EventListing[@ID = ../../EventSearch/FindEventID]">
<!-- since eID and the text are the same (but eID may be less accidental -->
<xsl:template match="clientxyz/Events/EventListing[@ID = ../../EventSearch/FindEventID/@eID]">
<!-- or -->
<xsl:template match="clientxyz/Events/EventListing[@ID = //EventSearch/FindEventID/@eID]">
[/tt]
[4] You can also experiment by looking at what do get in the special kind of "not equal" (!=) behaviour and may find it perplexed, as explained in [2].
[tt]
<xsl:template match="clientxyz/Events/EventListing[@ID [highlight]![/highlight]= ../../EventSearch/FindEventID/@eID]">
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top