我有一個關於XSLT XPath性能的問題。如果我們看下面的例子。在我的場景中,什麼是最好的XSLT XPath性能?
哪個下列選項中具有最佳的性能:
<xsl:for-each select="//cd">
或
<xsl:for-each select="//catalog/cd">
XML數據:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
</cd>
</catalog>
<catalog>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
</cd>
</catalog>
</root>
XSLT樣式表:
<?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 CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="//cd">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="artist" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
這是最好的表現嗎?
(我做了這個小的一個簡單的例子,在我的實際情況下,我有很多更復雜的數據意象結構)
您的輸入文檔不是格式良好的XML。請編輯你的問題。 –
感謝您的輸入,我糾正了這一點:) –
通常,顯式路徑比使用後代軸更有效率。 –