任何人都可以请帮助我进行以下转换吗?XSLT压扁XML
下面是输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author>
<name>Smith</name>
</author>
<author>
<name>Wallace</name>
</author>
<author>
<name>Brown</name>
</author>
</book>
<book>
<title>Other book</title>
<pages>100</pages>
<size>small</size>
<author>King</author>
</book>
<book>
<title>Pretty book</title>
<pages>150</pages>
<size>medium</size>
</book>
这是所需的输出
<book style="even">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Smith</author-name>
</book>
<book style="odd">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Wallace</author-name>
</book>
<book style="even">
<title>My book</title>
<pages>200</pages>
<size>big</size>
<author-name>Brown</author-name>
</book>
<book style="odd" >
<title>Other book</title>
<pages>100</pages>
<size>small</size>
<author-name>King</author-name>
</book>
<book style="even">
<title>Pretty book</title>
<pages>150</pages>
<size>medium</size>
<author-name />
</book>
我使用xsl:for-each
循环试过,但我想他们把我带到了一个死胡同。这里棘手的部分是无论在任何书籍中放置多少个作者标签,“样式”属性都需要成为“全局”。
嗯,不能得到这个工作。我得到以下错误:“与此样式表中另一个模板的模板冲突”。即使我进行改造,我也只能得到最后一本书... –
对不起,有一个多余的模板/我忘了删除,它现在被纠正。但奇怪的是,我用XSLT引擎成功尝试了它,并没有抱怨这一点。另外,我在输入文档中添加了一个根元素,这也可能是导致不同行为的原因。 –
我不知道为什么,但这只适用于Xalan处理器,不适用于包含在JRE中的标准处理器。 –