【问题标题】:XPath for indexed node in an XML documentXML 文档中索引节点的 XPath
【发布时间】:2013-12-05 03:49:41
【问题描述】:

我有以下文件。我正在尝试在ABCSite[0] 获取test:Category。 尝试了 XPath $ABCCustomerHierarchy(//ata:ABCSite[0])/test:Customer/test:CustomerID/test:Category,但我得到了一些 XPath 异常。

谁能在这里告诉我正确的 XPath 吗?

 <test:ABCCustomerHierarchy xmlns:test="http://wwwin.test.com/testschema">
        <test:ABCSite>
            <test:Customer>
                <test:CustomerID>
                    <test:ID>110984181</test:ID>
                    <test:Category>ABC_Customer</test:Category>
                </test:CustomerID>
            </test:Customer>
            <test:CustomerReferences>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>KLM_CUSTOMER</test:Category>
                </test:CustomerID>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>XYZ_Customer</test:Category>
                </test:CustomerID>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>PQR_CUSTOMER</test:Category>
                </test:CustomerID>
            </test:CustomerReferences>
        </test:ABCSite>
        <test:ABCSite>
            <test:Customer>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>XYZ_Customer</test:Category>
                </test:CustomerID>
            </test:Customer>
            <test:CustomerReferences>
                <test:CustomerID>
                    <test:ID>17808</test:ID>
                    <test:Category>PQR_CUSTOMER</test:Category>
                </test:CustomerID>
            </test:CustomerReferences>
        </test:ABCSite>
    </test:ABCCustomerHierarchy>

【问题讨论】:

  • 拜托,编辑框正下方有一个预览。你的代码格式被严重破坏了。此外,“某些”错误消息也无济于事,请显示您收到的 错误消息。

标签: xpath


【解决方案1】:

你可以试试:

//ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category

【讨论】:

  • 没有。这没有让我获得 [0] 的节点
  • XPath 是 1-indexed。没有元素 0。如果你想要第一个,@fastcodejava 的解决方案是正确的(但他省略了命名空间,这可能是个问题)。尝试使用带有命名空间的 your 查询,但索引为 1。
  • 我只尝试使用命名空间,问题是如果我像 //ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category = 'ABC' 进行比较,'ABC' 正在与所有ABCSite 在其父级的索引 [1] 处。所以我尝试了 $ABCCustomerHierarchy(//test:ABCSite[1])/test:Customer/test:CustomerID/test:Cate‌ ​g‌​ory 并得到了异常:javax.xml.transform.TransformerException: Extra非法令牌:跨度>
【解决方案2】:

您应该始终指定命名空间(测试您的示例)

还有一点是数组以1而不是0开头

如果要获取该节点的值,使用 text():

//test:ABCSite[1])/test:Customer/test:CustomerID/test:Category/text()

使用精确值匹配:

//test:ABCSite[1])/test:Customer/test:CustomerID/test:Category[text()='ABC_Customer']

【讨论】:

  • 我只尝试使用命名空间,问题是如果我像 //ABCCustomerHierarchy/ABCSite[1]/Customer/CustomerID/Category = 'ABC' 进行比较,'ABC' 正在与所有ABCSite 在其父级的索引 [1] 处。所以我尝试了 $ABCCustomerHierarchy(//test:ABCSite[1])/test:Customer/test:CustomerID/test:Categ‌ ory 并得到了异常:javax.xml.transform.TransformerException: Extra非法令牌:
  • 然后将 [] 之前的所有内容放入括号中。我编辑了我的回复
  • 不用担心。您应该将答案标记为有助于将来帮助他人
  • 谢谢evhen14。现在没有错误。但是 (//test:ABCCustomerHierarchy/test:ABCSite[1])/test:Customer/test:CustomerID/test:Category = 'XYZ_Customer' 返回 'true' 即使 ABCSite[1] 将一些其他文本保存为 Category 。似乎仍在检查整个文档。
  • 因此,如果 CustomerReferences 中的类别与 XYZ_Customer 不同,您希望它返回 false。换句话说,您希望所有类别(在 CustomerID 和 CustomerReferences 中)都有 XYZ_Customer?
猜你喜欢
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-03
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
相关资源
最近更新 更多