如何编写XSLT以在HTML表格中每次出现兄弟节点时重复兄弟节点值

时间:2012-09-27 19:39:07

标签: xslt

我是XSLT的新手,所以请原谅我,如果我的代码很糟糕的话。我希望有一个HTML表格,每个日常干预都会重复剧集细节。在使用我的代码的momemt我甚至没有桌子。我已经找到了方法来做到这一点,但无济于事

我的XML

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="example_XMLTransformationFile.xsl"?>
<ClientImport xmlns:xsi="http://www.w3.org/2001/XMLSchema">
<episode localID="P333456">
    <episodeDetails>
        <caseNo>0000000</caseNo>
        <address1>where Person1 live</address1>
        <address4>Do they live there</address4>
        <postcode>PP1 2PP</postcode>
        <familyName>Person1</familyName>
        <firstName>PersonF1</firstName>
        <dobEst>0</dobEst>
    </episodeDetails>
    <dailyInterventions>
    <dailyIntervention>
        <activityDate>2012-08-23</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>true</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    <dailyIntervention>
        <activityDate>2012-08-24</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>true</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    <dailyIntervention>
        <activityDate>2012-08-25</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>false</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    <dailyIntervention>
        <activityDate>2012-08-26</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>true</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    <dailyIntervention>
        <activityDate>2012-08-27</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>true</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    <dailyIntervention>
        <activityDate>2012-08-28</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>true</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    <dailyIntervention>
        <activityDate>2012-08-29</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>true</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    <dailyIntervention>
        <activityDate>2012-08-30</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>false</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    <dailyIntervention>
        <activityDate>2012-08-31</activityDate>
        <ecg>true</ecg>
        <cpox>true</cpox>
        <invVentET>false</invVentET>
        <invVentTT>false</invVentTT>
    </dailyIntervention>
    </dailyInterventions>
</episode>
<episode localID="20120410">
    <episodeDetails>
        <caseNo>0000001</caseNo>
        <address1>where Person2 Lives</address1>
        <address4>Street Person2</address4>
        <postcode>XX1 1XX</postcode>
        <familyName>Person2</familyName>
        <firstName>Person2 A</firstName>
        <dobEst>0</dobEst>
    </episodeDetails>
    <dailyInterventions>
    <dailyIntervention>
        <activityDate>2012-08-23</activityDate>
        <noCrit>true</noCrit>
    </dailyIntervention>
    </dailyInterventions>
</episode>
<episode localID="20120411">
<episodeDetails>
    <caseNo>0000002</caseNo>
    <address1>Where Person3 Lives</address1>
    <address4>Person 3 Street</address4>
    <postcode>WW1 1WW</postcode>
    <familyName>Person3</familyName>
    <firstName>Person 3 B</firstName>
    <dobEst>0</dobEst>
</episodeDetails>
<dailyInterventions>
<dailyIntervention>
    <activityDate>2012-08-24</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>true</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-25</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>true</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-26</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>true</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-27</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-28</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-29</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
</dailyInterventions>
</episode>
<episode localID="20120418">
<episodeDetails>
    <caseNo>000004</caseNo>
    <address1>where person4 lives</address1>
    <address4>Person4 Road</address4>
    <postcode>NN1 1NN</postcode>
    <familyName>Person4</familyName>
    <firstName>Person4 B</firstName>
    <dobEst>0</dobEst>
</episodeDetails>
<dailyInterventions>
<dailyIntervention>
    <activityDate>2012-08-24</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
</dailyInterventions>
</episode>
<episode localID="20120419">
<episodeDetails>
    <caseNo>0000005</caseNo>
    <address1>Where Person5 Lives</address1>
    <address4>Person5 Road</address4>
    <postcode>SS1 1SS</postcode>
    <familyName>Person5</familyName>
    <firstName>Person5 K</firstName>
    <dobEst>0</dobEst>
</episodeDetails>
<dailyInterventions>
<dailyIntervention>
    <activityDate>2012-08-25</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-26</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-27</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-28</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-29</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-30</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-31</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-09-01</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-09-02</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-09-03</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
</dailyInterventions>
</episode>
<episode localID="20120420">
<episodeDetails>
    <caseNo>0000006</caseNo>
    <address1>Where person6 Lives</address1>
    <address4>Person6 Road</address4>
    <postcode>EE1 EE1</postcode>
    <familyName>Person6</familyName>
    <firstName>Person6 N</firstName>
    <dobEst>0</dobEst>
</episodeDetails>
<dailyInterventions>
<dailyIntervention>
    <activityDate>2012-08-23</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-24</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-25</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-26</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
