xml - 使用 XSL 根据 XML 的条件添加命名空间
问题描述
我正在尝试根据条件将名称空间添加到 xml。但条件不起作用。有人可以帮忙吗。
输入 XML:
<n0:MainTag xmlns:n0='http://abc123.com' xmlns:prx='urn:testing.com' xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/'>
<header>M</header>
<Data>
<Child>623471568753</Child>
</Data>
</n0:MainTag>
输出 XML:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:MainTag xmlns:ns0="http://xyz987.com">
<header>M</header>
<Data>
<Child>623471568753</Child>
</Data>
</n0:MainTag>
第二个输入:输入 XML:
<n0:DifferentTag xmlns:n0='http://abc123.com' xmlns:prx='urn:testing.com' xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/'>
<header>M</header>
<Datum>
<Child>Test123</Child>
</Datum>
</n0:DifferentTag>
输出 XML:
<n0:DifferentTag xmlns:ns0="http://QWR.com">
<header>M</header>
<Datum>
<Child>Test123</Child>
</Datum>
</n0:DifferentTag>
XSL 试过:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://xyz987.com">
<xsl:output encoding='UTF-8' indent='yes' method='xml'/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="MainTag">
<xsl:element name="ns0:{local-name()}" namespace="http://xyz987.com">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="MainTag">
<xsl:element name="ns0:{local-name()}" namespace="http://QWR.com">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="*/*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
条件:检查源 XML 中的标签名称
解决方案
问题在于,在您的输入 XML 中,两者MainTag
都DifferentTag
在“ http://abc123.com ”中,但是您在 XSLT 中考虑到了这一点,因此它试图匹配没有命名空间中的标签。
您需要在 XSLT 中声明前缀,并在匹配中使用它。
另请注意,您当前的 XSLT 有两个模板匹配MainTag
,而其中一个可能应该匹配DifferentTag
。
试试这个 XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:n0="http://abc123.com"
exclude-result-prefixes="n0">
<xsl:output encoding='UTF-8' indent='yes' method='xml'/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="n0:MainTag">
<xsl:element name="ns0:{local-name()}" namespace="http://xyz987.com">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="n0:DifferentTag">
<xsl:element name="ns0:{local-name()}" namespace="http://QWR.com">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="*/*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
编辑:如果您真的不知道输入 XML 中的命名空间,请尝试使用此 XSLT...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding='UTF-8' indent='yes' method='xml'/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[local-name() = 'MainTag']">
<xsl:element name="ns0:{local-name()}" namespace="http://xyz987.com">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="*[local-name() = 'DifferentTag']">
<xsl:element name="ns0:{local-name()}" namespace="http://QWR.com">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="*/*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- spring-boot - 在 Websphere 8.5.5.16 上部署 Spring boot 2.2.1 解决方案时遇到问题
- liquibase - CockroachDB 中 dropIndex 的 CASCADE
- swift - 源文件中的编辑器占位符。谁能帮我修复这个错误?(我已附上图片,需要创建相同的应用程序)
- google-sheets - 为什么命名范围对应的sheetId为null,而实际sheet的sheetId属性为0?
- python - 如何从 cron 作业开始并行运行相同的 python 脚本?
- c++builder - 从 Vcl.Controls.TCustomControl 继承的自定义控件无权访问 SetPopupMenu
- python - 有没有一种通过蓝牙(低功耗)触发某些动作的好方法?
- java - 基于JAVA中的字符串输入调用各种类中存在的相同函数
- python - Flask 重定向(相同的路由,不同的 HTTP 方法)以 302 循环结束
- hibernate - SpringBoot 事务隔离级别