使用嵌套的XML数据在HTML表格中显示

时间:2017-08-18 11:41:13

标签: xml xslt

我正在使用XSLT将XML转换为HTML表,但仍然坚持XML有多个数据的点,如下所示

<NewDataSet>
    <Table>
        <Rating>Existing Internal Ratings</Rating>
        <Financial_Year>2011-12</Financial_Year>
        <Internal_Rating>SB-7</Internal_Rating>
        <Validation_Date>28.02.2017</Validation_Date>
        <Long_Term_Rating/>
        <Short_Term_Rating/>
        <Expiry_Date/>
        <Rating_Amount/>
    </Table>

    <Table>
        <Rating>Previous Internal Ratings</Rating>
        <Financial_Year>2010-11</Financial_Year>
        <Internal_Rating>SB-9</Internal_Rating>
        <Validation_Date>25.02.2016</Validation_Date>
        <Long_Term_Rating/>
        <Short_Term_Rating/>
        <Expiry_Date/>
        <Rating_Amount/>
    </Table>
    <Table>
        <Rating>Dynamic Rating</Rating>
        <Financial_Year/>
        <Internal_Rating>28.02.2017</Internal_Rating>
        <Validation_Date/>
        <Long_Term_Rating/>
        <Short_Term_Rating/>
        <Expiry_Date/>
        <Rating_Amount/>
    </Table>
    <Table>
        <Rating>Existing External Ratings</Rating>
        <Financial_Year/>
        <Internal_Rating/>
        <Validation_Date/>
        <Long_Term_Rating>BBB+</Long_Term_Rating>
        <Short_Term_Rating>B1</Short_Term_Rating>
        <Expiry_Date>31.03.2017</Expiry_Date>
        <Rating_Amount>125</Rating_Amount>
    </Table>
    <Table>
        <Rating>Previous External Ratings</Rating>
        <Financial_Year/>
        <Internal_Rating/>
        <Validation_Date/>
        <Long_Term_Rating>A+</Long_Term_Rating>
        <Short_Term_Rating>A2</Short_Term_Rating>
        <Expiry_Date>28.02.2016</Expiry_Date>
        <Rating_Amount>212</Rating_Amount>
    </Table>
</NewDataSet>

要显示表格中的数据,我想根据评级标记从XML获取数据。这是该XML数据中唯一存在的标识符。

我正在使用的XSLT如下所示

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" indent="no"/>                
    <xsl:template match="/">
        <html>
            <head>
                <!--<meta charset="utf-8" />-->
                <!--<title></title>-->
                <!--<style type="text/css">-->
                <style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;margin:0px auto;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:3px 11px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:3px 11px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-123c{font-weight:bold;font-family:"Times New Roman", Times, serif !important;;vertical-align:top}
