首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要替换命名空间

需要替换命名空间
EN

Stack Overflow用户
提问于 2016-03-03 01:34:41
回答 1查看 596关注 0票数 1

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

输入文件:

代码语言:javascript
复制
<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文件:

代码语言:javascript
复制
<?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>

生成的输出:

代码语言:javascript
复制
<?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>

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

预期输出

代码语言:javascript
复制
<?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>

无论如何,我已经在web上看到过很多XSLT代码。但没起作用。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-03-03 04:30:24

实际上,您的输出不应该有任何问题。它有额外的名称空间声明,但它们不影响输出中任何元素的命名空间。

但是,如果您想省略它们,可以添加一个额外的xsl:template来重新创建所有的子代元素,而无需在它们上复制所有的作用域内命名空间:

代码语言:javascript
复制
<?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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35761509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档