2017-01-23 79 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的其餘部分,爲了匹配元素,使用相同的前綴。