需要XPath帮助 [英] XPath help needed

查看:60
本文介绍了需要XPath帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


我有一个带有以下结构的xml文档,其中Form是

文档元素:

< Form>

< Content>

< Line> blah blah blah< / Line>

<行>

< PresentationGroup>

杂项标签

< / PresentationGroup>

< / Line>

< Line> blah blah blah 2< / Line>

< Line>

< PresentationGroup>

其他标签

< / PresentationGroup>

< / Line>

< / Content>

< / Form>

我想要XPath到最后一个PresentationGroup节点(每个xml文档有多个

PresentationGroup节点和PresentationGroup的数量

每个文档的节点从一个文档更改为下一个文档。我试过了

" Content / Line / PresentationGroup [last()]"我什么也没得到。


我的代码如下:


xmlDoc.DocumentElement.SelectSingleNode(" Content / Line / PresentationGroup [last( )]")


返回Nothing。我不知道为什么,虽然我确定它很简单!


任何人都可以看到我做错了什么?


TIA,

-

Joe

Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I''ve tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can''t figure out why, although I''m sure that it''s
simple!

Can anyone see what I am doing wrong?

TIA,
--
Joe

推荐答案



xmlDoc.DocumentElement.SelectSingleNode(" // PresentationGroup [last()]")



xmlDoc .DocumentElement.SelectSingleNode(" / Form / Content / Line / PresentationGroup [last()]")

-

HTH,

Phillip Williams
http://www.societopia.net
http://www.webswapp.com

" Joe" ;写道:

xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:
大家好:

我有一个带有以下结构的xml文档,其中Form是
文档元素:

<表格>
<内容>
<行>等等等等等等<行>
< PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< Line> blah blah blah 2< / Line>
< Line>
< ; PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< / Content>
< / Form>
<我希望XPath到最后一个PresentationGroup节点(每个xml文档有多个PresentationGroup节点,每个文档的PresentationGroup
节点数从一个文档更改为下一个文档)。我试过了
Content / Line / PresentationGroup [last()]我什么也没得到。

我的代码如下:

xmlDoc.DocumentElement.SelectSingleNode(" Content / Line / PresentationGroup [last()]")

这将返回Nothing。我不知道为什么,虽然我很确定它很简单!

任何人都可以看到我做错了吗?
TIA,
-
Joe
Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I''ve tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can''t figure out why, although I''m sure that it''s
simple!

Can anyone see what I am doing wrong?

TIA,
--
Joe



Phillip,


我试过这两个。由于某些原因,每个返回第一个

PresentationGroup。知道为什么吗?


-

Joe

" Phillip Williams"写道:
Phillip,

I have tried both of these. For some reason each returns the first
PresentationGroup. Any idea why?

--
Joe
"Phillip Williams" wrote:

xmlDoc.DocumentElement.SelectSingleNode(" // PresentationGroup [last()]")

xmlDoc.DocumentElement。 SelectSingleNode(" / Form / Content / Line / PresentationGroup [last()]")

-
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com

" Joe"写道:

xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:
大家好:

我有一个带有以下结构的xml文档,其中Form是
文档元素:

<表格>
<内容>
<行>等等等等等等<行>
< PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< Line> blah blah blah 2< / Line>
< Line>
< ; PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< / Content>
< / Form>
<我希望XPath到最后一个PresentationGroup节点(每个xml文档有多个PresentationGroup节点,每个文档的PresentationGroup
节点数从一个文档更改为下一个文档)。我试过了
Content / Line / PresentationGroup [last()]我什么也没得到。

我的代码如下:

xmlDoc.DocumentElement.SelectSingleNode(" Content / Line / PresentationGroup [last()]")

这将返回Nothing。我不知道为什么,虽然我很确定它很简单!

任何人都可以看到我做错了吗?
TIA,
-
Joe
Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I''ve tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can''t figure out why, although I''m sure that it''s
simple!

Can anyone see what I am doing wrong?

TIA,
--
Joe






Joe写道:



Joe wrote:

我有一个具有以下结构的xml文档,其中Form是
文档元素:

< Form> ;
<内容>
< Line> blah blah blah< / Line>
< Line>
< PresentationGroup>
杂项标签
< / PresentationGroup>
< / Line>
< Line> blah blah blah 2< / Line>
< Line>
< PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< / Content>
< / Form>
xmlDoc.DocumentElement.SelectSingleNode(" Content / Line / PresentationGroup [last()]")
I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form> xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")




这是一个包含你的完整C#代码片段提供XML作为输入

XML和你提供的XPath表达式,当然找到了元素:


string xmlMarkup = @"< Form>

< Content>

< Line> blah blah blah< / Line>

< Line>

< PresentationGroup>

其他标签

< / PresentationGroup>

< / Line>

< Line> blah blah blah 2< / Line>

< Line>

< PresentationGroup>

其他标签

< / PresentationGroup>

< / Line>

< / Content>

< / Form>" ;;

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.LoadXml(xmlMarkup);

XmlElement presentationGroup =

xmlDocu ment.DocumentElement.SelectSingleNode(@" Con tent / Line / PresentationGroup [last()]")

as XmlElement;

if(presentationGroup!= null){

Console.WriteLine(presentationGroup.OuterXml);

}

else {

Console.WriteLine("找不到任何元素。);

}


因此,您的真实代码和/或真实XML更复杂。只是

猜测,该XML文档是否声明了名称空间,特别是

默认名称空间,例如

< Form xmlns =" http:/ /example.com/2006/ns1">

?然后检查

< http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>


-


Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


这篇关于需要XPath帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