使用XSL转换XML可生成纯文本输出

时间:2017-01-23 05:28:29

标签: xml xslt maximo

这是我第一次使用.xsl,所以应该是一个简单的问题。

我有这个xml

<?xml version="1.0" encoding="UTF-8"?>
<PublishTESTWS1ASSET xmlns="http://www.ibm.com/maximo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2017-01-22T20:07:28-08:00" transLanguage="EN" baseLanguage="EN" messageID="3649776.1485144448726917270" maximoVersion="7 6 20141117-2230 V7600-218" event="0">
   <TESTWS1ASSETSet>
      <ASSET>
         <ASSETID>52</ASSETID>
         <ASSETNUM>1001</ASSETNUM>
         <DESCRIPTION>Fire Extinguisher</DESCRIPTION>
      </ASSET>
   </TESTWS1ASSETSet>
</PublishTESTWS1ASSET>

我有这个.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tes="http://testws1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
   <xsl:template match="PublishTESTWS1ASSET/TESTWS1ASSETSet">
      <xsl:apply-templates select="ASSET"/>
   </xsl:template>
   <xsl:template match="ASSET">
         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tes="http://testws1/">
         <soapenv:Header/>
         <soapenv:Body>
            <tes:addAsset>
                <name><xsl:value-of select="DESCRIPTION"/></name>
               <number><xsl:value-of select="ASSETID"/></number>
            </tes:addAsset>
       </soapenv:Body>
      </soapenv:Envelope>
   </xsl:template>
</xsl:stylesheet>

当它被转换时,它以纯文本

输出以下内容
     52
     1001
     Fire Extinguisher

我已将其缩小到PublishTESTWS1ASSET

中的属性
xmlns="http://www.ibm.com/maximo"

删除它会产生正确的输出

  <soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:tes="http://testws1/"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header/>
   <soapenv:Body>
      <tes:addAsset>
         <name>Fire Extinguisher</name>
         <number>52</number>
      </tes:addAsset>
   </soapenv:Body>
</soapenv:Envelope>

有人可以向我解释为什么会发生这种情况以及如何在不更改原始xml文档的情况下修复它

1 个答案:

答案 0 :(得分:1)

您没有匹配XSLT中的命名空间;如果没有这个,你编写的模板将与输入XML的元素不匹配。您获得输出的原因是有内置模板来处理您的输入XML。

您需要在XSLT中声明命名空间(以及前缀,可选)并使用它们来匹配元素。 请参考以下代码:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tes="http://testws1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="http://www.ibm.com/maximo">
   <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
   <xsl:template match="ns:PublishTESTWS1ASSET/ns:TESTWS1ASSETSet">
      <xsl:apply-templates select="ns:ASSET"/>
   </xsl:template>
   <xsl:template match="ns:ASSET">
         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tes="http://testws1/">
         <soapenv:Header/>
         <soapenv:Body>
            <tes:addAsset>
                <name><xsl:value-of select="ns:DESCRIPTION"/></name>
               <number><xsl:value-of select="ns:ASSETID"/></number>
            </tes:addAsset>
       </soapenv:Body>
      </soapenv:Envelope>
   </xsl:template>
</xsl:stylesheet>

在上面的XSLT中,名称空间http://www.ibm.com/maximo使用前缀ns声明。在其余的XPATH中,为了匹配元素,使用相同的前缀。

相关问题