XPath问题 [英] XPath Question

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

问题描述

好的,这是交易。我有一个小的XML文件,代表一个小的
数据库表。我将它加载到System.XML.XMLDocument中。到目前为止一直很好。

我对它运行XPath查询以检索所有字段名称。一切都好

工作正常。


这是我的XML文档:


<?xml版本= QUOT; 1.0"独立= QUOT;是" ?>

< DataSet1 xmlns =" http://www.tempuri.org/DataSet1.xsd">

< tblItem>

< ID> 1< / ID>

<名称>垃圾邮件< /名称>

<类别>食物< /类别>

< Description>美味!没有天然成分< /描述>

<价格> 4< /价格>

< ImageURL> images / 1.png< / ImageURL>

< LargeImageURL> images / L1.png< / LargeImageURL>

< / tblItem>

< tblItem>

< ; ID> 2< / ID>

<名称>远程控制< /名称>

<类别>其他< / Category>

<描述>通用遥控器< /描述>

<价格> 12< / Price>

< ImageURL> images / 2.png< / ImageURL>

< LargeImageURL> images / L2.png< / LargeImageURL>

< / tblItem>

< / DataSet>


现在是棘手的部分。我正在尝试提出三个XPath查询

将返回以下内容:


1)所有具有子ID节点的tblItem节点值为1(即,所有

tblItem,其中ID = 1),

2)所有具有杂项类别的tblItem节点(即,所有

tblItem其中Category =''Miscellaneous'')

3)所有在描述中都有单词Remote的tblItem ndoes(即所有

tblItem类别在任何位置都包含Remote这个词。


来自SQL背景,我很难实现XPath

表达式。我希望有人能指出我正确的方向。
方向。我尝试了几种组合,例如tblItem / ID [。=''1'']",

" tblItem [ID =''1'']"和// tblItem [ID =''Miscellaneous'']"。他们中的任何一个似乎都不会工作......


TIA

OK, here''s the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here''s my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I''m trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = ''Miscellaneous'')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word ''Remote'' in any position)

Coming from a SQL background, I''m having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I''ve tried several combinations, like "tblItem/ID[.=''1'']",
"tblItem[ID=''1'']" and "//tblItem[ID=''Miscellaneous'']". None of them seem to
be working though...

TIA

推荐答案

你的前两个查询并不困难:
your first two queries are not difficult:
1)所有tblItem节点都有一个值为1的子ID节点(即
)所有tblItem,其中ID = 1),


// tblItem [ID = 1]

2)所有具有杂项类别的tblItem节点(即,所有
tblItem其中Category =''Miscellaneous'')


// tblItem [Category =''Miscellaneous'']

3)全部tblItem在描述中有单词Remote的ndoes(即
所有tblItem,其中Category在任何位置都包含单词''Remote')

我不知道这个。

如果你想在代码中执行此操作,必须记住的一件事是,你必须设置xpath查询的命名空间管理器才能包括在内

定义的名称空间。在您的示例中,您有一个默认命名空间。如果你没有提供命名空间管理器,那么你的查询总是会返回

零结果。


-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:本论坛发表的意见均为我自己的意见,而不是

代表我的雇主。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-

" Michael C#" < xy*@abcdef.com>在消息中写道

新闻:65 ***************** @ fe08.lga ......好的,这是交易。我有一个小的XML文件,代表一个小的数据库表。我将它加载到System.XML.XMLDocument中。到目前为止一直很好。
我对它运行XPath查询以检索所有字段名称。
其中一切正常。

这是我的XML文档:

<?xml version =" 1.0"独立= QUOT;是" ?>
< DataSet1 xmlns =" http://www.tempuri.org/DataSet1.xsd">
< tblItem>
< ID> 1< / ID> ;
<名称>垃圾邮件< /名称>
<类别>食物< / Category>
<说明>美味!没有天然成分< /说明>
<价格> 4℃; /价格>
< IMAGEURL>图像/ 1.png< / IMAGEURL>
< LargeImageURL>图像/ L1.png< ; / LargeImageURL>
< / tblItem>
< tblItem>
< ID> 2< / ID>
<名称>远程控制< /名称>
<类别>其他< / Category>
<说明>通用遥控器< /描述>
<价格> 12< / Price>
< ImageURL> images / 2。 png< / ImageURL>
< LargeImageURL> images / L2.png< / LargeImageURL>
< / tblItem>
< / DataSet>

现在棘手的部分。我正在尝试提出三个XPath查询
将返回以下内容:
1)所有tblItem节点都有一个值为1的子ID节点(即
所有tblItem,其中ID = 1),
2)所有具有杂项类别的tblItem节点(即,所有
tblItem,其中Category =''Miscellaneous'')
3)所有在描述中都有单词Remote的tblItem ndoes(即
所有tblItem,其中Category在任何位置都包含单词''Remote')

来自SQL背景,我'我很难实现XPath
表达式。我希望有人能指出我正确的方向。我尝试了几种组合,例如tblItem / ID [。=''1'']",
" tblItem [ID =''1'']"和// tblItem [ID =''Miscellaneous'']"。他们中的任何一个看起来都不是
... ... TIA
1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all tblItem where ID = 1),
//tblItem[ID=1]
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = ''Miscellaneous'')
//tblItem[Category=''Miscellaneous'']
3) All tblItem ndoes that have the word Remote in the Description (i.e., all tblItem where Category contains the word ''Remote'' in any position)
I don''t know this one.
One thing that you have to keep in mind, if you want to do this in code, is
that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace. If
you don''t provide the namespace manager, then your query will ALWAYS return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--
"Michael C#" <xy*@abcdef.com> wrote in message
news:65*****************@fe08.lga... OK, here''s the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything there works fine.

