简体   繁体   English

在XML标头中添加命名空间

[英]Adding Namespace to header in XML

As per my initial requirement, I have written xslt code to remove namespace prefix from the code , but the namespace is also getting removed. 按照我的最初要求,我已经编写了xslt代码以从代码中删除名称空间前缀,但是名称空间也正在被删除。

Below is the input file , output file and xslt code. 以下是输入文件,输出文件和xslt代码。

input.xml input.xml

<?xml version="1.0" encoding="UTF-8"?>
<ns0:nfeProc xmlns:ns0="http://www.p.in.br/nf" versao="4.00">
<ns0:cUF>35</ns0:cUF>
<ns0:cNF>10131445</ns0:cNF>
</ns0:nfeProc>

transform.xsl transform.xsl

<?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" indent="yes"/>

  <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:stylesheet>

Output: 输出:

<?xml version="1.0" encoding="UTF-8"?>
<nfeProc versao="4.00">
<cUF>35</cUF>
<cNF>10131445</cNF>
</nfeProc>

I also want to append n0 as namespace prefix for just nfeProc element and two namespaces inside it. 我还想仅将nfeProc元素和其中的两个命名空间附加n0作为命名空间前缀。 Below is the desired output. 以下是所需的输出。

<?xml version="1.0" encoding="UTF-8"?>
<n0:nfeProc xmlns="http://www.p.in.br/nf" xmlns:n0="http://www.p.in.br/nf" versao="4.00">
<cUF>35</cUF>
<cNF>10131445</cNF>
</n0:nfeProc>

Please let me know what changes are required. 请让我知道需要什么更改。 Kindly help 请帮助

Here's an XSLT 1.0 option, but like others have said, it doesn't make a whole lot of sense and may not work with all processors... 这是一个XSLT 1.0选项,但是就像其他人所说的那样,它没有太多意义,并且可能不适用于所有处理器...

XML Input XML输入

<ns0:nfeProc xmlns:ns0="http://www.p.in.br/nf" versao="4.00">
<ns0:cUF>35</ns0:cUF>
<ns0:cNF>10131445</ns0:cNF>
</ns0:nfeProc>

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:n0="http://www.p.in.br/nf" xmlns="http://www.p.in.br/nf">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

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

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

</xsl:stylesheet>

XML Output XML输出

<n0:nfeProc versao="4.00" xmlns:n0="http://www.p.in.br/nf" xmlns="http://www.p.in.br/nf">
<cUF>35</cUF>
<cNF>10131445</cNF>
</n0:nfeProc>

Fiddle (thanks @tim-c): http://xsltfiddle.liberty-development.net/3NJ3915/3 小提琴(感谢@ tim-c): http : //xsltfiddle.liberty-development.net/3NJ3915/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM