使用转换xml

时间:2017-04-08 04:14:46

标签: xml xslt xslt-2.0 saxon

我有一个像下面这样的xml:

looping = True
while looping:
    userInput = input("Press X to leave the loop")
    if userInput == "X":
        looping = False
print("Left the while loop")

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

class Animal:
    def __init__(self):
        print("Creating animal")

    def make_sound(self, sound):
        print("Making a sound...")
        print(sound)

# Create an instance of the Animal class.
animal = Animal()
# Call a method. The self parameter implicitly refers to the instance, animal.
animal.make_sound("meow")

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

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

当我运行它时,我收到以下错误:
XTDE1490:不能将多个结果文档写入同一个URI:
有人可以帮我这个..

1 个答案:

答案 0 :(得分:3)

你只需要

<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>
相关问题