<dailyIntervention>
    <activityDate>2012-08-27</activityDate>
    <ecg>true</ecg>
    <cpox>true</cpox>
    <invVentET>false</invVentET>
    <invVentTT>false</invVentTT>
</dailyIntervention>
</dailyInterventions>
</episode>

我的XSLT

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">     
        <html>    
            <xsl:apply-templates select="ClientImport"/>       
        </html> 
    </xsl:template> 
    <xsl:template match="ClientImport">     
        <table border="1">
        <tr>
            <th>LOCALID</th>
            <th>CASENO</th>
            <th>ADDRESS1</th>
            <th>ADDRESS4</th>
            <th>POSTCODE</th>
            <th>FAMILYNAME</th>
            <th>FIRSTNAME</th>
            <th>DOBEST</th>
            <th>ACTIVITYDATE</th>
            <th>ECG</th>
            <th>CPOX</th>
            <th>INVVENTET</th>
            <th>INVVENTTT</th>
        </tr>
        <xsl:apply-templates select="episode"/>                 
    </table>         
</xsl:template>
<xsl:template match="episode"> 
    <tr>
        <td><xsl:value-of select="@localID"/></td> 
        <xsl:apply-templates select="dailyInterventions/dailyIntervention"/> 
    </tr>       
</xsl:template>
<xsl:template match="episodeDetails">       
        <td><xsl:value-of select="caseNo"/></td>
        <td><xsl:value-of select="address1"/></td>
        <td><xsl:value-of select="address4"/></td>
        <td><xsl:value-of select="postcode"/></td>
        <td><xsl:value-of select="familyName"/></td>
        <td><xsl:value-of select="firstName"/></td>
        <td><xsl:value-of select="dobEst"/></td>
</xsl:template>
<xsl:template match="dailyInterventions/dailyIntervention"> 
    <xsl:apply-templates select="episodeDetails"> 
    <xsl:for-each select="dailyInterventions/dailyIntervention">
    <tr>

    <td><xsl:value-of select="activityDate"/></td>
    <td><xsl:value-of select="ecg"/></td>
    <td><xsl:value-of select="cpox"/></td>
    <td><xsl:value-of select="invVentET"/></td>
    <td><xsl:value-of select="invVentTT"/></td>
    </tr>
</xsl:for-each>
</xsl:template>

期望输出

localID caseNo  address1    address4    postcode    familyName  firstName   dobEst  activityDate    ecg cpox    invVentET   invVentTT   noCrit
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   23/08/2012  TRUE    TRUE    TRUE    FALSE   
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   24/08/2012  TRUE    TRUE    TRUE    FALSE   
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   25/08/2012  TRUE    TRUE    FALSE   FALSE   
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   26/08/2012  TRUE    TRUE    TRUE    FALSE   
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   27/08/2012  TRUE    TRUE    TRUE    FALSE   
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   28/08/2012  TRUE    TRUE    TRUE    FALSE   
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   29/08/2012  TRUE    TRUE    TRUE    FALSE   
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   30/08/2012  TRUE    TRUE    FALSE   FALSE   
P333456 0   where Person1 live  Do they live there  PP1 2PP Person1 PersonF1    0   31/08/2012  TRUE    TRUE    FALSE   FALSE   
20120410    1   where Person2 Lives Street Person2  XX1 1XX Person2 Person2 A   0   23/08/2012                  TRUE
20120411    2   Where Person3 Lives Person 3 Street WW1 1WW Person3 Person 3 B  0   24/08/2012  TRUE    TRUE    TRUE    FALSE   
20120411    2   Where Person3 Lives Person 3 Street WW1 1WW Person3 Person 3 B  0   25/08/2012  TRUE    TRUE    TRUE    FALSE   
20120411    2   Where Person3 Lives Person 3 Street WW1 1WW Person3 Person 3 B  0   26/08/2012  TRUE    TRUE    TRUE    FALSE   
20120411    2   Where Person3 Lives Person 3 Street WW1 1WW Person3 Person 3 B  0   27/08/2012  TRUE    TRUE    FALSE   FALSE   
20120411    2   Where Person3 Lives Person 3 Street WW1 1WW Person3 Person 3 B  0   28/08/2012  TRUE    TRUE    FALSE   FALSE   
20120411    2   Where Person3 Lives Person 3 Street WW1 1WW Person3 Person 3 B  0   29/08/2012  TRUE    TRUE    FALSE   FALSE   
20120418    4   where person4 lives Person4 Road    NN1 1NN Person4 Person4 B   0   24/08/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   25/08/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   26/08/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   27/08/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   28/08/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   29/08/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   30/08/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   31/08/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   01/09/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   02/09/2012  TRUE    TRUE    FALSE   FALSE   
20120419    5   Where Person5 Lives Person5 Road    SS1 1SS Person5 Person5 K   0   03/09/2012  TRUE    TRUE    FALSE   FALSE   
20120420    6   Where person6 Lives Person6 Road    EE1 EE1 Person6 Person6 N   0   23/08/2012  TRUE    TRUE    FALSE   FALSE   
20120420    6   Where person6 Lives Person6 Road    EE1 EE1 Person6 Person6 N   0   24/08/2012  TRUE    TRUE    FALSE   FALSE   
20120420    6   Where person6 Lives Person6 Road    EE1 EE1 Person6 Person6 N   0   25/08/2012  TRUE    TRUE    FALSE   FALSE   
20120420    6   Where person6 Lives Person6 Road    EE1 EE1 Person6 Person6 N   0   26/08/2012  TRUE    TRUE    FALSE   FALSE   
20120420    6   Where person6 Lives Person6 Road    EE1 EE1 Person6 Person6 N   0   27/08/2012  TRUE    TRUE    FALSE   FALSE   

