使用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
schemaLocationas a namespace node, butxsi:schemaLocationis an attribute, not a namespace. Thenamespace::schemaLocationselector 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
- Namespace Binding: We define a
docprefix for the target namespace so we can accurately match the<Document>element (since it lives in that namespace). - Identity Template: This handles copying all other nodes and attributes unchanged, so your XML's content stays intact.
- Root Element Template:
- We explicitly recreate the
<Document>element with the default namespace (no prefix), which removes anyns0prefix if it existed. - We add the
xmlns:xsiattribute to declare the XSI namespace. - We add the
xsi:schemaLocationattribute with your specified schema values. - We then copy any existing attributes and child nodes from the original element.
- We explicitly recreate the
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
相关产品推荐
相关产品推荐

