如何从xslt中具有冒号的属性中选择值?

如何从xslt中具有冒号的属性中选择值?,xslt,xml-parsing,xslt-1.0,Xslt,Xml Parsing,Xslt 1.0,我正在使用xslt处理从web服务返回的结果。我首先需要确定结果是针对哪个web服务的。我知道标记platformCore:record具有属性“xsi:type=”listRel:Contact或“xsi:type=”listEmp:Employee”。我正在尝试选择该属性存储的值,但在尝试选择该值时冒号似乎导致了一些问题 这是我尝试过的,但失败了 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl

我正在使用xslt处理从web服务返回的结果。我首先需要确定结果是针对哪个web服务的。我知道标记platformCore:record具有属性“xsi:type=”listRel:Contact或“xsi:type=”listEmp:Employee”。我正在尝试选择该属性存储的值,但在尝试选择该值时冒号似乎导致了一些问题

这是我尝试过的,但失败了

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">

<xsl:variable name="Type"><xsl:value-of select="//*[local-name()='searchResponse']//*[local-name()='searchResult']//*[local-name()='recordList']//*[local-name()='record']@xsi:type"/></xsl:variable>

<root>
<test><xsl:value-of select="$Type"/></test>
</root>
</xsl:template>
</xsl:stylesheet>

这是一个简单的例子

<?xml version="1.0" encoding="UTF-8"?>
<searchResponse:searchResponse xmlns="urn:messages_2012_2.platform.webservices.itsthesuite.com" 
                               xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                               xmlns:searchResponse="urn:messages_2012_2.platform.webservices.itsthesuite.com"
                               xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <platformCore:searchResult xmlns:platformCore="urn:core_2012_2.platform.webservices.itsthesuite.com">
    <platformCore:status isSuccess="true"/>
    <platformCore:totalRecords>1</platformCore:totalRecords>
    <platformCore:recordList>
      <platformCore:record internalId="154098" xsi:type="listRel:Contact" xmlns:listRel="urn:relationships_2012_2.lists.webservices.itsthesuite.com">
        <listRel:entityId>John Smith</listRel:entityId>
        <listRel:firstName>John</listRel:firstName>
        <listRel:lastName>Smith</listRel:lastName>
        <listRel:phone>(777) 777-7777</listRel:phone>
        <listRel:email>john.smith@yormoms.com</listRel:email>
      </platformCore:record>
    </platformCore:recordList>
  </platformCore:searchResult>
</searchResponse:searchResponse>

1.
约翰·史密斯
约翰
史密斯
(777) 777-7777
厕所。smith@yormoms.com
我需要的解决方案,以工作为这个样本以及

员工样本

        <?xml version="1.0" encoding="UTF-8"?>
<searchResponse xmlns="urn:messages_2012_2.platform.webservices.netsuite.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:searchResponse="urn:messages_2012_2.platform.webservices.netsuite.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<platformCore:searchResult xmlns:platformCore="urn:core_2012_2.platform.webservices.netsuite.com" >
<platformCore:status isSuccess="true"/>
<platformCore:totalRecords>1</platformCore:totalRecords>
<platformCore:recordList>
<platformCore:record internalId="158778" xsi:type="listEmp:Employee" xmlns:listEmp="urn:employees_2012_2.lists.webservices.netsuite.com">
<listEmp:entityId>331sfds Dipo  Chaponda</listEmp:entityId>
<listEmp:salutation>Mr.</listEmp:salutation>
<listEmp:firstName>Dipo</listEmp:firstName>
<listEmp:lastName>Chaponda</listEmp:lastName>
<listEmp:email>dchapond@youmm.com</listEmp:email>
</platformCore:record>
</platformCore:recordList>
</platformCore:searchResult>
</searchResponse>

1.
331sfds迪波查彭达酒店
先生
迪波
查彭达
dchapond@youmm.com

您可以选择使用本地名称的属性,类似于您正在执行的操作,但在*前面加@:

@*[local-name() = 'type']
但是,在XPath中乱扔
local-name()=
和双斜杠不是一种好的做法。您应该正确使用名称空间,并在已知的情况下使用精确的路径,尽管这似乎不是您案例中的元素的选项,因为在这两个示例中,它们使用的名称空间不同。这应该有效:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                exclude-result-prefixes="sr pc xsi"
                >

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">

    <xsl:variable name="Type">
      <xsl:value-of select="*[local-name() = 'searchResponse']/
                            *[local-name() = 'searchResult']/
                            *[local-name() = 'recordList']/
                            *[local-name() = 'record']/
                            @xsi:type"/>
    </xsl:variable>

    <root>
      <test>
        <xsl:value-of select="$Type"/>
      </test>
    </root>
  </xsl:template>
</xsl:stylesheet>

在样例输入上运行时,将生成预期结果:

<root>
  <test>listRel:Contact</test>
</root>

listRel:联系人

冒号之前的内容称为“名称空间”。此外,“冒号似乎导致了一些问题”还不足以描述问题。请更具体一些。此外,属性
xsi:type
不包含任何内容的值。它描述了标记的数据类型。什么是排除结果前缀=“sr pc xsi”“在您的解决方案中做什么?它显示了从输出到结果文档的这三个前缀的名称空间。是否可以使您的解决方案同时适用于第一个示例和此示例?”?我试过了,但似乎不起作用。你说的第一个样品和这个样品是什么意思?只有一个样本,我刚刚发布了第二个。