Filtering on locations in XPath

B

Ben Jessel

All,

I have an rss feed:

<rss>
<channel>

<item>
<title>
some title
</title>
<link>
some link
</link>
<description>
some description
</description>
</item>


<item>
<title>
my
</title>
<link>
some link
</link>
<description>
test
</description>
</item>

<webMaster>[email protected]</webMaster>
</channel>
</rss>

How could I write an xPath statement that would return me all my data
apart from <item> clauses that don't meed a specific critera?

I'd like the output to be:




I've tried

"//item[( contains(title,'my') or contains(description,'test') )]"
hoping that it would return:




<rss>
<channel>
<item>
<title>
my
</title>
<link>
some link
</link>
<description>
test
</description>
</item>
<webMaster>[email protected]</webMaster>
</channel>
</rss>

However it only returns the matching <item>.

I've tried reading trhought the specification, but I really need a
real-world example to help me!

Thanks,

Ben
 
R

Richard Tobin

Ben Jessel said:
How could I write an xPath statement that would return me all my data
apart from <item> clauses that don't meed a specific critera?

You can't. XPath can only return structures that are present in the
input, and you want a modified version of the input.

You could use a stylesheet to do it.

-- Richard
 
B

Ben Jessel

You can't. XPath can only return structures that are present in the
input, and you want a modified version of the input.

Without wanting someone else to do my work for me ( :) ) could
someone come up with a sample stylesheet that gets me some of the way
to where I want to be with this?
Thanks,

Ben
 
G

Graham Shaw

Hi Ben,

not perfect but the output matches what you want and you can probably use it
as a starter

Graham

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:element name="rss">
<xsl:element name="channel">
<xsl:for-each select="/rss/channel/item">
<xsl:if test="current()[contains(title,'my') or
contains(description,'test')]">
<xsl:element name="item">
<xsl:element name="title">
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="link">
<xsl:value-of select="link"/>
</xsl:element>
<xsl:element name="description">
<xsl:value-of select="description"/>
</xsl:element>
</xsl:element>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="/rss/channel/webMaster">
<xsl:element name="webMaster">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top