需要替换命名空间

时间:2016-03-03 01:34:42

标签: xml xslt namespaces

我需要替换输出中生成的错误命名空间。但是,当我将XML输出转换为XSLTFile时,根元素中生成的命名空间现在是正确的。但旧的命名空间出现在其他元素中。 提前感谢您的帮助。

INPUT FILE:

<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文件:

<?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>

元素生成一个额外的命名空间,这是错误的。

预期输出

<?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 个答案:

答案 0 :(得分: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>