2017-02-01 78 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>