2016-08-19 112 views
1

所以最近我一直試圖提取最簡單的數據,但XSL始終選擇第一條記錄。XML保持選擇相同的記錄

現在我已經測試選擇=「//學生」,它成功地選擇所有的數據,但是,當涉及到表中顯示出來,它我想攪亂

XML

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?> 
<school> 
<class unitId="3311"> 
<className>English</className> 
<studentList> 
<student id="1001">Lisa Simpson</student> 
<student id="1002">Barney Rubble</student> 
<student id="1003">Donald Duck</student> 
</studentList> 
</class> 
</school> 

XSL

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

<xsl:template match="/"> 
    <html> 
    <body> 
    <h2>My Students</h2> 
    <table border="1"> 
     <tr bgcolor="#9acd32"> 
     <th style="text-align:left">STUDENT</th> 
     </tr> 
     <xsl:for-each select="//student"> 
     <tr> 
     <td><xsl:value-of select="../student"/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

我保持Runni吳進

enter image description here

+0

非常感謝幫助提前:) –

+0

莫非'的'在'td'中做更有用的事情?我並不太熟悉XSLT,但是你寫給我看起來像「選擇所有'學生'子節點,然後去他們的父母並選擇第一個'學生'子節點」。這不是你想要的 - 你需要剛選擇的節點,其相對路徑是「。」。 – starturtle

+0

OMG YOU LREEND !!!!!!!我一直在以前的文檔中使用這個代碼,這是出於某種原因工作,但隨後它停止工作在這個標準,即時通訊一個完整的初學者在XML感謝花花公子 –

回答

1

你應該使用:

<xsl:value-of select="." /> 

而且最好是在換各專門選擇父節點:

<xsl:for-each select="/school/studentList/student"> 
[...] 
</xsl:for-each> 
0

中的XPath,../student,就是問題所在。對於文檔中的每個student//student),您將轉到其父項並選擇子項student s。使用xsl:value-of您將只獲得第一個值,因爲XSLT1.0僅選擇第一個元素的值。

將其更改爲 <xsl:value-of select="."/>

,以便可以選擇每一個student的當前值。