【发布时间】:2019-03-04 05:14:26
【问题描述】:
我正在尝试根据条件将命名空间添加到 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 中的标签名称
【问题讨论】:
-
别忘了加上 XSL 试过了!另外,你能解释一下“条件”是什么吗?谢谢!
-
我已尝试添加 XSLT 代码,但未获得所需的输出。我们也期望不同的 xmls(超过 2 个),所以我只给出了两个输入 xmls。
标签: xml xslt namespaces