Xslt 使用XSL基于唯一节点组合两个xml文件
我有两个文件。数据中有一些重叠,但我想从文件2中提取特定信息并添加到文件1中 File1.xml看起来是这样的:Xslt 使用XSL基于唯一节点组合两个xml文件,xslt,Xslt,我有两个文件。数据中有一些重叠,但我想从文件2中提取特定信息并添加到文件1中 File1.xml看起来是这样的: <content> <book> <id>111aaa</id> <author>John Doe</author> <title>This is a book</title> <price>$10.00</price> &l
<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>
<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>
<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>
111aaa
无名氏
这是一本书
$10.00
111bbb
无名氏
这是另一本书
$20.00
File2.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>
<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>
<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>
111aaa
无名氏
2011
100
111bbb
无名氏
2010
200
我想从file2中提取year和pages标签,并将其添加到file1中,然后输出一个file3.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>
<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>
<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>
111aaa
无名氏
这是一本书
2011
100
$10.00
111bbb
无名氏
这是另一本书
2010
200
$20.00
我正在使用命令行运行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>
我想你在寻找类似的东西。修复语法由您决定
<xsl:for-each select="book">
<xsl:copy>
<xsl:apply-templates/>
<xsl:variable name="id" select="string(id)"/>
<xsl:for-each select="document('file2.xml')/content/book[string(id)=$id]/*">
<xsl:apply-templates/><!-- Add in a choose here if you just want to pick out certain fields, or in the for-each above -->
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
交叉引用要求使用密钥:
<xsl:key name="k1" match="book" use="id"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<xsl:variable name="id" select="id"/>
<xsl:apply-templates select="@* | id | author"/>
<xsl:for-each select="document('file2.xml')">
<xsl:apply-templates select="key('k1', $id)/year | key('k1', $id)/pages"/>
</xsl:for-each>
<xsl:apply-templates select="price"/>
</xsl:template>
确保接受最能解决问题的答案。@empo:他不知道“接受答案”是什么意思。