2017-06-21 77 views
0

我有下面的XMLXSLT - 每個與多個列表

<ListA> 
    <item index="0">Test 1</item> 
    <item index="1">Test 2</item> 
    <item index="2">Test 3</item> 
</ListA> 
<ListB> 
    <item index="0">100</item> 
    <item index="1">200</item> 
    <item index="2">300</item> 
</ListB> 
用XSLT

現在我想輸出下面for-each聲明:

<xsl:for-each select="ListA/item"> 
    <li> 
     <xsl:value-of select="."/> <xsl:value-of select="ListB/item"/> 
    </li> 
</xsl:for-each>  

問題在於內使用的物品數組listB。如果我用這個:

<xsl:for-each select="ListA/item"> 
     <li> 
      <xsl:value-of select="."/> 
     </li> 
    </xsl:for-each>  

我會得到輸出:

<li> Test 1 </li> 
<li> Test 2 </li> 
<li> Test 3 </li> 

但我不知道我怎麼能使用內部的for-each兩個列表。任何人都可以幫助 我嗎?

- 編輯 - 預期的輸出,我想的是:

<li> Test 1 100</li> 
<li> Test 2 200</li> 
<li> Test 3 300</li> 
+1

請出示您的預計產量。 – Utkanos

+0

@Utkanos是很好的一個。添加! – Rotan075

+2

使用[** key **](https://www.w3.org/TR/xslt/#key)從ListB獲取相應的項目。 –

回答

2

在XSLT 1.0中,我會使用密鑰。這個想法是,你應該在ListB中索引項目以便快速查找。

輸入:

<root> 
    <ListA> 
    <item index="0">Test 1</item> 
    <item index="1">Test 2</item> 
    <item index="2">Test 3</item> 
    </ListA> 
    <ListB> 
    <item index="0">100</item> 
    <item index="1">200</item> 
    <item index="2">300</item> 
    </ListB> 
</root> 

樣式表:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:key name="list_b" match="/root/ListB/item" use="@index"/> 

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

    <xsl:template match="ListA/item"> 
    <li> 
     <xsl:apply-templates /> - <xsl:text /> 
     <xsl:value-of select="key('list_b', @index)" />    
    </li> 
    </xsl:template> 
</xsl:stylesheet> 
2

使用XSLT 3.0xsl:merge由撒克遜9.8(所有版本)或最新版本的Altova的XMLSpy /猛禽的,你可以使用所支持

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" 
    exclude-result-prefixes="xs math" 
    version="3.0"> 

    <xsl:output indent="yes"/> 

    <xsl:template match="Root"> 
     <ul> 
      <xsl:merge> 
       <xsl:merge-source for-each-item="ListA, ListB" select="item"> 
        <xsl:merge-key select="@index"></xsl:merge-key> 
       </xsl:merge-source> 
       <xsl:merge-action> 
        <li> 
         <xsl:value-of select="current-merge-group()"/> 
        </li> 
       </xsl:merge-action> 
      </xsl:merge> 
     </ul> 
    </xsl:template> 

</xsl:stylesheet> 

其將

<Root> 
    <ListA> 
     <item index="0">Test 1</item> 
     <item index="1">Test 2</item> 
     <item index="2">Test 3</item> 
    </ListA> 
    <ListB> 
     <item index="0">100</item> 
     <item index="1">200</item> 
     <item index="2">300</item> 
    </ListB> 
</Root> 

分成

<ul> 
    <li>Test 1 100</li> 
    <li>Test 2 200</li> 
    <li>Test 3 300</li> 
</ul> 
0
<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

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

    <xsl:template match="item"> 
    <li> 
     <xsl:value-of select="."/><xsl:text> </xsl:text> 
     <xsl:value-of select="../../ListB/item[@index=current()/@index]"/> 
    </li> 
    </xsl:template> 

</xsl:stylesheet> 

用於簡單的foreach

<xsl:for-each select="ListA/item"> 
    <li> 
    <xsl:value-of select="."/> 
    <xsl:text> </xsl:text> 
    <xsl:value-of select="../../ListB/item[@index=current()/@index]"/> 
    </li> 
</xsl:for-each>