首页 > 解决方案 > 使用 XSLT 向现有命名空间添加额外的命名空间

问题描述

我有一个要求得到输出:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02 camt.053.001.02.xsd">

输入:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02">

我已经尝试过代码,但它不起作用。

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02 camt.053.001.02.xsd"
 xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02">
<xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:template match="/*">
  <xsl:copy>
   <xsl:copy-of select="document('')/*/namespace::xsi"/>
    <xsl:copy-of select="document('')/*/namespace::schemaLocation"/>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
 </xsl:copy>
</xsl:template>
<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

我还需要删除前缀 ns0:。请帮忙。

标签: xmlxslt

解决方案


您是否有理由不能简单地做:

XSLT 1.0

<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:strip-space elements="*"/>

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

<xsl:template match="/*">
    <Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02 camt.053.001.02.xsd">
        <xsl:apply-templates select="@*|node()"/>
    </Document>
</xsl:template>

</xsl:stylesheet>

推荐阅读