Here''s my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I''m trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = ''Miscellaneous'')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all tblItem where Category contains the word ''Remote'' in any position)

Coming from a SQL background, I''m having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I''ve tried several combinations, like "tblItem/ID[.=''1'']",
"tblItem[ID=''1'']" and "//tblItem[ID=''Miscellaneous'']". None of them seem to be working though...

TIA



还有一件事:与XPath查询教程的良好链接是:
http: //www.w3schools.com/xpath/xpath_syntax.asp

-

--- Nick Malik [微软]
MCSD,CFPS,认证Scrummaster
http://blogs.msdn .com / nickmalik


免责声明:本论坛发表的意见是我自己的,而不是我雇主的b $ b代表。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-

Nick Malik [微软]" < NI ******* @ hotmail.nospam.com>在消息中写道

新闻:Gb ******************** @ comcast.com ...
one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:Gb********************@comcast.com...
你的前两个查询并不困难:
your first two queries are not difficult:
1)所有tblItem节点都有一个值为1的子ID节点(即所有
1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem其中ID = 1),
tblItem where ID = 1),


// tblItem [ID = 1]



//tblItem[ID=1]

2)所有具有杂项类别的tblItem节点(即,所有
tblItem其中Category =''Miscellaneous'')
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = ''Miscellaneous'')



// tblItem [Category =''Miscellaneous'']



//tblItem[Category=''Miscellaneous'']

3)所有在描述中都有单词Remote的tblItem ndoes(即
3) All tblItem ndoes that have the word Remote in the Description (i.e.,


所有

tblItem其中Category在任何位置包含单词''Remote')
tblItem where Category contains the word ''Remote'' in any position)


我不知道这个。

你要记住一件事,如果你想在代码中这样做,


I don''t know this one.
One thing that you have to keep in mind, if you want to do this in code,



就是那个您必须为xpath查询设置名称空间管理器以包含已定义的名称空间。在您的示例中,您有一个默认命名空间。
如果你不提供命名空间管理器,那么你的查询总是
返回零结果。

-
--- Nick Malik [微软]
MCSD,CFPS,认证Scrummaster
http://blogs.msdn。 com / nickmalik

免责声明:本论坛中发表的意见是我自己的,而不是我雇主的代表。
我代表我不回答问题雇主。我只是一名帮助程序员的程序员。
-
Michael C# < xy*@abcdef.com>在消息中写道
新闻:65 ***************** @ fe08.lga ...


is that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace. If you don''t provide the namespace manager, then your query will ALWAYS return zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--
"Michael C#" <xy*@abcdef.com> wrote in message
news:65*****************@fe08.lga...

好的,这里是交易。我有一个小的XML文件,代表一个小的数据库表。我将它加载到System.XML.XMLDocument中。到目前为止,所以
好​​。我对它运行XPath查询以检索所有字段名称。一切
OK, here''s the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the field names. Everything
工作正常。

这是我的XML文档:

<?xml version =" 1.0"独立= QUOT;是" ?>
< DataSet1 xmlns =" http://www.tempuri.org/DataSet1.xsd">
< tblItem>
< ID> 1< / ID> ;
<名称>垃圾邮件< /名称>
<类别>食物< / Category>
<说明>美味!没有天然成分< /说明>
<价格> 4℃; /价格>
< IMAGEURL>图像/ 1.png< / IMAGEURL>
< LargeImageURL>图像/ L1.png< ; / LargeImageURL>
< / tblItem>
< tblItem>
< ID> 2< / ID>
<名称>远程控制< /名称>
<类别>其他< / Category>
<说明>通用遥控器< /描述>
<价格> 12< / Price>
< ImageURL> images / 2。 PNG< / IMAGEURL>
< LargeImageURL>图像/ L2.png< / LargeImageURL>
< / tblItem>
< /数据集>

