xml - 使用 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:。请帮忙。
解决方案
您是否有理由不能简单地做:
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>
推荐阅读
- python - 谁能解释一下这个 Python 3 命令的作用?
- java - 在 Gson 反序列化期间使用 Dagger 进行依赖注入
- .net - 在 .Net 核心中构建具有动态对象数组的表单
- html - 为什么 css 样式会这样?
- angular - 使用 Angular 6 进行 Cognito Facebook 社交登录:如何使用返回的代码在放大 Auth 中对用户进行身份验证
- java - 图片按钮 onclick 更改图片
- mdt - mdt 2013 如何一次为多个型号的 pc 部署单个操作系统
- oracle - 使用spring boot jpa在类路径资源中创建名称为“entityManagerFactory”的bean时出错
- excel - 如何在一系列单元格中显示活动目录的结果
- kubernetes - 使用 Google IAM 进行 GKE 服务网络访问