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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xpath and for-each 1

Status
Not open for further replies.

MrT2005

Programmer
Joined
Mar 8, 2005
Messages
22
Location
CA
demo xml file

<books>
<book>
<title>My Book</title>
<author>
<name>Mr. McGillicuddy</name>
<dob>01.01.1901</dob>
<city>Falafel</city>
</author>
</book>
</books>

Im trying to search the books and find the name
of the authors that match the $keyword parameter.

<xsl:for-each select="//books[.=$keyword]">

this doesnt work, any ideas?

 
Code:
<xsl:for-each select="//books[author/name = $keyword]">
Avoid using // as it will search all levels of the tree. Use "mypath/books..." instead.
 
try something like [untested]:


Code:
<xsl:template match="*" >
    <xsl:apply-templates mode="keyword-search" select="//*[text() = $keyword]"/>
</xsl:template>
<xsl:template match="title" mode="keyword-search">
    Title: <xsl:value-of select="."/>
    Author <xsl:value-of select="../author"/>
</xsl:template>

<xsl:template match="author/name" mode="keyword-search">
   Title: <xsl:value-of select="../../title"/>
   Author: <xsl:value-of select="./name"/>
</xsl:template>

...etc..

you sometimes have to think laterally with xsl.


Hope that helps

Matt

 
They're just searching by author, not by any text. Nice idea though.
 
thanks JontyMC

works perfect!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top