现在对于棘手的部分。我正在尝试提出三个将返回以下内容的XPath查询:
1)所有具有值为1的子ID节点的tblItem节点(即
there works fine.

Here''s my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I''m trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,


所有

tblItem,其中ID = 1),
2)所有具有杂项类别的tblItem节点(即,所有类别=其他类别= ''其他'')3)所有在描述中都有单词Remote的tblItem ndoes(即
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = ''Miscellaneous'')
3) All tblItem ndoes that have the word Remote in the Description (i.e.,


所有

tblItem其中Category包含单词' 遥控在任何位置)

来自SQL背景,我很难实现XPath
表达式。我希望有人能指出我正确的方向。我尝试了几种组合,例如tblItem / ID [。=''1'']",
" tblItem [ID =''1'']"和// tblItem [ID =''Miscellaneous'']"。他们都没有
tblItem where Category contains the word ''Remote'' in any position)

Coming from a SQL background, I''m having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I''ve tried several combinations, like "tblItem/ID[.=''1'']",
"tblItem[ID=''1'']" and "//tblItem[ID=''Miscellaneous'']". None of them



似乎


seem to

正在工作......

TIA
be working though...

TIA




嗨Michael,


因为你的XML中有元素而不是属性,你的xpath查询

即tblItem [ID =''Miscellaneous'']将不起作用,因为在这种情况下ID是

元素而不是属性。这种类型的XPath查询只能用于

属性。

要使其工作,您需要使用text()函数。你的三个场景的XPath查询如下,


1)所有tblItem节点都有一个值为1的子ID节点(即,所有

tblItem其中ID = 1)


/ DataSet1 / tblItem // ID [text()=''1'']


2)所有具有杂项类别的tblItem节点(即,所有

tblItem,其中Category =''Miscellaneous'')


/ DataSet1 / tblItem //类别[text()=''其他'']


3)所有tblItem在描述中都有单词Remote(即全部

tblItem其中Category在任何位置包含单词''Remote''


/ DataSet1 / tblItem //描述[contains(text(),'''远程'')]


欲了解更多信息,请查看以下网址...


< http: //www.developer.com/net/net/article.php/11087_3383961_1>


HTH


问候,

Madhu


MVP - C#| MCSD.NET


" Michael C#"写道:
Hi Michael,

Since you have elements instead of attributes in your XML, your xpath query
i.e. tblItem[ID=''Miscellaneous''] will not work since ID in this case is a
element and not a attribute. This type of XPath query can be done only for
attributes.
To make it work, you need to use the text() function. the XPath query for
your three scenarios are as follows,

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1)

/DataSet1/tblItem//ID[text()=''1'']

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = ''Miscellaneous'')

/DataSet1/tblItem//Category[text()=''Miscellaneous'']

3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word ''Remote'' in any position)

/DataSet1/tblItem//Description[contains(text(),''Remote'')]

For more information abt Xpath queries... look at the below url...

<http://www.developer.com/net/net/article.php/11087_3383961_1>

HTH

Regards,
Madhu

MVP - C# | MCSD.NET

"Michael C#" wrote:
好的,这是交易。我有一个小的XML文件,代表一个小的数据库表。我将它加载到System.XML.XMLDocument中。到目前为止一直很好。
我对它运行XPath查询以检索所有字段名称。一切都工作正常。

这是我的XML文档:

<?xml version =" 1.0"独立= QUOT;是" ?>
< DataSet1 xmlns =" http://www.tempuri.org/DataSet1.xsd">
< tblItem>
< ID> 1< / ID> ;
<名称>垃圾邮件< /名称>
<类别>食物< / Category>
<说明>美味!没有天然成分< /说明>
<价格> 4℃; /价格>
< IMAGEURL>图像/ 1.png< / IMAGEURL>
< LargeImageURL>图像/ L1.png< ; / LargeImageURL>
< / tblItem>
< tblItem>
< ID> 2< / ID>
<名称>远程控制< /名称>
<类别>其他< / Category>
<说明>通用遥控器< /描述>
<价格> 12< / Price>
< ImageURL> images / 2。 PNG< / IMAGEURL>
< LargeImageURL>图像/ L2.png< / LargeImageURL>
< / tblItem>
< /数据集>

现在对于棘手的部分。我正在尝试提出三个将返回以下内容的XPath查询:

1)所有具有值为1的子ID节点的tblItem节点(即所有
tblItem其中ID = 1),
2)所有tblItem节点都有一个杂项类别(即所有
tblItem,其中Category =''Miscellaneous'')
3)所有在描述中都有单词Remote的tblItem ndoes(即,所有
tblItem,其中Category在任何位置都包含单词''Remote')

来自SQL背景,我'我很难实现XPath
表达式。我希望有人能指出我正确的方向。我尝试了几种组合,例如tblItem / ID [。=''1'']",
" tblItem [ID =''1'']"和// tblItem [ID =''Miscellaneous'']"。他们似乎都没有工作......

TIA
OK, here''s the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here''s my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I''m trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = ''Miscellaneous'')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word ''Remote'' in any position)

Coming from a SQL background, I''m having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I''ve tried several combinations, like "tblItem/ID[.=''1'']",
"tblItem[ID=''1'']" and "//tblItem[ID=''Miscellaneous'']". None of them seem to
be working though...

TIA



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

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