0

已经尝试了几个小时让这个工作,但我一定错过了一些东西。

我想通过两个文档中的 id 将两个 XML 文档映射在一起。我可以使用一个 XSLT 从两个文档中获取数据输出,但我不知道如何映射它们。

第一个xml:

...
<member>
  <id>1</id>
  <name>John</name>
</member>
<member>
  <id>2</id>
  <name>Otto</name>
</member>
...

第二个xml:(ss命名空间)

...
<row>
  <cell ss:Type="String">id</cell>
  <cell ss:Type="String">Number 1</cell>
  <cell ss:Type="String">Number 2</cell>
</row>
<row>
  <cell ss:Type="Number">1</cell>
  <cell ss:Type="Number">1231312313</cell>
  <cell ss:Type="Number">234234234342</cell>
</row>
<row>
  <cell ss:Type="Number">2</cell>
  <cell ss:Type="Number">4353453453</cell>
  <cell ss:Type="Number">345345345455</cell>
</row>
...

我遍历 XSLT 文件中的 name 和 id 元素,在这个循环中,我试图使用应用模板获取映射到第二个 xml 中的 id 的值。

最终的 HTML 输出应该类似于:

Id      Name          Number 1         Number 2
1       John          1231312313       234234234342
2       Otto          4353453453       345345345455
4

2 回答 2

0

我用调用模板“函数”解决了它,并将 id 的值作为参数传递。然后使用 for-each 和 if 语句在第二个文件中选取“数字”标签值。有用。

于 2012-11-20T12:23:02.113 回答
0

也许以下可以作为起点:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0" xmlns:ss="NS:SS">

  <xsl:template match="/">
    <html>
      <table>
        <xsl:apply-templates/>
      </table>
    </html>
  </xsl:template>

  <xsl:template match="*">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="ss:row">
    <tr>
      <xsl:apply-templates/>
    </tr>
  </xsl:template>

  <xsl:template match="ss:cell">
    <td><xsl:value-of select="."/></td>
  </xsl:template>

  <xsl:template match="ss:cell[1]">
    <xsl:variable name="id" select="string()"/>
    <td><xsl:value-of select="$id"/></td>
    <!-- Especially for large amounts of data, making use of key() would be better than "//" 
         The key() call can be wrapped inside a <xsl:for-each select="document('firstDocument.xml')>
         that changes the context to the other document -->
    <td><xsl:value-of select="document('firstDocument.xml')//member[id=$id]/name"/></td>
  </xsl:template>
</xsl:stylesheet>

这必须适用于您所谓的第二个文档。使用该document()函数调用第一个文档。

正如 Martin Honnen 指出的那样,使用key()会更好(特别是如果您有大型数据集)。请参阅有关在 XSLT 1.0 中包装调用 to 以使其工作的key()评论<xsl:for-each>

于 2012-11-19T15:35:57.063 回答