Xpath 使用谓词将节点与数据中的斜杠匹配

发布于 2024-10-03 04:22:11 字数 4160 浏览 7 评论 0原文

使用 SoapUI 验证 xml 响应数据时出现问题。我将问题转移到 XPath Visualizer,以便我可以解决这些问题。

使用 Xpath 查询,我想返回子节点 b:ActivityDescription 值等于的 b:AccountActivity 节点 下面的 xml 摘录中的“7.75000%10/30-11-10 $1602”。

这是我的 xpath 查询:

//b:AccountActivity[b:ActivityDescription = "7.75000%10/30-11/10    $1602"]

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <RetrieveAccountActivityResponse xmlns="http://tempuri.org/">
         <RetrieveAccountActivityResult xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:key xmlns:b="http://schemas.datacontract.org/2004/07/IntegratorSuite.DataContracts">
               <b:CurrentPage>0</b:CurrentPage>
               <b:Direction>Ascending</b:Direction>
               <b:PageSize>50</b:PageSize>
               <b:SortByParameter i:nil="true"/>
               <b:TotalEntries>4</b:TotalEntries>
               <b:TotalPages>1</b:TotalPages>
            </a:key>
            <a:value xmlns:b="http://schemas.datacontract.org/2004/07/IntegratorSuite.DataContracts">
               <b:AccountActivity>
                  <b:AccountNumber>11111111</b:AccountNumber>
                  <b:AccountType>2</b:AccountType>
                  <b:ActivityDescription>7.75000%10/30-11/10    $1602</b:ActivityDescription>
                  <b:Amount>4.1200000000</b:Amount>
                  <b:CallPut_HasValue>false</b:CallPut_HasValue>
                  <b:CallPut_Value>ALL</b:CallPut_Value>
                  <b:CmpQualCode i:nil="true"/>
                  <b:Currency>USD</b:Currency>
                  <b:Cusip></b:Cusip>
                  <b:Description1/>
                  <b:Description2/>
                  <b:EntryType>DC</b:EntryType>
                  <b:ExpirationDate>9999-12-31T23:59:59.9999999</b:ExpirationDate>
                  <b:JournalDescription i:nil="true"/>
                  <b:NetAmount>-4.1200000000</b:NetAmount>
                  <b:Price>0.000000</b:Price>
                  <b:PrincipalAmount>-4.1200000000</b:PrincipalAmount>
                  <b:Quantity>0.000000</b:Quantity>
                  <b:RecordType>H</b:RecordType>
                  <b:RootSymbol i:nil="true"/>
                  <b:SecQualCode i:nil="true"/>
                  <b:SecurityDescription>Cash</b:SecurityDescription>
                  <b:SecurityGroupCategoryID>0</b:SecurityGroupCategoryID>
                  <b:SecurityGroupDescription i:nil="true"/>
                  <b:SecurityQuantityConversion>0</b:SecurityQuantityConversion>
                  <b:SecurityTypeCode i:nil="true"/>
                  <b:StockConversionFactor>0</b:StockConversionFactor>
                  <b:StrikePrice>0.0</b:StrikePrice>
                  <b:Symbol/>
                  <b:SymbolCusip i:nil="true"/>
                  <b:TableID i:nil="true"/>
                  <b:TradeDate_HasValue>true</b:TradeDate_HasValue>
                  <b:TradeDate_Value>2010-11-11T00:00:00</b:TradeDate_Value>
                  <b:TradeDetailID_HasValue>false</b:TradeDetailID_HasValue>
                  <b:TradeDetailID_Value>0</b:TradeDetailID_Value>
                  <b:TradeNumber i:nil="true"/>
                  <b:TransactionType>Interest Collected</b:TransactionType>
                  <b:UnderlyingCusip i:nil="true"/>
                  <b:UnderlyingSymbol i:nil="true"/>
               </b:AccountActivity>
               <b:AccountActivity>
               ...

看来节点值中的正斜杠是导致匹配失败的原因。如果我从 xml 和 xpath 查询中删除它们,我会得到一个匹配。节点值中的斜杠匹配是否存在问题?似乎是一个基本问题,但我找不到答案。

Problem originated while using SoapUI to validate xml response data. I transfered the problem to XPath Visualizer so I could work through the issues.

Using an Xpath query I want to return the b:AccountActivity node that has the child node b:ActivityDescription value equal to
'7.75000%10/30-11-10 $1602' from the xml excerpt below.

This is my xpath query:

