我有两个文件。数据有一些重叠,但我想从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中提取年份和页面标签并将其添加到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&gt; 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>
答案 0 :(得分:1)
我想,你正在寻找这些方面的东西。修复语法留给你。
<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>
答案 1 :(得分:1)
交叉引用要求使用密钥:
<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>