【问题标题】:Combining two xml files based on unique nodes using XSL使用 XSL 基于唯一节点组合两个 xml 文件
【发布时间】: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


【解决方案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>

【讨论】:

    【解决方案2】:

    交叉引用要求使用密钥:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2011-11-03
      • 1970-01-01
      • 2013-06-09
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      相关资源
      最近更新 更多