根据两个条件删除元素

时间:2011-02-08 14:21:10

标签: xml xslt

我是XSLT的新手,从输出中删除重复的条目时遇到问题。

示例XML-File:

<Datenaustausch>
<Meldung>
    <Anfallstelle>
        <AS>
            <ASStamm>
                <ASNR>009803336</ASNR>
                <ASENF>false</ASENF>
                <ASInaktiv>false</ASInaktiv>
            </ASStamm>
            <ASDaten>
                <ASMeldung>
                    <ASGueltigAb>2003-04-25</ASGueltigAb>
                    <ASMldData>
                        <ASLizData>
                            <ASFrk>8xx</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                    </ASMldData>
                </ASMeldung>
                <ASMeldung>
                    <ASGueltigAb>2008-05-15</ASGueltigAb>
                    <ASMldData>
                        <ASLizData>
                            <ASFrk>8xx</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                    </ASMldData>
                </ASMeldung>
                <ASMeldung>
                    <ASGueltigAb>2010-08-20</ASGueltigAb>
                    <ASMldData>
                        <ASLizData>
                            <ASFrk>40x</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                        <ASLizData>
                            <ASFrk>8xx</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                    </ASMldData>
                </ASMeldung>
            </ASDaten>
        </AS>
        <AS>
            <ASStamm>
                <ASNR>031630116</ASNR>
                <ASENF>false</ASENF>
                <ASInaktiv>false</ASInaktiv>
            </ASStamm>
            <ASDaten>
                <ASMeldung>
                    <ASGueltigAb>2009-04-21</ASGueltigAb>
                    <ASMldData>
                        <ASLizData>
                            <ASFrk>400</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                        <ASLizData>
                            <ASFrk>8xx</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                    </ASMldData>
                </ASMeldung>
            </ASDaten>
        </AS>
        <AS>
            <ASStamm>
                <ASNR>040917889</ASNR>
                <ASENF>false</ASENF>
                <ASInaktiv>false</ASInaktiv>
            </ASStamm>
            <ASDaten>
                <ASMeldung>
                    <ASGueltigAb>2007-11-15</ASGueltigAb>
                    <ASMldData>
                        <ASLizData>
                            <ASFrk>400</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                        <ASLizData>
                            <ASFrk>42x</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                        <ASLizData>
                            <ASFrk>8xx</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                    </ASMldData>
                </ASMeldung>
                <ASMeldung>
                    <ASGueltigAb>2009-01-19</ASGueltigAb>
                    <ASMldData>
                        <ASLizData>
                            <ASFrk>400</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                        <ASLizData>
                            <ASFrk>42x</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                        <ASLizData>
                            <ASFrk>8xx</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                    </ASMldData>
                </ASMeldung>
                <ASMeldung>
                    <ASGueltigAb>2010-06-25</ASGueltigAb>
                    <ASMldData>
                        <ASLizData>
                            <ASFrk>400</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                        <ASLizData>
                            <ASFrk>42x</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                        <ASLizData>
                            <ASFrk>8xx</ASFrk>
                            <ASLizGrad>100</ASLizGrad>
                            <ASAntVerp>100</ASAntVerp>
                        </ASLizData>
                    </ASMldData>
                </ASMeldung>
            </ASDaten>
        </AS>
                    <AS>
                    ....
                    </AS>
        </Anfallstelle>
</Meldung>
</Datenaustausch>

....表示与上述相同的订单

我需要获得此输出:

从ID为'ASNR'的每个'ASStamm'我删除带有最早日期('ASGueltigAb')的重复条目('ASFrk')。

例如,第一个“AS”条目应如下所示:

       <tr>
            <td>009803336</td>
            <td>8xx</td>
            <td>100</td>
            <td>100</td>
            <td>2008-05-15</td>
        </tr>
        <tr>
            <td>009803336</td>
            <td>40x</td>
            <td>100</td>
            <td>100</td>
            <td>2010-08-20</td>
        </tr>

删除了重复条目(带有'ASFrk'(8xx)和旧日期'ASGueltigAb'(2003-04-25))。

