【发布时间】:2011-08-31 15:07:06
【问题描述】:
我有两个文件。数据中有一些重叠,但我想从 file2 中提取特定信息并添加到 file1。
File1.xml 如下所示:
<content>
<book>
<id>111aaa</id>
<author>John Doe</author>
<title>This is a book</title>
<price>$10.00</price>
</book>
<book>
<id>111bbb</id>
<author>Jane Doe</author>
<title>This is another book</title>
<price>$20.00</price>
</book>
</content>
File2.xml 如下所示:
<content>
<book>
<id>111aaa</id>
<author>John Doe</author>
<year>2011</year>
<pages>100</pages>
</book>
<book>
<id>111bbb</id>
<author>Jane Doe</author>
<year>2010</year>
<pages>200</pages>
</book>
</content>
我想从 file2 中提取 year 和 pages 标签并将其添加到 file1 并输出一个如下所示的 file3.xml 文件:
<content>
<book>
<id>111aaa</id>
<author>John Doe</author>
<title>This is a book</title>
<year>2011</year>
<pages>100</pages>
<price>$10.00</price>
</book>
<book>
<id>111bbb</id>
<author>Jane Doe</author>
<title>This is a another book</title>
<year>2010</year>
<pages>200</pages>
<price>$20.00</price>
</book>
</content>
我正在使用命令行运行 xsltproc:
xsltproc transform.xsl file1.xml > file3.xml
我的 xsl 中有以下块,但它只是结合了第一本书的数据。你知道如何编写 xsl 来浏览每本书吗?
<xsl:choose>
<xsl:when test="document('file2.xml')/content/book/id = id">
<xsl:for-each select="document('file2.xml')/content/book">
<xsl:element name="pages">
<xsl:value-of select="document('file2.xml')/content/book/pages"/>
</xsl:element>
<xsl:element name="year">
<xsl:value-of select="document('file2.xml')/content/book/year"/>
</xsl:element>
</xsl:for-each>
</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
【问题讨论】:
-
确保接受最能解决您问题的答案。
-
@empo:他不知道“接受答案”是什么意思。
标签: xslt