xslt使用命名空间重命名标记

问题描述 投票:1回答:1

我正在尝试重命名父xml标记。我在stackoverflow中查找它,我看到以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Response_123">
<Response>
  <xsl:apply-templates select="@*|node()"/>
</Response>
</xsl:template>
</xsl:stylesheet>

遗憾的是,该代码不符合我的要求。在下面的示例中,标记与命名空间相关联。

<ns1:Response_123 xmlns:ns1="AAA">
<System>Alpha</System>
</ns1:Response_123>

当我使用上面的代码时,我仍然得到相同的消息(没有任何区别)。如何将输出更改为:

<ns1:Response xmlns:ns1="AAA">
<System>Alpha</System>
</ns1:Response>

我也尝试在代码中添加命名空间,它也不起作用。

<xsl:template match="Response_123" namespace="AAA">
<Response namespace="AAA">
  <xsl:apply-templates select="@*|node()"/>
</Response>
</xsl:template>

感谢大家!

xml xslt xslt-1.0
1个回答
0
投票

在XSLT中添加命名空间,并使用命名空间调用模板

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns1="AAA"
    version="1.0">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="ns1:Response_123">
        <Response>
            <xsl:apply-templates select="@*|node()"/>
        </Response>
    </xsl:template>
</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.