使用xslt对节点进行分组并删除相同的值

时间:2013-08-14 06:27:06

标签: xml xslt xpath

之前我问过这个,但我会尝试更详细地解释我的问题。我在一个用户可以预订房间的系统中获取一个XML(这个实际上比我的例子更大)。这些房间用于从一个城市到另一个城市进行通信。 因此,如果一个人想要预订,他必须预订至少2个房间。我创建了一个表,以便其他用户可以看到谁在哪个日期/时间预订了哪个房间。

table(暂时无法上传图片) 你可以看到表格不是很清楚。它有很多相同的值,不需要。所以我想要的只是一个日期和时间以及每个预订的一个名称。但是房间必须至少有2个相同才能看到更好的景色。 在我的xslt你也可以看到试图在测试中获得一组节点

你可以帮忙吗?

<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href= 'testxsl.xsl'?>
<Objects>
    <Object>
        <Property Name="Datum">01.08.2013</Property>
        <Property Name="Von">04:00:00</Property>
        <Property Name="Bis">06:00:00</Property>
        <Property Name="Raum">Cologne</Property>
        <Property Name="Gebucht_Von">ExamplePerson 1</Property>
    </Object>
    <Object>
        <Property Name="Datum">01.08.2013</Property>
        <Property Name="Von">04:00:00</Property>
        <Property Name="Bis">06:00:00</Property>
        <Property Name="Raum">Munich</Property>
        <Property Name="Gebucht_Von">ExamplePerson 1</Property>
    </Object>
    <Object>
        <Property Name="Datum">01.08.2013</Property>
        <Property Name="Von">08:00:00</Property>
        <Property Name="Bis">12:00:00</Property>
        <Property Name="Raum">Beijing </Property>
        <Property Name="Gebucht_Von">ExpamplePerson 2 </Property>
    </Object>
    <Object>
        <Property Name="Datum">01.08.2013</Property>
        <Property Name="Von">08:00:00</Property>
        <Property Name="Bis">12:00:00</Property>
        <Property Name="Raum">Munich </Property>
        <Property Name="Gebucht_Von">ExpamplePerson 2 </Property>
    </Object>
    <Object>
        <Property Name="Datum">01.08.2013</Property>
        <Property Name="Von">08:30:00</Property>
        <Property Name="Bis">11:00:00</Property>
        <Property Name="Raum">Bombay </Property>
        <Property Name="Gebucht_Von">ExpamplePerson 2 </Property>
    </Object>
</Objects>

XSLT样式表

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"   encoding="iso-8859-1" indent="yes"/>
    <xsl:template match="/">
        <TABLE border="6pt" align="center" >
            <colgroup>
                <col width="150"/>
                <col width="100" />
                <col width="100" />
                <col width="200"/>
                <col width="500"/>
            </colgroup>
            <TR STYLE="font-size:16pt; background-color:#E4FEFF; color:051956; font-family:Times New      Roman',Times,serif"> <!-- Eine Zeile der Tabelle wird geöffnet -->
                <span />
                <TD>
                    <u>
                        <i>Daten</i>
                    </u>
                </TD>
                <TD>
                    <u>
                        <i>From</i>
                    </u>
                </TD>
                <TD>
                    <u>
                        <i>To</i>
                    </u>
                </TD>
                <TD>
                    <u>
                        <i>Room</i>
                    </u>
                </TD>
                <TD>
                    <u>
                        <i>Bookey by</i>
                    </u>
                </TD>
                <TD>
                    <u>
                        <i>test</i>
                    </u>
                </TD>
            </TR>
            <xsl:apply-templates/>
        </TABLE>
    </xsl:template>

    <xsl:template match="Objects">
        <xsl:for-each select="Object">
            <TR STYLE="font-size:13pt;  background-color:#E4FEFF;color:051956; font-family:ARIAL">
                <TD>
                    <xsl:value-of select="Property[1]/text()"/>
                    <TD>
                        <xsl:value-of select="Property[2]/text()"/>
                    </TD>
                    <TD>
                        <xsl:value-of select="Property[3]/text()"/>
                    </TD>
                    <TD>
                        <xsl:value-of select="Property[4]/text()"/>
                    </TD>
                    <TD>
                        <xsl:value-of select="Property[5]/text()"/>
                        <TD>
                            <xsl:value-of select= "current()/Property[(current()/Property[1]= following:: */Property[1])and
                                (current()/Property[2]= following::  */Property[2])and
                                (current()/Property[3]= following:: */Property[3])and
                                (current()/Property[4]!= following::*/Property[4])and
                            (current()/Property[5]= following:: */Property[5])]"/>
                        </TD>
                    </TR>
                </xsl:for-each>
            </xsl:template>
        </xsl:stylesheet>

