2017-02-01 79 views
0

您能告訴我如何在XSLT中應用for-loop嗎?XSLT:如何申請循環?

這是我的live code at xsltransform.net

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<body> 
    <h1>A new version of xsltransform.net is released!</h1> 
    <p>We have added the following new features:</p> 
    <ul> 
     <li>A new XSLT engine is added: Saxon 9.5 EE, with a license (thank you Michael Kay!)</li> 
     <li>XSLT 3.0 support when using the new Saxon 9.5 EE engine!</li> 
     <li>Preview your result as HTML when doctype is set to HTML (see this example)</li> 
     <li>Preview your result as PDF when doctype is set to XML and your document starts with root element of XSL-FO. Apache FOP is used to generate the PDF</li> 
     <li>Added some links to useful XSLT sites</li> 
    </ul> 
</body> 

我的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:variable name="test" select="'ss'"/> 
    <xsl:variable name="inline-array"> 
     <Item>A</Item> 
     <Item>B</Item> 
     <Item>C</Item> 
    </xsl:variable> 

    <xsl:param name="array" select="document('')/*/xsl:variable[@name='inline-array']/*"/> 

    <xsl:template match="/"> 
     <xsl:for-each select="$inline-array"> 
      <h1><xsl:value-of select="."/></h1> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

預期輸出

<h1>A</h1> 
<h1>B</h1> 
<h1>C</h1> 

回答

2

在XSLT 1.0,您inline-array變量包含一個 「結果樹片段」,因此訪問其中的節點上,您將需要利用「節點集」擴展功能。

試試這個XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:exsl="http://exslt.org/common" 
    extension-element-prefixes="exsl"> 

    <xsl:variable name="inline-array"> 
     <Item>A</Item> 
     <Item>B</Item> 
     <Item>C</Item> 
    </xsl:variable> 

    <xsl:template match="/"> 
     <xsl:for-each select="exsl:node-set($inline-array)/Item"> 
     <h1><xsl:value-of select="."/></h1> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

另外請注意,您還可以使用document功能,因爲你已經包含在你品嚐,但我相信XSLTransform.net不允許使用文檔的功能,所以你不能有測試,但將使用某種在你的XSLT文件中的「數據孤島」的這應該工作在本地

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:variable name="array" select="document('')//xsl:variable[@name='inline-array']/*"/> 

    <xsl:variable name="inline-array"> 
     <Item>A</Item> 
     <Item>B</Item> 
     <Item>C</Item> 
    </xsl:variable> 

    <xsl:template match="/"> 
     <xsl:for-each select="$array"> 
      <h1> 
       <xsl:value-of select="."/> 
      </h1> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 
3

只需添加另一個for-eachitem

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:variable name="test" select="'ss'"/> 
    <xsl:variable name="inline-array"> 
     <Item>A</Item> 
     <Item>B</Item> 
     <Item>C</Item> 
    </xsl:variable> 

    <xsl:param name="array" select="document('')/*/xsl:variable[@name='inline-array']/*"/> 

    <xsl:template match="/"> 
     <xsl:for-each select="$inline-array"> 
     <xsl:for-each select="Item"> //<--Added this line 
      <h1><xsl:value-of select="."/></h1>  
     </xsl:for-each> //<--Added this line too 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<?xml version="1.0" encoding="UTF-8"?><h1>A</h1><h1>B</h1><h1>C</h1> 

Demo

+0

樣式表聲明1.0版本,但你用XSLT 2.0處理器測試它。 **它在XSLT 1.0 **中不起作用:http://xsltransform.net/gWEamLL/5 ---更不用說單個的'就足夠了。 –

0

另一種運行創建和訪問的<xsl:variable>

這種方法的一個主要優勢是您不必處理RTF(結果樹片段),因此您可以隨心所欲地查詢XML。

它的創建只需通過爲其定義名稱空間(此處爲:xmlns:items="http://ite.ms"),然後通過document('')/xsl:stylesheet/namespace:... XPath訪問它來完成。

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

    <!-- kind of "data-island" --> 
    <items:Items>  
     <Item>A</Item> 
     <Item>B</Item> 
     <Item>C</Item> 
    </items:Items>  

    <xsl:template match="/"> 
     <xsl:for-each select="document('')/xsl:stylesheet/items:Items/Item"> 
     <h1><xsl:value-of select="text()" /></h1><xsl:text>&#xa;</xsl:text> 
     </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

我更喜歡這種方式,因爲恕我直言,這是將XML數據嵌入到XSLT中最乾淨的方法。

輸出:

<?xml version="1.0"?> 
<h1>A</h1> 
<h1>B</h1> 
<h1>C</h1>