473,654 Members | 3,084 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 2894
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:styleshe et 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 following statement fails to find a node (even though one exists in the XML) if the value of the LDAPServerURL attribute is \\LocalServer but works if the value is "LocalServer".
2
6056
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 similar. Thanks! Neil
2
2968
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 XPath expression to select the namespace nodes with prefix "foo"? And what's the XPath expression to select the
2
2132
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 currently using. The only problem I have is that the value of parent nodes will be the value of all subnodes ie "<parentnode><childnode>value</childnode></parentnode>" will become "<parentnode>value<childnode>value</childnode></parentnode>" after...
1
1657
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> <OtherErrorID>4</OtherErrorID> </OtherErrorIDs>
4
3802
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 anyone give me an idea where i am going wrong here? So far.... Dim strXmlFeedUrl As String = server.mappath("test.xml") Dim reader As XmlTextReader = New XmlTextReader(strXmlFeedUrl)
3
3557
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 (I'm pretty sure it's v.8.7). The manifest from the jar file follows: Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.0 Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.) Project-Name: Saxon-B
0
1128
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"> <document id="New Document6" desc="" link="#" keywords="" uid="6" /> <documents id="New Heading9" desc="" link="#" uid="9"> <document id="New Document12" desc="" link="#" keywords="" uid="12" /> <document id="New Document14" desc=""...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.