合并两个单词列表不适用于位置功能

问题描述 投票:0回答:1

我一直在尝试通过将两个不同的单词列表混合在一起来获取字典,但是当我通过 Saxon 运行它时,它只在每一行重复 wordlist2 中的单词#1。看起来 Position() 一直返回 1,即使它在循环?我对此很陌生,所以一切都很混乱。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="Lexicon" select="document('./Lexicon.xml')"/>
<xsl:variable name="URI1" select="$Lexicon/lexicon/wordlist1"/>
<xsl:variable name="URI2" select="$Lexicon/lexicon/wordlist2"/>
<xsl:variable name="URI3" select="$Lexicon/lexicon/icon"/>
<xsl:variable name="Wordlist1" select="document($URI1)"/>
<xsl:variable name="Wordlist2" select="document($URI2)"/>
<xsl:variable name="Icon" select="document($URI3)"/>
<xsl:template match="/">
    <html>
      <body>
      <xsl:copy-of select="$Icon"/>
        <table border="1">
          <tr bgcolor="#9acd32">
          <td colspan="2">Lexicon from <xsl:value-of select="document($URI1)/wordlist/@lang"/> to <xsl:value-of select="document($URI2)/wordlist/@lang"/></td>
          </tr>
          <xsl:for-each select="$Wordlist1/wordlist/word">
          <xsl:sort select="."/>
            <tr>
              <td><xsl:value-of select="."/></td>
              <td><xsl:value-of select="$Wordlist2/wordlist/word[position()]"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
 </xsl:stylesheet>

我希望它列出文件 A 中的 10 个单词,然后列出文件 B 中的 10 个单词,每行一个单词对。

xml xslt position
1个回答
0
投票

考虑以下简化示例:

XML

<lexicon>
   <wordlist1>
      <word>aaa</word>
      <word>bbb</word>
      <word>ccc</word>
      <word>ddd</word>
   </wordlist1>
   <wordlist2>
      <word>one</word>
      <word>two</word>
      <word>three</word>
      <word>four</word>
   </wordlist2>
</lexicon>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/lexicon">
    <xsl:variable name="wordlist2" select="wordlist2" />
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>From</th>
                    <th>To</th>
                </tr>
                <xsl:for-each select="wordlist1/word">
                    <xsl:variable name="i" select="position()" />
                    <tr>
                        <td>
                            <xsl:value-of select="."/>
                        </td>
                        <td>
                            <xsl:value-of select="$wordlist2/word[$i]"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

结果(渲染)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.