2012-07-01 35 views
2

我有以下XML。我能夠刪除所有名稱空間,但無法使用XSL刪除xsi:type。使用xslt刪除xsi:type

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<StudentResult xmlns='http://ns.xyz.org/2004-08-02' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:ns1='http://ns.xyz.org/2004-08-02' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:type='ns1:StudentResult'> 
<StudentId idOwner='xyz'><IdValue name='ClientId'>9103-XML</IdValue></StudentId> 
<ClientOrderId idOwner='Cloud'><IdValue name='OrderNumber'>272454</IdValue></ClientOrderId> 
<Results>false</Results> 
</StudentResult> 

所需的輸出:

<?xml version="1.0" encoding="utf-8"?> 
<StudentResult> 
<StudentId idOwner="xyz"><IdValue name="ClientId">9103-XML</IdValue></StudentId> 
<ClientOrderId idOwner="Cloud"><IdValue name="OrderNumber">272454</IdValue></ClientOrderId> 
<Results>false</Results> 
</StudentResult> 

這是我用XSLT,但它並沒有幫助。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output indent="yes" method="xml" encoding="utf-8" /> 
<xsl:template match="/|comment()|processing-instruction()"> 
<xsl:copy> 
<xsl:apply-templates/> 
</xsl:copy> 
</xsl:template> 
<xsl:template match="*"> 
<xsl:element name="{local-name()}"> 
<xsl:apply-templates select="@*|node()"/> 
</xsl:element> 
</xsl:template> 
<xsl:template match="@*"> 
<xsl:attribute name="{local-name()}"> 
<xsl:value-of select="."/> 
</xsl:attribute> 
</xsl:template> 
<xsl:template match="/">  
<xsl:apply-templates select="@*|node()|processing-instruction()"/> 
</xsl:template> 
</xsl:stylesheet> 
+0

您的xslt可以正常運行xsltproc。你用什麼來運行它? –

回答

2

添加樣式表的根元素的模板

<xsl:template match="@xsi:type"/> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    exclude-result-prefixes="xsi"> 

+0

非常感謝Martin。有效! –

0

這種轉變

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()[not(self::*)]"> 
    <xsl:copy/> 
</xsl:template> 

<xsl:template match="*"> 
    <xsl:element name="{local-name()}"> 
    <xsl:apply-templates select="@*[not(name()='xsi:type')]|node()"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="@*"> 
    <xsl:attribute name="{local-name()}"> 
    <xsl:value-of select="."/> 
    </xsl:attribute> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<StudentResult 
xmlns='http://ns.xyz.org/2004-08-02' 
xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
xmlns:ns1='http://ns.xyz.org/2004-08-02' 
xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
xsi:type='ns1:StudentResult'> 
    <StudentId idOwner='xyz'> 
     <IdValue name='ClientId'>9103-XML</IdValue> 
    </StudentId> 
    <ClientOrderId idOwner='Cloud'> 
     <IdValue name='OrderNumber'>272454</IdValue> 
    </ClientOrderId> 
    <Results>false</Results> 
</StudentResult> 

產生想要的,正確的結果:

<StudentResult> 
    <StudentId idOwner="xyz"> 
     <IdValue name="ClientId">9103-XML</IdValue> 
    </StudentId> 
    <ClientOrderId idOwner="Cloud"> 
     <IdValue name="OrderNumber">272454</IdValue> 
    </ClientOrderId> 
    <Results>false</Results> 
</StudentResult>