0

I have a XML file like this one

<contents>
        <content loid="1.0.71719207" type="images"/>
        <content loid="1.0.71719207" type="images"/>
    <content loid="1.0.71719207" type="images"/>
    <content loid="1.0.71719207" type="images"/>
</contents>

With XSL I want to obtain the following XML:

<div class="Image_1"></div>
<div class="Image_2"></div>
<div class="Image_3"></div>
<div class="Image_4"></div>

So basically I need to perform a loop and for every <xsl:for-each select="contents/content"> to print out <div class="Image_N"></div> where N is the number of the node.

I'm trying with Altova simulator, but I don't know how to increment N from 1 to number of nodes.

This is my code. I'm a beginner with XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="no"     encoding="utf-8" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"     doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />
    <xsl:template match="/">

                        <xsl:for-each select="contents/content">
                                <div class="EM_Story_Image_N"></div>

                       </xsl:for-each>


    </xsl:template>
</xsl:stylesheet>
4

3 回答 3

2

当这个 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="content">
    <div class="Image_{position()}" />
  </xsl:template>

</xsl:stylesheet>

...应用于 OP 的原始 XML:

<contents>
  <content loid="1.0.71719207" type="images"/>
  <content loid="1.0.71719207" type="images"/>
  <content loid="1.0.71719207" type="images"/>
  <content loid="1.0.71719207" type="images"/>
</contents>

...产生了预期的结果:

<div class="Image_1" />
<div class="Image_2" />
<div class="Image_3" />
<div class="Image_4" />
于 2012-11-14T11:59:22.200 回答
2

forXSLT 中没有像您在更多过程或 OO 语言中那样的计数循环,但您确实可以访问当前列表中当前节点的位置,您正在通过该position()函数对其进行迭代。

<xsl:for-each select="contents/content">
  <div class="EM_Story_Image_{position()}"></div>
</xsl:for-each>

但请注意,这在position()很大程度上取决于上下文 - 如果你有,说

<contents>
  <content loid="1.0.71719207" type="images"/>
  <content loid="1.0.71719207" type="images"/>
  <content type="somethingelse" />
  <content loid="1.0.71719207" type="images"/>
  <content loid="1.0.71719207" type="images"/>
</contents>

那么在<xsl:for-each select="contents/content">最后一个<content>元素中的位置为 5,但在<xsl:for-each select="contents/content[@type='images']">同一个节点中的位置为 4。

于 2012-11-14T12:08:32.160 回答
1

我建议学习 Michael Kay 的书“XSLT 2.0 和 XPath 2.0 程序员参考”(亚马逊链接与回扣

这是一种比使用更麻烦的方法position()

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="no"     encoding="utf-8" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"     doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />
    <xsl:template match="/">
        <html>
            <head><title>Test conteggio</title>
            </head>
            <body>

                <xsl:for-each select="contents/content">
                    <xsl:variable name="count"><xsl:number level="any" count="content"/></xsl:variable>
                    <div class="Image_{$count}"></div>
                </xsl:for-each>

            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
于 2012-11-14T12:49:01.237 回答