繁体   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