XPath查询local-name() - 根据其他标记的值获取标记

时间:2014-02-21 07:42:37

标签: xpath

我有以下xml格式。我需要获取'typename1'并且我写了// * [local-name()='type-name'和text()='typeName1']。如果使用XPath Query表达式'type-name'为'typeName1',有人可以帮助我如何获取'objectid1'。

    <?xml-stylesheet type="text/xsl" href="/API/xsl/abcde.xml" ?>
    <name xmlns="http://www.ccc.org/2011/SSS" xmlns:hostmgmt='http://www.xyzw.com/abcd'>
       <title type="text">test</title>   
       <content>
          <id>48035</id>      
          <host-contacts>
             <host-contact>
                <host-contact-type>Employee</host-contact-type>
                <host-contact-role>Role</host-contact-role>
                <host-contact-name>Employee Name</host-contact-name>
             </host-contact>
          </host-contacts>
          <syslog-messages>N</syslog-messages>      
          <services>
             <service>            
                <objectid>objectid1</objectid>
                <type-name>typeName1</type-name>
             </service>
             <service>            
                <objectid>objectid2</objectid>
                <type-name>typeName2</type-name>
             </service>
          </services>
          <building>ABC</building>
          <asset>XYZ</asset>
       </content>
    </name>

3 个答案:

答案 0 :(得分:1)

为什么不直接声明命名空间并使用普通轴步骤而不是local-name() hacks(忽略名称空间)?在不同的编程语言中声明命名空间是不同的,但是对于大多数命名空间而言,它们非常简单直接。

//sss:service[sss:type-name='typeName1']/sss:objectid

答案 1 :(得分:0)

尝试以下xpath

//*[local-name()='service'][child::*[local-name()='type-name'][.='typeName1']]/*[local-name()='objectid']/text()

它说找到一个节点(本地名称为service)(子项的本地名称为type-name,其值等于“typeName1”)具有本地名称的子内容objectid

答案 2 :(得分:0)

试试这个.. //服务[类型名称= “typeName1”] / OBJECTID 另一个例子可以 // * [local-name()='type-name'和text()='typeName1'] / objectid