当数据元素具有属性** xmlns =“...”时,无法使用XPath进行查询**

时间:2013-05-03 18:08:45

标签: xml xslt xpath xml-namespaces

当属性 xmlns =“...”并且它是parent元素时,我很难查询数据元素值的值。 以下示例是SOAP响应的一部分,我想通过使用XPATH / PartyInq_v2Response / PartyInq_v2Rs_Type / * [local-name()=“person”] / firstName来获取它的名字和姓氏的值'。但它什么也没有回报。如果我全部删除它,它可以返回值 查询前xml中的 xmlns =“...”。有人知道如何直接从示例中查询名字吗?

<PartyInq_v2Response xmlns="urn:Somewhere.Int" xmlns:q2="http://SomewhereOps.v20120719" xmlns:q10="http://SomewhereTypes.v20120719.GenericTypes">
    <PartyInq_v2Rs_Type>
        <q2:person>
            <firstName xmlns="http://SomewhereTypes.v20120719.Types">somebody</firstName>
            <lastName xmlns="http://SomewhereTypes.v20120719.Types">nobody</lastName>           
        </q2:person>
    </PartyInq_v2Rs_Type>
</PartyInq_v2Response>

由于

1 个答案:

答案 0 :(得分:1)

目前尚不清楚您使用的xslt处理器。但是你必须使xlst知道所有使用过的命名空间。

以下xlst将执行:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:s="urn:Somewhere.Int" 
            xmlns:q2="http://SomewhereOps.v20120719"
            xmlns:q10="http://SomewhereTypes.v20120719.GenericTypes"
            xmlns:t="http://SomewhereTypes.v20120719.Types">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/" >
        <xsl:value-of select="/s:PartyInq_v2Response/s:PartyInq_v2Rs_Type/q2:person/t:firstName"/>
    </xsl:template>
</xsl:stylesheet>

如果未知名称空间URL,则可以使用local-name()。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
             xmlns:s="urn:Somewhere.Int" >
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/" >
        <xsl:value-of select="/s:PartyInq_v2Response/*[local-name() = 'PartyInq_v2Rs_Type']/*[local-name() = 'person']/*[local-name()='firstName']"/>
    </xsl:template>
</xsl:stylesheet>