XPath 查询帮助语法

发布于 2024-11-05 06:15:59 字数 1223 浏览 1 评论 0原文

我在使用 xPath 表达式时遇到一些困难。

上下文:我正在使用 XML 文档在 ASP 应用程序中存储查询/存储过程名称。我使用实用程序函数来加载 xmldocument(如果之前未加载),并且尝试从文档中选择与 Id 元素匹配的节点集。这是我的 Xml 文档:

<Queries>
  <Id>USER001
    <Sql>spUsers_GetUserByUserName</Sql>
  </Id >
  <Id <USER002
    <Sql>spUsers_GetUserByEmail</Sql>
</Queries>

这是代码(我使用的是 VB.NET)

Module Utility

private sqlXml as xmldocument

'....other stuff.....


Public Function GetSql(queryId as string) as string

dim qry as string
dim node as XmlNode

if sqlXml is nothing then
   sqlXml = new xmldocument
   sqlXml.Load (..path)
end if 

qry = "//Id['" & queryId  & "']"    'xPath to select the Id node = to paramter passed

node = sqlxml.SelectSingleNode(qry) 'set node <Id><Sql></Sql></Id>

return node.SelectSingleNode("//Sql").InnerText 'Return the Sql element value from the Id

End Function

问题:

节点变量仅返回第一个元素。我已验证用于 SelectSingleNodeqry 字符串是正确的 Id 值(即 USER002) - 但是该节点正在加载 USER001 元素。显然是 xPath 表达式被搞乱了。我需要对 xPath 表达式进行哪些调整,以便可以返回正确的 元素和相应的子 元素。

任何帮助将不胜感激。

I am having some difficulty with an xPath expression.

Context: I am using an XML document to store queries / stored procedure names in an ASP application. I use a utility function to load the xmldocument (if it isn't previously loaded) and I am attempting to select the node set from the document that matches an Id element. Here is my Xml document:

<Queries>
  <Id>USER001
    <Sql>spUsers_GetUserByUserName</Sql>
  </Id >
  <Id <USER002
    <Sql>spUsers_GetUserByEmail</Sql>
</Queries>

Here is the code (I am using VB.NET)

Module Utility

private sqlXml as xmldocument

'....other stuff.....


Public Function GetSql(queryId as string) as string

dim qry as string
dim node as XmlNode

if sqlXml is nothing then
   sqlXml = new xmldocument
   sqlXml.Load (..path)
end if 

qry = "//Id['" & queryId  & "']"    'xPath to select the Id node = to paramter passed

node = sqlxml.SelectSingleNode(qry) 'set node <Id><Sql></Sql></Id>

return node.SelectSingleNode("//Sql").InnerText 'Return the Sql element value from the Id

End Function

The Problem:

the node variable only returns the first element. I have verified the that the qry string that is used to SelectSingleNode IS the correct Id value (i.e. USER002) - however the node is getting loaded with the USER001 element. It is obviously the xPath expression that is messed up. What do I need to tweak on the xPath expression so that I can return the correct <Id> element and corresponding child <Sql> element.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2024-11-12 06:15:59

Google XML COOKTOP 并安装它。它是一个很棒的小型免费软件应用程序,用于在 XML 数据文件上尝试 XPATH 和 XSLT 表达式并查看结果。

Google XML COOKTOP and install it. It's a great little freeware app for trying out XPATH and XSLT expressions on XML data files and seeing the results.

雄赳赳气昂昂 2024-11-12 06:15:59

看起来您正在尝试构建类似 //Id['USER002'] 的表达式。这将选择“USER002”的有效布尔值为 true 的所有 E 元素 - 即全部。目前尚不清楚您的意图,因为您的 XML 示例一团糟,但它应该类似于 //Id[@id='USER002']

另外,您确实不应该通过以下方式构建 XPath 表达式字符串连接。当有人提供包含引号的字符串时,它会让您容易受到注入攻击和意外故障。这也是低效的。构造一个包含变量引用 $param 的字符串,并从 API 设置参数。 (恐怕不知道如何在 VB 中做到这一点)。

It looks like you're trying to to construct an expression like //Id['USER002']. That will select all the E elements for which the effective boolean value of 'USER002' is true - which is all of them. It's not clear what you intended because your XML sample is a mess, but it should be something like //Id[@id='USER002']

Also, you really shouldn't be constructing XPath expressions by string concatenation. It leaves you prone to injection attacks and to accidental misfunctioning when someone supplies a string containing quotes. It's also inefficient. Construct a string containing a variable reference $param, and set the parameter from the API. (Don't know how to do that in VB, I'm afraid).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文