.tg .tg-xnj0{font-weight:bold;font-family:"Times New Roman", Times, serif !important;;background-color:#3166ff;text-align:center}
.tg .tg-g145{font-family:"Times New Roman", Times, serif !important;;text-align:center;vertical-align:top}
.tg .tg-smzr{font-family:"Times New Roman", Times, serif !important;;vertical-align:top}
.tg .tg-gh1y{font-weight:bold;font-family:"Times New Roman", Times, serif !important;;text-align:center;vertical-align:top}
.tg .tg-20ea{font-weight:bold;font-family:"Times New Roman", Times, serif !important;;background-color:#3531ff;text-align:center}
.tg .tg-62yd{font-family:"Times New Roman", Times, serif !important;;background-color:#ffffff}
                </style>
            </head>
            <body>

                <table class="tg">
                    <tr>
                        <th class="tg-20ea" colspan="3">Internal Ratings</th>
                        <th class="tg-20ea">Dynamic Rating</th>
                        <th class="tg-62yd" rowspan="6"/>
                        <th class="tg-xnj0" colspan="3">External Ratings</th>
                    </tr>
                    <tr>
                        <td class="tg-smzr"/>
                        <td class="tg-gh1y">Existing</td>
                        <td class="tg-gh1y">Previous</td>
                        <td class="tg-smzr" rowspan="2"/>
                        <td class="tg-123c">Agency</td>
                        <td class="tg-gh1y">Existing (CARE)</td>
                        <td class="tg-gh1y">Previous (CRISIL)</td>
                    </tr>
                    <tr>
                        <td class="tg-gh1y">Financial Year</td>
                        <td class="tg-g145">2011-12</td>
                        <td class="tg-g145">2010-11</td>
                        <td class="tg-123c">Long Term Rating</td>
                        <td class="tg-g145">BBB+</td>
                        <td class="tg-g145">A+</td>
                    </tr>
                    <tr>
                        <td class="tg-gh1y" rowspan="2">Internal Rating</td>
                        <td class="tg-g145" rowspan="2">SB-7</td>
                        <td class="tg-g145" rowspan="2">SB-9</td>
                        <td class="tg-g145" rowspan="2">SB-7</td>
                        <td class="tg-123c">Short Term Rating</td>
                        <td class="tg-g145">B1</td>
                        <td class="tg-g145">A2</td>
                    </tr>
                    <tr>
                        <td class="tg-123c">Expiry Date</td>
                        <td class="tg-g145">31.03.2017</td>
                        <td class="tg-g145">28.02.2016</td>
                    </tr>
                    <tr>
                        <td class="tg-gh1y">Validation Date</td>
                        <td class="tg-g145">28.02.2017</td>
                        <td class="tg-g145">25.02.2016</td>
                        <td class="tg-g145">31.03.2016</td>
                        <td class="tg-123c">Rating Amount</td>
                        <td class="tg-g145">125</td>
                        <td class="tg-g145">212</td>
                    </tr>
                </table>

            </body>
        </html>         
    </xsl:template>                     
</xsl:stylesheet>

我想动态更改样本输出图像中未以粗体突出显示的数据。

预期输出 enter image description here

目前,我正在传递输入XML中存在的XSLT中的硬编码值。 他们是否可以根据特定标记中的数据从嵌套XML中获取其他标记数据?与提到的XML数据中的评级标签一样。

我想在下面做一些我们可以在SQL中做的事情。

SELECT Financial_Year FROM Table WHERE Rating = 'First'
SELECT Financial_Year FROM Table WHERE Rating = 'Second'

我尝试在td之一中获取值,如下所示,但在输出中返回空白。

<tr>
    <td class="tg-gh1y">Validation Date</td>
    <td class="tg-g145">28.02.2017</td>
    <td class="tg-g145">25.02.2016</td>
    <td class="tg-g145">31.03.2016</td>
    <td class="tg-123c">Rating Amount</td>
    <td class="tg-g145">125</td>
    <td class="tg-g145">
        <xsl:value-of select="NewDataSet/Table[Rating = 'First']/Internal_Rating"/>
    </td>
</tr>

使用XSLT获得预期的输出。如果有任何建议,请告诉我。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" indent="no"/>                
    <xsl:template match="/">
        <html>
            <head>
                <!--<meta charset="utf-8" />-->
                <!--<title></title>-->
                <!--<style type="text/css">-->
                <style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;margin:0px auto;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:3px 11px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:3px 11px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-123c{font-weight:bold;font-family:"Times New Roman", Times, serif !important;;vertical-align:top}
.tg .tg-xnj0{font-weight:bold;font-family:"Times New Roman", Times, serif !important;;background-color:#3166ff;text-align:center}
.tg .tg-g145{font-family:"Times New Roman", Times, serif !important;;text-align:center;vertical-align:top}
.tg .tg-smzr{font-family:"Times New Roman", Times, serif !important;;vertical-align:top}
.tg .tg-gh1y{font-weight:bold;font-family:"Times New Roman", Times, serif !important;;text-align:center;vertical-align:top}
.tg .tg-20ea{font-weight:bold;font-family:"Times New Roman", Times, serif !important;;background-color:#3531ff;text-align:center}
.tg .tg-62yd{font-family:"Times New Roman", Times, serif !important;;background-color:#ffffff}
                </style>
            </head>
            <body>

                <table class="tg">
                    <tr>
                        <th class="tg-20ea" colspan="3">Internal Ratings</th>
                        <th class="tg-20ea">Dynamic Rating</th>
                        <th class="tg-62yd" rowspan="6"/>
                        <th class="tg-xnj0" colspan="3">External Ratings</th>
                    </tr>
                    <tr>
                        <td class="tg-smzr"/>
                        <td class="tg-gh1y">Existing</td>
                        <td class="tg-gh1y">Previous</td>
                        <td class="tg-smzr" rowspan="2"/>
                        <td class="tg-123c">Agency</td>
                        <td class="tg-gh1y">Existing (CARE)</td>
                        <td class="tg-gh1y">Previous (CRISIL)</td>
                    </tr>
                    <tr>
                        <td class="tg-gh1y">Financial Year</td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Existing Internal Ratings']/Financial_Year"/></td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Previous Internal Ratings']/Financial_Year"/></td>
                        <td class="tg-123c">Long Term Rating</td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Existing External Ratings']/Long_Term_Rating"/></td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Previous External Ratings']/Long_Term_Rating"/></td>
                    </tr>
                    <tr>
                        <td class="tg-gh1y" rowspan="2">Internal Rating</td>
                        <td class="tg-g145" rowspan="2"><xsl:value-of select="NewDataSet/Table[Rating = 'Existing Internal Ratings']/Internal_Rating"/></td>
                        <td class="tg-g145" rowspan="2"><xsl:value-of select="NewDataSet/Table[Rating = 'Previous Internal Ratings']/Internal_Rating"/></td>
                        <td class="tg-g145" rowspan="2"><xsl:value-of select="NewDataSet/Table[Rating = 'Dynamic Rating']/Internal_Rating"/></td>
                        <td class="tg-123c">Short Term Rating</td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Existing External Ratings']/Short_Term_Rating"/></td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Previous External Ratings']/Short_Term_Rating"/></td>
                    </tr>
                    <tr>
                        <td class="tg-123c">Expiry Date</td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Existing External Ratings']/Expiry_Date"/></td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Previous External Ratings']/Expiry_Date"/></td>
                    </tr>
                    <tr>
                        <td class="tg-gh1y">Validation Date</td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Existing Internal Ratings']/Validation_Date"/></td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Previous Internal Ratings']/Validation_Date"/></td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Dynamic Rating']/Validation_Date"/></td>
                        <td class="tg-123c">Rating Amount</td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Existing External Ratings']/Rating_Amount"/></td>
                        <td class="tg-g145"><xsl:value-of select="NewDataSet/Table[Rating = 'Previous External Ratings']/Rating_Amount"/></td>
                    </tr>
                </table>

            </body>
        </html>         
    </xsl:template>                     
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

以下XPath表达式对应于您作为示例提供的SQL语句:

//Table[Rating = 'First']/Financial_Year

将此应用于您的输入会产生:

> xmllint --xpath "//Table[Rating = 'First']/Financial_Year" test.xml 
<Financial_Year>2011-12</Financial_Year>