目前我使用这个“解决方案”将xml转换为html表,但不删除重复的条目。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <head>
                <title>XML</title>
            </head>
            <body>
                <table border="1">
                   <tr>
                        <th>ASNR</th>
                        <th>ASFrk</th>
                        <th>ASLizGrad</th>
                        <th>ASAntVerp</th>
                        <th>ASGueltigAb</th>
                    </tr>
                    <xsl:for-each select="/Datenaustausch/Meldung/Anfallstelle/AS">
                        <xsl:sort select="ASStamm/ASNR" data-type="number"/>
                        <xsl:for-each select="ASDaten/ASMeldung">
                            <xsl:sort select="ASMldData/ASLizData/ASFrk" order="descending"/>
                            <xsl:sort select="ASGueltigAb" order="descending"  />
                            <tr>
                                <td>
                                    <xsl:value-of select="parent::*/parent::AS/ASStamm/ASNR"/>
                                </td>
                                <td>
                                    <xsl:value-of select="ASMldData/ASLizData/ASFrk"/>
                                </td>
                                <td>
                                    <xsl:value-of select="ASMldData/ASLizData/ASLizGrad"/>
                                </td>
                                <td>
                                    <xsl:value-of select="ASMldData/ASLizData/ASAntVerp"/>
                                </td>
                                <td>
                                    <xsl:value-of select="ASGueltigAb"/>
                                </td>
                            </tr>
                        </xsl:for-each>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

HTML输出:

示例1: http://img152.imageshack.us/i/xmlex.jpg/

对于'ASNR'-ID(009803336),应删除标记的行,对于下一个示例'ASNR'-ID(040917889),应删除两个标记的行。

示例2: http://img710.imageshack.us/i/xmlex2.jpg/

我认为有一个使用“Muenchian分组”的解决方案,但我不知道如何。

有人可以帮助我使用XSLT 1.0获得解决方案吗?

1 个答案:

答案 0 :(得分:2)

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kASLizDataByASNR-ASFrk"
             match="ASLizData"
             use="concat(preceding::ASNR[1],'+',ASFrk)"/>
    <xsl:template match="text()"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>XML</title>
            </head>
            <body>
                <table border="1">
                    <tr>
                        <th>ASNR</th>
                        <th>ASFrk</th>
                        <th>ASLizGrad</th>
                        <th>ASAntVerp</th>
                        <th>ASGueltigAb</th>
                    </tr>
                    <xsl:apply-templates/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="ASLizData[
                            count(
                               .|key('kASLizDataByASNR-ASFrk',
                                     concat(preceding::ASNR[1],'+',ASFrk)
                                 )[1]
                            ) = 1
                         ]">
        <xsl:variable name="vASNR" select="preceding::ASNR[1]"/>
        <xsl:for-each select="key('kASLizDataByASNR-ASFrk',
                                  concat($vASNR,'+',ASFrk)
                              )">
            <xsl:sort select="../../ASGueltigAb" order="descending"/>
            <xsl:if test="position()=1">
                <tr>
                    <td>
                        <xsl:value-of select="$vASNR"/>
                    </td>
                    <xsl:apply-templates mode="cell"/>
                    <td>
                        <xsl:value-of select="../../ASGueltigAb"/>
                    </td>
                </tr>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="ASLizData/*" mode="cell">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>
</xsl:stylesheet>

输出:

<html>
    <head>
        <META http-equiv="Content-Type" content="text/html; charset=UTF-16">
        <title>XML</title>
    </head>
    <body>
        <table border="1">
            <tr>
                <th>ASNR</th>
                <th>ASFrk</th>
                <th>ASLizGrad</th>
                <th>ASAntVerp</th>
                <th>ASGueltigAb</th>
            </tr>
            <tr>
                <td>009803336</td>
                <td>8xx</td>
                <td>100</td>
                <td>100</td>
                <td>2010-08-20</td>
            </tr>
            <tr>
                <td>009803336</td>
                <td>40x</td>
                <td>100</td>
                <td>100</td>
                <td>2010-08-20</td>
            </tr>
            <tr>
                <td>031630116</td>
                <td>400</td>
                <td>100</td>
                <td>100</td>
                <td>2009-04-21</td>
            </tr>
            <tr>
                <td>031630116</td>
                <td>8xx</td>
                <td>100</td>
                <td>100</td>
                <td>2009-04-21</td>
            </tr>
            <tr>
                <td>040917889</td>
                <td>400</td>
                <td>100</td>
                <td>100</td>
                <td>2010-06-25</td>
            </tr>
            <tr>
                <td>040917889</td>
                <td>42x</td>
                <td>100</td>
                <td>100</td>
                <td>2010-06-25</td>
            </tr>
            <tr>
                <td>040917889</td>
                <td>8xx</td>
                <td>100</td>
                <td>100</td>
                <td>2010-06-25</td>
            </tr>
        </table>
    </body>
</html>

注意:ASNR和ASFrk的密钥。最高for-each