2016-03-03 94 views
1

我需要替換輸出中生成的錯誤名稱空間。但是,當我將XML輸出轉換爲XSLTFile時,根元素中生成的名稱空間現在是正確的。但是舊的命名空間出現在其他元素中。 非常感謝您的幫助。需要替換名稱空間

輸入文件:

<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
     xmlns="http://uri.etsi.org/01903/v1.4.1#" 
     xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"> 
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID> 
    <cbc:CustomizationID>TR1.2</cbc:CustomizationID> 
    <cbc:ProfileID>TEMELFATURA</cbc:ProfileID> 
    <cac:InvoicePeriod> 
    <cbc:StartDate>2016-02-04</cbc:StartDate> 
    <cbc:StartTime>06:00:00</cbc:StartTime> 
    <cbc:DurationMeasure unitCode="DAY">15</cbc:DurationMeasure> 
    </cac:InvoicePeriod> 
</Invoice> 

XSLT FILE:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 


<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
     <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/*[local-name()='Invoice']"> 
     <Invoice 
      xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
      xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
      xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../xsdrt/maindoc/UBL-Invoice-2.1.xsd"> 
      <xsl:apply-templates select="@* | node()"/> 
     </Invoice> 
    </xsl:template> 
</xsl:stylesheet> 

產生的輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
     xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
     xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../xsdrt/maindoc/UBL-Invoice-2.1.xsd"> 
    <cbc:UBLVersionID xmlns="http://uri.etsi.org/01903/v1.4.1#">2.1</cbc:UBLVersionID> 
    <cbc:CustomizationID xmlns="http://uri.etsi.org/01903/v1.4.1#">TR1.2</cbc:CustomizationID> 
    <cbc:ProfileID xmlns="http://uri.etsi.org/01903/v1.4.1#">TEMELFATURA</cbc:ProfileID> 
    <cac:InvoicePeriod xmlns="http://uri.etsi.org/01903/v1.4.1#"> 
    <cbc:StartDate>2016-02-04</cbc:StartDate> 
    <cbc:StartTime>06:00:00</cbc:StartTime> 
    <cbc:DurationMeasure unitCode="DAY">15</cbc:DurationMeasure> 
    </cac:InvoicePeriod> 
</Invoice> 

元素GE nerate一個額外的命名空間,這是錯誤的。

預期輸出

<?xml version="1.0" encoding="UTF-8"?> 
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
     xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
     xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../xsdrt/maindoc/UBL-Invoice-2.1.xsd"> 
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID> 
    <cbc:CustomizationID>TR1.2</cbc:CustomizationID> 
    <cbc:ProfileID>TEMELFATURA</cbc:ProfileID> 
    <cac:InvoicePeriod> 
    <cbc:StartDate>2016-02-04</cbc:StartDate> 
    <cbc:StartTime>06:00:00</cbc:StartTime> 
    <cbc:DurationMeasure uniedtCode="DAY">15</cbc:DurationMeasure> 
    </cac:InvoicePeriod> 
</Invoice> 

不管怎麼說,我已經嘗試過在XSLT我在網上看到很多代碼。但它沒有奏效。謝謝。

回答

1

實際上應該沒有任何問題,你在那裏的輸出。它有額外的名稱空間聲明,但它們不會影響輸出中任何元素的名稱空間。

不過,如果你想忽略他們,你可以添加一個額外的xsl:template,再現所有的後代元素沒有複製所有範圍的命名空間對他們說:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

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

    <xsl:template match="/*[local-name()='Invoice']"> 
    <Invoice 
     xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
     xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
     xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../xsdrt/maindoc/UBL-Invoice-2.1.xsd"> 
     <xsl:apply-templates select="@* | node()"/> 
    </Invoice> 
    </xsl:template> 

    <!-- v------------- this template --> 
    <xsl:template match="*"> 
    <xsl:element name="{name()}" namespace="{namespace-uri()}"> 
     <xsl:apply-templates select="@* | node()" /> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

嗨@JLRishe,謝謝你的反饋意見。它已經在工作:) – hannah

相關問題