//b:AccountActivity[b:ActivityDescription = "7.75000%10/30-11/10    $1602"]

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <RetrieveAccountActivityResponse xmlns="http://tempuri.org/">
         <RetrieveAccountActivityResult xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:key xmlns:b="http://schemas.datacontract.org/2004/07/IntegratorSuite.DataContracts">
               <b:CurrentPage>0</b:CurrentPage>
               <b:Direction>Ascending</b:Direction>
               <b:PageSize>50</b:PageSize>
               <b:SortByParameter i:nil="true"/>
               <b:TotalEntries>4</b:TotalEntries>
               <b:TotalPages>1</b:TotalPages>
            </a:key>
            <a:value xmlns:b="http://schemas.datacontract.org/2004/07/IntegratorSuite.DataContracts">
               <b:AccountActivity>
                  <b:AccountNumber>11111111</b:AccountNumber>
                  <b:AccountType>2</b:AccountType>
                  <b:ActivityDescription>7.75000%10/30-11/10    $1602</b:ActivityDescription>
                  <b:Amount>4.1200000000</b:Amount>
                  <b:CallPut_HasValue>false</b:CallPut_HasValue>
                  <b:CallPut_Value>ALL</b:CallPut_Value>
                  <b:CmpQualCode i:nil="true"/>
                  <b:Currency>USD</b:Currency>
                  <b:Cusip></b:Cusip>
                  <b:Description1/>
                  <b:Description2/>
                  <b:EntryType>DC</b:EntryType>
                  <b:ExpirationDate>9999-12-31T23:59:59.9999999</b:ExpirationDate>
                  <b:JournalDescription i:nil="true"/>
                  <b:NetAmount>-4.1200000000</b:NetAmount>
                  <b:Price>0.000000</b:Price>
                  <b:PrincipalAmount>-4.1200000000</b:PrincipalAmount>
                  <b:Quantity>0.000000</b:Quantity>
                  <b:RecordType>H</b:RecordType>
                  <b:RootSymbol i:nil="true"/>
                  <b:SecQualCode i:nil="true"/>
                  <b:SecurityDescription>Cash</b:SecurityDescription>
                  <b:SecurityGroupCategoryID>0</b:SecurityGroupCategoryID>
                  <b:SecurityGroupDescription i:nil="true"/>
                  <b:SecurityQuantityConversion>0</b:SecurityQuantityConversion>
                  <b:SecurityTypeCode i:nil="true"/>
                  <b:StockConversionFactor>0</b:StockConversionFactor>
                  <b:StrikePrice>0.0</b:StrikePrice>
                  <b:Symbol/>
                  <b:SymbolCusip i:nil="true"/>
                  <b:TableID i:nil="true"/>
                  <b:TradeDate_HasValue>true</b:TradeDate_HasValue>
                  <b:TradeDate_Value>2010-11-11T00:00:00</b:TradeDate_Value>
                  <b:TradeDetailID_HasValue>false</b:TradeDetailID_HasValue>
                  <b:TradeDetailID_Value>0</b:TradeDetailID_Value>
                  <b:TradeNumber i:nil="true"/>
                  <b:TransactionType>Interest Collected</b:TransactionType>
                  <b:UnderlyingCusip i:nil="true"/>
                  <b:UnderlyingSymbol i:nil="true"/>
               </b:AccountActivity>
               <b:AccountActivity>
               ...

It appears that the forward slash in the node value is what is causing the match to fail. If I remove those from the xml and xpath query i get a match. Is there some issue with matching slashes in node values? Seem like a fundamental question but I can't find an answer.

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

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

发布评论

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

评论(3

栖迟 2024-10-10 04:22:11

事实证明,问题出在我发布的部分下面(远低于)的 xml 文档中(正如 Alejandro 建议的那样)。正在查询的元素的名称空间 URI 稍后在文档中被重新定义为不同的 URI。一旦第二个命名空间更改为不同的唯一名称,xpath 就会按预期工作。由于某种原因,当所需节点上方的字段数据中包含斜杠时,在文档后面重新定义 URI 会导致某些 xpath 工具出现异常行为,而其他工具则不会。感谢大家的意见。下次我将发布整个文档:)。

It turns out that the problem was in the xml document below (way below) the portion I posted (as Alejandro suggested). The namespace URI for the element being queried was being redefined to a different URI later in the document. Once the second namespace was changed to a different unique name the xpath works as expected. For some reason redefining the URI later in the document caused the abnormal behavior in some xpath tools and not others when field data above the desired node had a slash in it. Thanks for everyones input. Next time I will post the entire document :).

被翻牌 2024-10-10 04:22:11

我无法重现此内容

alt text

I cannot reproduce this:

alt text

扶醉桌前 2024-10-10 04:22:11

正如 Alejandro 在他对问题的评论中指出的那样,名称空间绑定在我用来测试此 xpath 的所有工具(除了 xmlspy)中都不起作用。然而,以下 xpath 确实有效(我只是忽略了名称空间)。

//*[local-name()='AccountActivity' and ./*[local-name()='ActivityDescription' and text()='7.75000%10/30-11/10    $1602']]

As Alejandro pointed out in his comment to the question, the namespace binding is not working in all the tools I used to test this xpath (except xmlspy). However the following xpath did work (I simply ignored the namespaces).

//*[local-name()='AccountActivity' and ./*[local-name()='ActivityDescription' and text()='7.75000%10/30-11/10    $1602']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文