簡體   English   中英

在相同的XML子代上使用XSL for-each

[英]Using an XSL for-each on identical XML children

簡而言之,我的問題是我想遍歷同一孩子之間的一個孩子的內容,但是當我嘗試這樣做時,它遍歷所有孩子的內容,因此給了我盡可能多的數據副本兒童。

示例代碼:

Input.xml文件

<?xml version="1.0"?>
<base>
    <item name="item1">
        <foo>
            <type1>1</type1>
            <type2>2</type2>
        </foo>
        <bar>
            <type3>3</type3>
        </bar>
    </item>
    <item name="item2">
        <foo>
            <type1>4</type1>
            <type2>5</type2>
        </foo>
        <bar>
            <type3></type3>
            <!-- NOTE! This value is missing. Therefore we must put a blank value in the table-->
        </bar>
    </item>
    <item name="item3">
        <foo>
            <type1>7</type1>
            <type2></type2>
            <!-- NOTE! This value is missing. Therefore we must put a blank value in the table-->
        </foo>
        <bar>
            <type3>9</type3>
        </bar>
    </item>
</base>

tableMaker.xsl

<?xml version="1.0"?>

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:cyg="http://CygNetSCADA.com/Schemas/CygNetEnterpriseObjects730">

    <xsl:output method="html" encoding="UTF-8" />

    <xsl:template match="/">
        <html>
            <body>
                <table border="1">
                    <tr>
                        <th>Name</th>
                        <xsl:for-each select="base/item/*/*">
                            <th>
                                <xsl:value-of select="local-name()"/>
                            </th>
                        </xsl:for-each>
                    </tr>

                    <xsl:for-each select="base/item">
                        <tr>
                            <th>
                                <xsl:value-of select="@name"/>
                            </th>

                            <xsl:for-each select="foo/*">
                                <xsl:choose>
                                    <xsl:when test=".[node()]">
                                        <td>
                                            <xsl:value-of select="." />
                                        </td>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <td />
                                        <!-- This is for that empty value in item3 -->
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:for-each>

                            <xsl:for-each select="bar/*">
                                <xsl:choose>
                                    <xsl:when test=".[node()]">
                                        <td>
                                            <xsl:value-of select="." />
                                        </td>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <td />
                                        <!-- This is for that empty value in item2 -->
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

在此示例中發生的是,生成的HTML有10列-一列用於“名稱”,三列“ type1”,“ type2”,“ type3”(三個元素三遍;如果我的輸入中有4個元素將會有3列)。 我只希望“ type1”,“ type2”和“ type3”在該列中顯示一次。 我該怎么做呢?

感謝所有幫助,並在此先感謝!

您正在使用XSL 2.0,因此大概可以訪問xsl:for-each-group元素。 在這種情況下,您需要的是:

<tr>
  <th>Name</th>
  <xsl:for-each-group select="base/item/*/*" group-by="local-name()">
    <th>
      <xsl:value-of select="current-grouping-key()"/>
    </th>
  </xsl:for-each>
</tr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM