473,503 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XPath selecting on subnodes

Hi,

I have the following XML:

<a>
<b>
<c name="bob"/>
<c name="billy"/>
</b>
<b>
<c name="george"/>
<c name="nick"/>
</b>
</a>

I'm trying to write an XPath-expression to get this XML:

<b>
<c name="billy"/>
</b>
<b>
<c name="george"/>
<c name="nick"/>
</b>

but I can't get it right.

I have tried /a/b/c[@name != 'bob']/.. but it does not work. I know
why, but I don't know how to fix it.

Any help is appreciated.

/Bent

Nov 23 '06 #1
1 2885
Hapsdog wrote:
I have the following XML:

<a>
<b>
<c name="bob"/>
<c name="billy"/>
</b>
<b>
<c name="george"/>
<c name="nick"/>
</b>
</a>

I'm trying to write an XPath-expression to get this XML:

<b>
<c name="billy"/>
</b>
<b>
<c name="george"/>
<c name="nick"/>
</b>

but I can't get it right.
You can't. XPath is selection language and what you need is filtering.
Once you select node b you get it as is with all children nodes.
A simple XSLT script could do the job:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Filter this one out -->
<xsl:template match="c[@name='bob']"/>
</xsl:stylesheet>

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Nov 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2975
by: Alastair Cameron | last post by:
VB6, MSXML 3.2 installed: Q1. I am having a problem selecting nodes with XPATH expressions when an attribute values contain backslashes (\\) in as part of its value: For example the...
2
6044
by: Neil Zanella | last post by:
Hello, I would like to know whether the mozilla web browser has built in support for searching XML documents via XPath expressions as with IE's xmlobject's and xmlDoc's function selectNodes() or...
2
2954
by: kj | last post by:
Suppose I have some XML document that contains tags of the form <... xmlns:foo="http://www.bar.org/foo"> <... xmlns:foo="baz"> <... xmlns:frobozz="http://www.bar.org/foo"> What's the...
2
2114
by: Kristofer Andersson | last post by:
I am using xslt to transform some parts of a document but want to output some parts of the document just like they are (node with all subnodes and attributes). Below are the templates I am...
1
1652
by: Swanand Mokashi | last post by:
Ok here is my XML structure : <Files> <File Name="One.txt"> <Client Name="test1"> <Errors> <OtherErrorIDs> <OtherErrorID>1</OtherErrorID> <OtherErrorID>2</OtherErrorID>...
4
3788
by: amessimon | last post by:
Hi, im trying to use the Xpath function "reverse" with an XmlTextReader. Im not sure if i need to add a namespace to reference this function or what, if so whats the address of the namespace. Can...
3
3542
by: Bloody Viking | last post by:
Namaste, Y'all! I've got a valid XQuery expression that I need to convert to XPath 2.0. This expression will be stored in a resource file and applied to XML by a Java program with saxon8.jar...
0
1123
by: wernh | last post by:
Hi, I have the following xml sample file: <?xml version="1.0" encoding="utf-8"?> <catalogue id="catalogue"> <alldocs id="alldocs"> <documents id="New Heading1" desc="" link="#" uid="1">...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.