2017-04-24 62 views
0

嗨我無法遍歷我的輸入XML文件來獲得所需的輸出。請幫助。 所需的輸出:XSLT轉換

<DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport> 

我的XSLT文件:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rout="http://safe.com/RoutePlanner/" xmlns:dp="http://www.datapower.com/extensions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" extension-element-prefixes="dp" exclude-result-prefixes="dp"> 
    <xsl:output method="xml"/> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="/Envelope/Body/ABCCustomMsg/*"/> 
    </xsl:template> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

輸入XML:

<Envelope 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rout="http://safe.com/RoutePlanner/"> 

<Body> 
<rout:ABCCustomMsg 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
> 

<DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport></ns1:ABCCustomMsg> 
</Body> 
</Envelope> 
+2

您的輸入和輸出都不是格式良好的XML文檔。 –

回答

0

你需要在你的XPath使用命名空間前綴。這就是他們的東西:

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:rout="http://safe.com/RoutePlanner/" 
       xmlns:dp="http://www.datapower.com/extensions" 
       xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
       extension-element-prefixes="dp" exclude-result-prefixes="dp"> 
    <xsl:output method="xml"/> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/rout:ABCCustomMsg/*"/> 
    </xsl:template> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

這將產生輸出:

<DocFWImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rout="http://safe.com/RoutePlanner/"><Header senderID="ABC1234"/></Request></DocFWImport> 

如果你真的想刪除要複製的元素的命名空間(如您的示例輸出沒有命名空間) ,你可以這樣做是這樣的:

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:rout="http://safe.com/RoutePlanner/" 
       xmlns:dp="http://www.datapower.com/extensions" 
       xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
       extension-element-prefixes="dp" exclude-result-prefixes="dp"> 
    <xsl:output method="xml"/> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/rout:ABCCustomMsg/*"/> 
    </xsl:template> 

    <xsl:template match="@*|text()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*"> 
     <xsl:element name="{local-name()}"> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

這將產生,您顯示所需的輸出。