English 中文(简体)
在相同的XML儿童中使用XSL
原标题: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>

这一例子中发生的情况是,生成的超文本有10栏,其中1栏是“姓名”和“类型1”、“类型2”、“类型3”三倍(三部分是三部分);如果我的投入中有4项内容的话,将增加3栏。 我只想在一栏中显示“类型1”、“类型2”和“类型3”。 我怎么办?

对所有帮助都表示赞赏和感谢。

最佳回答

您使用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>
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签