A better xPath statement

M

mTbrains

I'm trying to pull out just three elements with a particular content
from a list of elements
my xml:

<content>
<topic>
<name>Sport</name>
....
</topic>
...
<topic>
<name>Health</name>
...
</topic>
.....
</content>
Here's what works in my xsl file, but there's probably a
better/easier/more efficient way of doing it:

<xsl:for-each select="topic[./name='Sports']| topic[./name='Movies']|
topic[./name='US News']">

Thanks in advance...martin
 
D

David Carlisle

mTbrains said:
I'm trying to pull out just three elements with a particular content
from a list of elements
my xml:

<content>
<topic>
<name>Sport</name>
....
</topic>
...
<topic>
<name>Health</name>
...
</topic>
....
</content>
Here's what works in my xsl file, but there's probably a
better/easier/more efficient way of doing it:

<xsl:for-each select="topic[./name='Sports']| topic[./name='Movies']|
topic[./name='US News']">

Thanks in advance...martin

well you could (always) drop the ./ as the Xpath is the same without
that.

then something like

<xsl:for-each select="topic[name='Sports' or name='Movies' or name='US
News']

or a bit shorter but perhaps a little less clear:

<xsl:for-each select="topic[name[.='Sports' or .='Movies' or .='US News']]

In XPath2 you will be able to do

<xsl:for-each select="topic[name=('Sports','Movies','US News')]

David
 
M

Martin Honnen

mTbrains said:
I'm trying to pull out just three elements with a particular content
from a list of elements
my xml:

<content>
<topic>
<name>Sport</name>
....
</topic>
...
<topic>
<name>Health</name>
...
</topic>
....
</content>
Here's what works in my xsl file, but there's probably a
better/easier/more efficient way of doing it:

<xsl:for-each select="topic[./name='Sports']| topic[./name='Movies']|
topic[./name='US News']">

topic[name = 'Sports' or name = 'Movies' or name = 'US News']
 

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

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top