给定XML的XSLT [重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

XML命名空间正在为我创建一个问题,我无法选择任何值。我相信这种情况正在发生,因为有些标签有命名空间,有些则没有。

你能帮我创建一个XSLT来获得所需的输出吗?

样本输入 -

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns3:NotificationResponse xmlns:ns3="http://www.test.com/ns/wsdl/test-V2_4" xmlns="http://www.test.com/ns/xsd/test-V2_1" xmlns:ns2="http://www.test.com/ns/xsd/test-V2_2">
         <Header>
            <appCode />
            <isRemNot>false</isRemNot>
            <identification>Test</identification>
            <msgDT>2018-05-01T16:29:12.937+02:00</msgDT>
         </Header>
         <Contract />
         <Notification>
            <ns2:notificationTypeCode>TestTypeCode</ns2:notificationTypeCode>
         </Notification>
      </ns3:NotificationResponse>
   </soap:Body>
</soap:Envelope>

样本输出 -

<Contract>
            <isRemNot>false</isRemNot>
        <identification>Test</identification>
        <msgDT>2018-05-01T16:29:12.937+02:00</msgDT>
            <notificationTypeCode>TestTypeCode</notificationTypeCode>
<Contract>
xml xslt
1个回答
0
投票

有些标签有命名空间,有些则没有。 - 没有前缀为名称空间前缀的输入XML元素与名称空间xmlns="http://www.test.com/ns/xsd/test-V2_1"相关联。为了获取数据,您必须映射XSLT中的所有命名空间并相应地访问元素。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns3="http://www.test.com/ns/wsdl/test-V2_4"
    xmlns:ns1="http://www.test.com/ns/xsd/test-V2_1"
    xmlns:ns2="http://www.test.com/ns/xsd/test-V2_2" 
    exclude-result-prefixes="soap ns1 ns2 ns3">

如果输出XML中不需要命名空间,则可以使用exclude-result-prefixes属性。

在这种情况下,命名空间"http://www.test.com/ns/xsd/test-V2_1"已经使用XSL中的前缀ns1和元素即识别。 <Header><isRemNot>等可以访问<ns1:Header><ns1:isRemNot>等。

以下是您可以使用的模板。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns3="http://www.test.com/ns/wsdl/test-V2_4"
    xmlns:ns1="http://www.test.com/ns/xsd/test-V2_1"
    xmlns:ns2="http://www.test.com/ns/xsd/test-V2_2" 
    exclude-result-prefixes="soap ns1 ns2 ns3">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="ns3:NotificationResponse">
        <Contract>
            <isRemNot><xsl:value-of select="ns1:Header/ns1:isRemNot" /></isRemNot>
            <identification><xsl:value-of select="ns1:Header/ns1:identification" /></identification>
            <msgDT><xsl:value-of select="ns1:Header/ns1:msgDT" /></msgDT>
            <notificationTypeCode><xsl:value-of select="ns1:Notification/ns2:notificationTypeCode" /></notificationTypeCode>
        </Contract>
    </xsl:template>
</xsl:stylesheet>

产量

<Contract>
    <isRemNot>false</isRemNot>
    <identification>Test</identification>
    <msgDT>2018-05-01T16:29:12.937+02:00</msgDT>
    <notificationTypeCode>TestTypeCode</notificationTypeCode>
</Contract>
© www.soinside.com 2019 - 2024. All rights reserved.