0

我正在尝试在<item>节点和前一个<item>节点中输出数据。我可以<item>在同一个节点中访问前一个节点,<group>但是一旦它移动到下一个组,我就会失去前一个。

<?xml version="1.0"?>
<?xml-stylesheet href="flatten.xslt" type="text/xsl"?>
<data>
    <group id="1">
        <title>Group Title 1</title>
        <category>A</category>
        <items>
            <item id="11">
                <title>Title 11</title>
            </item>
            <item id="12">
                <title>Title 12</title>
            </item>
            <item id="13">
                <title>Title 13</title>
            </item>
            <item id="14">
                <title>Title 14</title>
            </item>
            <item id="16">
                <title>Title 15</title>
            </item>
            <item id="17">
                <title>Title 16</title>
            </item>
        </items>
    </group>

    <group id="2">
        <title>Group Title 2</title>
        <category>A</category>
        <items>
            <item id="21">
                <title>Title 21</title>
            </item>
            <item id="23">
                <title>Title 22</title>
            </item>
            <item id="22">
                <title>Title 23</title>
            </item>
            <item id="24">
                <title>Title 24</title>
            </item>
            <item id="25">
                <title>Title 25</title>
            </item>
            <item id="27">
                <title>Title 27</title>
            </item>
        </items>
    </group>

    <group id="3">
        <title>Group Title 3</title>
        <category>B</category>
        <items>
            <item id="31">
                <title>Title 31</title>
            </item>
            <item id="32">
                <title>Title 32</title>
            </item>
            <item id="33">
                <title>Title 33</title>
            </item>
            <item id="34">
                <title>Title 34</title>
            </item>
            <item id="36">
                <title>Title 36</title>
            </item>
            <item id="37">
                <title>Title 37</title>
            </item>
        </items>
    </group>

</data>

我正在寻找这个:

<ul>
    <li>
        <h1>Item Title 11</h1>
        <h2>Previous: </h2>
    </li>
    <li>
        <h1>Item Title 12</h1>
        <h2>Previous: Item Title 11</h2>
    </li>
    <li>
        <h1>Item Title 13</h1>
        <h2>Previous: Item Title 12</h2>
    </li>
    <li>
        <h1>Item Title 14</h1>
        <h2>Previous: Item Title 13</h2>
    </li>
    <li>
        <h1>Item Title 15</h1>
        <h2>Previous: Item Title 14</h2>
    </li>
    <li>
        <h1>Item Title 16</h1>
        <h2>Previous: Item Title 15</h2>
    </li>
    <li>
        <h1>Item Title 21</h1>
        <h2>Previous: Item Title 16</h2>
    </li>
    <li>
        <h1>Item Title 22</h1>
        <h2>Previous: Item Title 21</h2>
    </li>
    <li>
        <h1>Item Title 23</h1>
        <h2>Previous: Item Title 22</h2>
    </li>
    <li>
        <h1>Item Title 24</h1>
        <h2>Previous: Item Title 23</h2>
    </li>
</ul>
4

1 回答 1

0

好的,我想出了这个:

  1. 创建一个新变量,将所有<item>节点拉出到平面结构中。
  2. 循环遍历变量

如果您知道更好的方法,请分享。

<?xml version="1.0"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:exsl="http://exslt.org/common"
version="1.0">

    <xsl:output method="html"/>

    <xsl:template match="/">
        <html>
        <head>
            <title>XSLT Flatten</title>
        </head>

        <body>

            <xsl:variable name="flat-items">
                <xsl:element name="items">
                    <xsl:for-each select="data/group/items/item">
                        <xsl:copy-of select="."/>
                    </xsl:for-each>
                </xsl:element>
            </xsl:variable>

            <p>
                <h2>Total Items: <xsl:value-of select="count(exsl:node-set($flat-items)/items/item)"/></h2>
            </p>

            <ul>
                <xsl:for-each select="exsl:node-set($flat-items)/items/item">
                    <li>
                        <h2><xsl:value-of select="title"/> (<xsl:value-of select="@id"/>)</h2>
                        <h3>Previous: <xsl:value-of select="./preceding-sibling::item[1]/title"/> (<xsl:value-of select="./preceding-sibling::item[1]/@id"/>)
                        </h3>
                    </li>
                </xsl:for-each>
            </ul>

        </body>
        </html>

    </xsl:template>

</xsl:stylesheet>
于 2013-07-04T08:45:00.437 回答