2 个答案:

答案 0 :(得分:0)

以下样式表产生了我认为所需的输出:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwyzy'"/>
    <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWYZY'"/>

    <xsl:template match="/">     
        <html>    
            <xsl:apply-templates select="ClientImport"/>       
        </html> 
    </xsl:template> 

    <xsl:template match="ClientImport">     
        <table border="1">
            <tr>
                <xsl:apply-templates select="episode[1]/@localID" mode="header"/>
                <xsl:apply-templates select="episode[1]/episodeDetails[1]/*" mode="header"/>
                <xsl:apply-templates select="episode[1]/dailyInterventions[1]/dailyIntervention[1]/*" mode="header"/>
            </tr>
            <xsl:apply-templates select="episode"/>                 
        </table>         
    </xsl:template>

    <xsl:template match="@*|*" mode="header">
        <th><xsl:value-of select="translate(local-name(), $lower, $upper)"/></th>
    </xsl:template>

    <xsl:template match="episode"> 
        <xsl:apply-templates select="dailyInterventions/dailyIntervention"/>
    </xsl:template>

    <xsl:template match="@localID">
        <td><xsl:value-of select="."/></td> 
    </xsl:template>

    <xsl:template match="episodeDetails">
        <xsl:apply-templates select="../@localID"/>
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="dailyInterventions/dailyIntervention"> 
        <tr>
            <xsl:apply-templates select="../preceding-sibling::episodeDetails" />
            <xsl:apply-templates select="*"/>
        </tr>     
    </xsl:template>

    <xsl:template match="episodeDetails/* | dailyIntervention/*">
        <td><xsl:value-of select="."/></td>
    </xsl:template>

</xsl:stylesheet>

我更改了一些样板硬编码项目以使用xsl:apply-templates

例如,要生成标题,它会以创建<th>的模式为属性和元素应用模板,并输出项目的大写名称。

答案 1 :(得分:0)

我只想通过dailyIntervention元素,并为每个找到的元素插入表中。 dailyIntervention节点中不可用的所有字段都可以通过xpath的表达式访问..对于父级。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="ClientImport">
<html>
    <head>
        <title></title>
    </head>
    <body>
<table border="1">
        <tr>
            <th>LOCALID</th><th>CASENO</th><th>ADDRESS1</th><th>ADDRESS4</th><th>POSTCODE</th><th>FAMILYNAME</th><th>FIRSTNAME</th><th>DOBEST</th><th>ACTIVITYDATE</th><th>ECG</th><th>CPOX</th><th>INVVENTET</th><th>INVVENTTT</th>
        </tr>
        <xsl:apply-templates select="//dailyIntervention"/>
    </table>
    </body>
</html>
</xsl:template>

<xsl:template match="dailyIntervention">
<tr>
    <td><xsl:value-of select="./../../@localID"/></td>
    <td><xsl:value-of select="./../../episodeDetails/caseNo"/></td>
    <td><xsl:value-of select="./../../episodeDetails/address1"/></td>
    <td><xsl:value-of select="./../../episodeDetails/address4"/></td>
    <td><xsl:value-of select="./../../episodeDetails/postcode"/></td>
    <td><xsl:value-of select="./../../episodeDetails/familyName"/></td>
    <td><xsl:value-of select="./../../episodeDetails/firstName"/></td>
    <td><xsl:value-of select="./../../episodeDetails/dobEst"/></td>
    <td><xsl:value-of select="./activityDate"/></td>
    <td><xsl:value-of select="./ecg"/></td>
    <td><xsl:value-of select="./cpox"/></td>
    <td><xsl:value-of select="./invVentET"/></td>
    <td><xsl:value-of select="./invVentTT"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>