473,785 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lost in XPath

I really believe it's possible to do what I want with a SelectNodes() XPath
query, but I'm lost. Any help would be appreciated. Suppose you have an
XML document (like a WordProcessingM L file) that has a bunch of elements
embedded in it that conform to a schema, but each schema-defined
parent/child relationship can have (an arbitrary number of) intervening
elements from another namespace. I'm trying to do something simple, find
all "children" (considering only my namespace) of a particular node. For
example suppose you have something like:

<w:wordDocume nt xmlns:w="http://theirs" xmlns:ns1="http ://mine" >
....
<w:body>
<wx:sect>
<ns1:ReportData >
<w:pPr>
<w:jc w:val="center" />
</w:pPr>
<w:p />
<ns1:Solution >
<w:p />
<ns1:Requiremen ts/>
<w:p />
</ns1:Solution>
<w:p />
<ns1:Path/>
</ns1:ReportData>
</wx:sect>
</w:body>
</w:wordDocument>

Suppose I have an XmlElement corresponding to the <ns1:ReportData > element.
I know I can get all ns1: descendants using SelectNodes("//ns1:*", ...).
But how in the world can I qualify this so that I only get the "child" (sort
of) descendants (within my namespace) which are <ns1:Solution > and
<ns1:Path> but not nodes that are under one of those, like
<ns1:Requiremen ts>.

Thanks for your help with this puzzler.
Tom Clement
Apptero, Inc.
Nov 12 '05 #1
2 1646
What you are trying to do is sometimes called subtree pruning or filtering
and is beyond the capabilities of XPath. I'd suggest using XSLT to solve
this problem.

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Tom Clement" <to*@ElideThisA pptero.com> wrote in message
news:eQ******** ******@tk2msftn gp13.phx.gbl...
I really believe it's possible to do what I want with a SelectNodes() XPath query, but I'm lost. Any help would be appreciated. Suppose you have an
XML document (like a WordProcessingM L file) that has a bunch of elements
embedded in it that conform to a schema, but each schema-defined
parent/child relationship can have (an arbitrary number of) intervening
elements from another namespace. I'm trying to do something simple, find
all "children" (considering only my namespace) of a particular node. For
example suppose you have something like:

<w:wordDocume nt xmlns:w="http://theirs" xmlns:ns1="http ://mine" >
...
<w:body>
<wx:sect>
<ns1:ReportData >
<w:pPr>
<w:jc w:val="center" />
</w:pPr>
<w:p />
<ns1:Solution >
<w:p />
<ns1:Requiremen ts/>
<w:p />
</ns1:Solution>
<w:p />
<ns1:Path/>
</ns1:ReportData>
</wx:sect>
</w:body>
</w:wordDocument>

Suppose I have an XmlElement corresponding to the <ns1:ReportData > element. I know I can get all ns1: descendants using SelectNodes("//ns1:*", ...).
But how in the world can I qualify this so that I only get the "child" (sort of) descendants (within my namespace) which are <ns1:Solution > and
<ns1:Path> but not nodes that are under one of those, like
<ns1:Requiremen ts>.

Thanks for your help with this puzzler.
Tom Clement
Apptero, Inc.

Nov 12 '05 #2
Dare,
Thank you for your response. I think I got a bit carried away with trying
to do it all in XPath since I actually have C# to work with. FYI, I just
wrote a function that served my narrow purposes.

private void GetChildElement sFromNamespace( XmlNode node, ArrayList tagNodes,
string ns)
{
foreach(XmlNode child in node.ChildNodes )
{
XmlElement elChild = child as XmlElement;
if( elChild != null ) // Do nothing with attributes etc.
{
if( elChild.Namespa ceURI == ns )
tagNodes.Add(el Child);
else // Only recurse if this isn't in our namespace
GetChildElement sFromNamespace( elChild, tagNodes, ns);
}
}
}

Tom

"Dare Obasanjo [MSFT]" <da***@online.m icrosoft.com> wrote in message
news:40******** @news.microsoft .com...
What you are trying to do is sometimes called subtree pruning or filtering
and is beyond the capabilities of XPath. I'd suggest using XSLT to solve
this problem.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Tom Clement" <to*@ElideThisA pptero.com> wrote in message
news:eQ******** ******@tk2msftn gp13.phx.gbl...
I really believe it's possible to do what I want with a SelectNodes()

XPath
query, but I'm lost. Any help would be appreciated. Suppose you have an XML document (like a WordProcessingM L file) that has a bunch of elements
embedded in it that conform to a schema, but each schema-defined
parent/child relationship can have (an arbitrary number of) intervening
elements from another namespace. I'm trying to do something simple, find all "children" (considering only my namespace) of a particular node. For example suppose you have something like:

<w:wordDocume nt xmlns:w="http://theirs" xmlns:ns1="http ://mine" >
...
<w:body>
<wx:sect>
<ns1:ReportData >
<w:pPr>
<w:jc w:val="center" />
</w:pPr>
<w:p />
<ns1:Solution >
<w:p />
<ns1:Requiremen ts/>
<w:p />
</ns1:Solution>
<w:p />
<ns1:Path/>
</ns1:ReportData>
</wx:sect>
</w:body>
</w:wordDocument>

Suppose I have an XmlElement corresponding to the <ns1:ReportData >

element.
I know I can get all ns1: descendants using SelectNodes("//ns1:*", ...).
But how in the world can I qualify this so that I only get the "child"

(sort
of) descendants (within my namespace) which are <ns1:Solution > and
<ns1:Path> but not nodes that are under one of those, like
<ns1:Requiremen ts>.

Thanks for your help with this puzzler.
Tom Clement
Apptero, Inc.


Nov 12 '05 #3

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

Similar topics

1
6826
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
0
1930
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
4
21210
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a native English speaker, it's quite hard to make the problem clear. Please see the following example.
5
4212
by: laks | last post by:
Hi I have the following xsl stmt. <xsl:for-each select="JOB_POSTINGS/JOB_POSTING \"> <xsl:sort select="JOB_TITLE" order="ascending"/> This works fine when I use it. But when using multiple values in the where clause as below
18
7739
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr); System.out.println(xp.evaluate(new InputSource(new FileInputStream("a.xml"))));
9
2155
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they should work. Also the second nav.OuterXml appears to also be wrong to me. Can someone explain to me why this does not work? (This is an example from a program we have where xpath can be entered in two parts so we have to be able
5
7932
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
1
2823
by: Monte Cristo | last post by:
Hi all, I'm having a problem with results being returned from a data source, it's returning the right value but without the zero's "0" in front. I've searched through google for "padding zero's in xpath" and i've only been able to find one thing but I can't tie into my xpath query. Software being used: Infopath 2003 with a Access 2003 Database. My xpath query: xdMath:Max(dfs:dataFields/d:Property/@Claim_Number) + 1 For a little...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10162
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
10101
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
8988
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...
1
7509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
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...
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
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.