这个xml的xpath是什么

时间:2010-10-27 16:19:00

标签: xml xpath

您好我正在尝试在下面的xml中获取变量值(ref在contractRef中),但是当我使用我假设的xpath时:

/discovery/contractRef[@xmlns='http://schemas.xmlsoap.org/disco/scl/']/@ref

它什么都不返回。我怎样才能得到这个变量,我错过了什么?感谢

<?xml version="1.0" encoding="utf-8"?>
<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/">
  <contractRef ref="http://127.0.0.1/Services/Core/Calendar/LBCalendar.svc?wsdl" docRef="http://127.0.0.1/Services/Core/Calendar/LBCalendar.svc" xmlns="http://schemas.xmlsoap.org/disco/scl/" />
</discovery>

2 个答案:

答案 0 :(得分:3)

这是常见问题解答。元素<discovery>位于命名空间http://schemas.xmlsoap.org/disco/中,子元素<contractRef>位于命名空间http://schemas.xmlsoap.org/disco/scl/中您需要使用一些前缀注册这些命名空间,然后在您的名称空间中使用这些前缀XPath表达式。如何“注册”取决于使用XPath表达式的语言/环境。如果在XML文档中使用XPath,则仅通过声明带前缀的命名空间来“注册”前缀,例如xmlns:scl="http://schemas.xmlsoap.org/disco/scl/"

假设http://schemas.xmlsoap.org/disco/已注册到前缀d,而http://schemas.xmlsoap.org/disco/scl/已注册到前缀scl,那么正确的XPath表达式将

/d:discovery/scl:contractRef/@ref

命名空间是XML和XPath的基本功能。请花时间学习它们。避免在XPath表达式中使用名称空间前缀的方法是使用local-name()函数

/*[local-name() = 'discovery*]/*[local-name() = 'contractRef']/@ref

请注意,属性名称不需要名称空间前缀,因为它属于no-namespace。

答案 1 :(得分:0)

我实际上已将您的xml上传到http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm,我认为您的xpath正在运行。