473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 WordProcessingML 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:wordDocument 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:Requirements/>
<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:Requirements>.

Thanks for your help with this puzzler.
Tom Clement
Apptero, Inc.
Nov 12 '05 #1
2 1627
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*@ElideThisApptero.com> wrote in message
news:eQ**************@tk2msftngp13.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 WordProcessingML 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:wordDocument 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:Requirements/>
<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:Requirements>.

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 GetChildElementsFromNamespace(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.NamespaceURI == ns )
tagNodes.Add(elChild);
else // Only recurse if this isn't in our namespace
GetChildElementsFromNamespace(elChild, tagNodes, ns);
}
}
}

Tom

"Dare Obasanjo [MSFT]" <da***@online.microsoft.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*@ElideThisApptero.com> wrote in message
news:eQ**************@tk2msftngp13.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 WordProcessingML 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:wordDocument 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:Requirements/>
<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:Requirements>.

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
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...
0
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
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...
5
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...
18
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);...
9
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...
5
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...

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.