You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用XSLT为现有命名空间添加额外命名空间并移除ns0前缀求助

Solution to Modify XML Document with xmlns:xsi and schemaLocation

Let's break down what's wrong with your current XSLT and fix it to get your desired output:

Issues with Your Original XSLT

  • You tried to copy schemaLocation as a namespace node, but xsi:schemaLocation is an attribute, not a namespace. The namespace::schemaLocation selector doesn't exist, so that line does nothing.
  • Your root element template didn't explicitly add the required attributes; it only copied existing ones, which is why the new attributes never appeared.

Corrected XSLT Code

This solution will add the required namespace and schema location attributes, while preserving all other content and removing any unnecessary prefixes (like ns0 if present):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:doc="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  
  <!-- Identity template: copies all nodes/attributes by default (preserves existing content) -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <!-- Target the root Document element in the specific namespace -->
  <xsl:template match="doc:Document">
    <!-- Recreate the element with the default namespace (no prefix) -->
    <Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02">
      <!-- Add the xmlns:xsi namespace attribute -->
      <xsl:attribute name="xmlns:xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>
      <!-- Add the xsi:schemaLocation attribute with your desired values -->
      <xsl:attribute name="xsi:schemaLocation">urn:iso:std:iso:20022:tech:xsd:camt.053.001.02 camt.053.001.02.xsd</xsl:attribute>
      <!-- Copy all existing attributes from the original element -->
      <xsl:apply-templates select="@*"/>
      <!-- Copy all child nodes from the original element -->
      <xsl:apply-templates select="node()"/>
    </Document>
  </xsl:template>
</xsl:stylesheet>

How This Works

  1. Namespace Binding: We define a doc prefix for the target namespace so we can accurately match the <Document> element (since it lives in that namespace).
  2. Identity Template: This handles copying all other nodes and attributes unchanged, so your XML's content stays intact.
  3. Root Element Template:
    • We explicitly recreate the <Document> element with the default namespace (no prefix), which removes any ns0 prefix if it existed.
    • We add the xmlns:xsi attribute to declare the XSI namespace.
    • We add the xsi:schemaLocation attribute with your specified schema values.
    • We then copy any existing attributes and child nodes from the original element.

Resulting Output

When you run this XSLT against your input XML, you'll get exactly the output you want:

<?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">
  <!-- All child nodes from your input will be preserved here -->
</Document>

内容的提问来源于stack exchange,提问作者swapnashree

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:37:43