转换 xml 使用

Transform the xml using

我有一个 xml 如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<entry key="user">1234</entry>
<entry key="name">sam</entry>
</properties>

我想使用 xslt 将键值(key="user" 到 key="cm:user")转换为一个新的 xml 文件,输出 xml 应该是像这样

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<entry key="cm:user">1234</entry>
<entry key="name">sam</entry>
</properties>

我正在使用下面的 xslt 和 saxon jar:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://schema.infor.com/InforOAGIS/2">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />   
<xsl:template match="@*|node()">
<xsl:result-document href="foo.xml" method="xml">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:result-document>
</xsl:template>

<xsl:template match="@key[.='user']">
<xsl:attribute name="key">
        <xsl:value-of select="'cm:user'"/>
    </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

当我 运行 它时,我收到以下错误:
XTDE1490:无法将多个结果文档写入同一 URI:
有人可以帮我解决这个问题吗..

你只需要

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:template match="@key[.='user']">
<xsl:attribute name="key">
        <xsl:value-of select="'cm:user'"/>
    </xsl:attribute>
 </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

如果要使用 xsl:result-document 定义结果文件名,请添加模板

<xsl:template match="/">
  <xsl:result-document href="foo.xml">
    <xsl:apply-templates/>
  </xsl:result-document>
</xsl:template>