Xpath 1.0表达式获取任何请求的值

时间:2015-06-03 17:14:53

标签: xml xpath

我不确定这是否可行?但我想知道我们是否可以使用通用Xpath表达式来获取任何请求的documentIdentifer值。

请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:cidx:names:specification:ces:schema:all:5:0">
   <soapenv:Header/>
   <soapenv:Body>
      <Invoice Version="3.0">
         <Header>
            <ThisDocumentIdentifier>
               <DocumentIdentifier>0000001193567128</DocumentIdentifier>
            </ThisDocumentIdentifier>
            <ThisDocumentDateTime>
               <DateTime DateTimeQualifier="On">20150520T145204Z</DateTime>
            </ThisDocumentDateTime>
            </Header>
      </Invoice>
   </soapenv:Body>
</soapenv:Envelope>

** Xpath **

/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='Invoice']/*[local-name()='Header']/*[local-name()='ThisDocumentIdentifier']/*[local-name()='DocumentIdentifier']

我能够得到这个值。

我的问题是root元素一直在变化,但格式保持不变。我只需要<DocumentIdentifier>0000001193567128</DocumentIdentifier>

如果根元素是<OrderCreate>而不是使用 这个Xpath

/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='OrderCreate']/*[local-name()='Header']/*[local-name()='ThisDocumentIdentifier']/*[local-name()='DocumentIdentifier']

还有其他方法可以实现这个价值吗?

1 个答案:

答案 0 :(得分:2)

  

我需要得到select * from round r, round_hole rh, player p, course_nine c, course_hole ch where r.r_id = rh.rh_rid and p.id = r.r_pid and c.cn_nine = r.r_nine and ch.ch_nine = c.cn_nine and rh.rh_hid = ch.ch_no

那将是<DocumentIdentifier>0000001193567128</DocumentIdentifier>,简单明了。

您不需要在XPath中过度专用。

由于您使用XSLT,请声明输入文档中出现的命名空间,并避免使用//DocumentIdentifier。像你一样使用local-name()(去除#34;摆脱命名空间&#34;)几乎总是有资格作为一个bug。声明命名空间并在XPath中使用它们,这样可以减少输入,减少错误,并且更容易在眼睛上使用。

请注意,只要您使用相同的命名空间URI,就不需要使用与输入中相同的前缀。在以下XSL片段中,我使用local-name()而不是se作为SOAP Envelope命名空间。

soapenv
相关问题