2014-11-06 71 views
0

下面是XML和XSL,我想通過完整的XML節點的XSL腳本來執行該節點上的一些操作,我不希望使用XPath和想用的SelectSingleNode中運行msxslscript。無法通過XML的XSL腳本

XML

<?xml version="1.0" ?> 
<?xml-stylesheet href="doc.xsl" type="text/xsl"?> 
<books> 
<book> 
<name>Revolution</name> 
<qty value="4">1</qty> 
</book> 
<book> 
<name>Life of a pie</name> 
<qty value="4">5</qty> 
</book> 
</books> 

XSL

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:user="com.nitish"> 
<msxsl:script language="javascript" implements-prefix="user" > 
function getNode(node){ 

return node.selectSingleNode("books/book/qty/@value"); 

} 
</msxsl:script> 

<xsl:template match="/"> 


<html> 
<body> 
<h2>Book Details</h2> 
<table xmlns:h="http://www.w3.org/TR/html4/" border="1px" cellspacing="20px"> 
<xsl:variable name="rootNode" select="books"/> 
<xsl:for-each select="//book"> 
<tr><td><xsl:value-of select="user:getNode($rootNode)"/> 
</td></tr> 
</xsl:for-each> 
</table> 
</body> 
</html> 

</xsl:template> 

</xsl:stylesheet> 

請幫助。

+0

我沒有看到你的樣品中的任何使用selectSingleNode'的'所以如果'selectSingleNode'是你面對然後顯示與樣品,詳細說明您所期望的結果,以及如何將問題你得到的不同。或者至少解釋發佈的樣本有什麼問題,即您期望獲得哪種結果以及獲得的結果有何不同。你有什麼錯誤嗎?你使用IE嗎?否則,完全不支持使用msxsl:腳本的整個方法。 – 2014-11-06 10:19:30

+0

我想使用selectSingleNode獲取qty屬性的值。 – 2014-11-06 10:47:57

回答

0

我不明白爲什麼你堅持使用擴展腳本等專有功能來簡單地選擇節點並輸出它們的值,因爲這就是XSLT和XPath的用途,但如果你需要一個例子,那麼使用IE下面適用於我:

<?xml-stylesheet type="text/xsl" href="test2014110601.xsl"?> 
<books> 
<book> 
<name>Revolution</name> 
<qty value="4">1</qty> 
</book> 
<book> 
<name>Life of a pie</name> 
<qty value="4">5</qty> 
</book> 
</books> 

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:user="http://example/com/user" 
    exclude-result-prefixes="msxsl user"> 

<msxsl:script language="javascript" implements-prefix="user"> 
function getNode(nodeSelection) { 
    return nodeSelection.item(0).selectSingleNode("book/qty/@value"); 
} 
</msxsl:script> 

<xsl:output method="html" indent="yes" version="4.01"/> 

<xsl:template match="/"> 
    <xsl:variable name="rootElement" select="books"/> 
    <html> 
    <body> 
     <h2>Book Details</h2> 
     <div> 
     <h3>XPath</h3> 
     <xsl:value-of select="$rootElement/book/qty/@value"/> 
     </div> 
     <div> 
     <h3>Script</h3> 
     <xsl:value-of select="user:getNode($rootElement)"/> 
     </div> 
    </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

在線在http://home.arcor.de/martin.honnen/xslt/test2014110601.xml。與IE

輸出
Book Details 


XPath 
4 

Script 
4 
+0

它的工作。非常感謝Martin。 – 2014-11-06 11:31:14