2017-08-18 127 views
0

我使用XSLT XML轉換成HTML表,但停留在一個點XML有多個數據如下使用嵌套的XML數據,HTML表格中顯示

<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

目前我通過硬編碼值在XSLT是存在於輸入XML。 他們有什麼辦法根據特定標籤中存在的數據從嵌套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> 

回答

0

以下XPath表達式對應於你作爲一個例子給出的SQL語句:

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

將此應用於您的輸入給出:

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

是否使用下面的XSLT這項工作命令。​​

+0

原則上是的,但主要取決於您希望生成的HTML如何。請用一個示例來說明結果的外觀。並且還請顯示您迄今爲止所嘗試的內容。 – Markus

+0

我想要顯示的數據有很大的表格。我用同樣的一小部分問的問題。截至目前,我試圖使用'​​'來選擇特定數據。 –