这就是我要输出的内容

<TABLE border="6pt" align="center">
<colgroup>
    <col width="150">
    <col width="100">
    <col width="100">
    <col width="200">
    <col width="500">
</colgroup>
<TR STYLE="font-size:16pt; background-color:#E4FEFF; color:051956; font-family:Times New Roman',Times,serif"><span></span><TD><u><i>Date</i></u></TD>
    <TD><u><i>From</i></u></TD>
    <TD><u><i>To</i></u></TD>
    <TD><u><i>Room</i></u></TD>
    <TD><u><i>Booked by</i></u></TD>
</TR>
<TR STYLE="font-size:13pt;  background-color:#E4FEFF;color:051956; font-family:ARIAL">
    <TD>01.08.2013</TD>
    <TD>04:00:00</TD>
    <TD>06:00:00</TD>
    <TD>Cologne, Munich</TD>
    <TD>ExamplePerson 1</TD>
</TR>
<TR STYLE="font-size:13pt;  background-color:#E4FEFF;color:051956; font-family:ARIAL">
    <TD>01.08.2013</TD>
    <TD>08:00:00</TD>
    <TD>12:00:00</TD>
    <TD>Beijing, Munich, Bombay </TD>
    <TD>ExpamplePerson 2 </TD>
    <TD>true</TD>
</TR>
      </TABLE>

1 个答案:

答案 0 :(得分:0)

根据要求:

<强> XSLT

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="groups" match="/Objects/Object" use="concat(Property[@Name='Datum'],Property[@Name='Von'],Property[@Name='Bis'],Property[@Name='Gebucht_Von'])" />
<xsl:template match="Objects">
  <html>
  <body>
    <table border="6pt" align="center">
    <colgroup>
        <col width="150" />
        <col width="100" />
        <col width="100" />
        <col width="200" />
        <col width="500" />
    </colgroup>
    <tr style="font-size: 16pt; background-color: #E4FEFF; color: 051956; font-family: Times New Roman ', Times, serif">
        <span></span>
        <th><u><i>Date</i></u></th>
        <th><u><i>From</i></u></th>
        <th><u><i>To</i></u></th>
        <th><u><i>Room</i></u></th>
        <th><u><i>Booked by</i></u></th>
    </tr>
      <xsl:for-each select="Object[generate-id() = generate-id(key('groups', concat(Property[@Name='Datum'],Property[@Name='Von'],Property[@Name='Bis'],Property[@Name='Gebucht_Von']))[1])]">
      <tr style="font-size: 13pt; background-color: #E4FEFF; color: 051956; font-family: ARIAL">
        <td><xsl:value-of select="Property[@Name='Datum']"/></td>
        <td><xsl:value-of select="Property[@Name='Von']"/></td>
        <td><xsl:value-of select="Property[@Name='Bis']"/></td>
        <td><xsl:for-each select="key('groups', concat(Property[@Name='Datum'],Property[@Name='Von'],Property[@Name='Bis'],Property[@Name='Gebucht_Von']))">
            <xsl:sort select="Property[@Name='Raum']" />
            <xsl:value-of select="Property[@Name='Raum']" /><xsl:if test="position() != last()" ><xsl:text>, </xsl:text></xsl:if>
        </xsl:for-each></td>
        <td><xsl:value-of select="Property[@Name='Gebucht_Von']"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

<强>输出 XSLT Ouput

注意:我更新了伦敦原始输入的其中一个值,以确保组正常工作。