2017-01-23 35 views
0

这是我第一次使用.xsl,所以应该是一个简单的问题。用XSL转换XML会产生纯文本输出

我有这样的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文档

+0

搜索“XSLT默认命名空间”,您会发现574个对这个问题的答案。 –

回答

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的其余部分,为了匹配元素,使用相同的前缀。