[英]How can I import or include xsl stylesheet from one xsl to another xsl?
如何將xsl樣式表從一個xsl導入或包括到另一個xsl
這是我的代碼
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
<employee id = "016">
<firstname>Aryan</firstname>
<lastname>Gupta</lastname>
<linkurl>https://www.aryanguptan.com/external</linkurl>
<salary>30000</salary>
</employee>
<employee id = "080">
<firstname>Sam</firstname>
<lastname>Alex</lastname>
<linkurl>/user/internal/080</linkurl>
<salary>10000</salary>
</employee>
<employee id = "024">
<firstname>Sara</firstname>
<lastname>Khan</lastname>
<linkurl>http://www.sarakhan.com</linkurl>
<salary>25000</salary>
</employee>
<employee id = "092">
<firstname>John</firstname>
<lastname>Samuel</lastname>
<linkurl>/user/internal/092</linkurl>
<salary>10000</salary>
</employee>
<employee id = "056">
<firstname>Peter</firstname>
<lastname>Symon</lastname>
<linkurl>www.petersymon.com</linkurl>
<salary>10000</salary>
</employee>
</class>
這是此代碼的xsl表
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:import href="globaVar.xsl" />
<xsl:template match = "/">
<xsl:apply-imports/>
<html>
<body>
<h2>Employee </h2>
<table border = "1">
<tr bgcolor = "pink">
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Link URL</th>
<th>Salary</th>
</tr>
<xsl:for-each select = "class/employee">
<tr>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<xsl:variable name="linkurl" select="linkurl" />
<xsl:choose>
<xsl:when test="$lookup/tld[starts-with($linkurl, .)]">
<td><a href="{linkurl}" target="_blank"><xsl:value-of select = "linkurl"/></a></td>
</xsl:when>
<xsl:otherwise>
<td><a href="www.google.com/{linkurl}"><xsl:value-of select = "concat('www.google.com/',linkurl)"/></a></td>
</xsl:otherwise>
</xsl:choose>
<td><xsl:value-of select = "salary"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
在這里,我正在嘗試將此代碼(GlobalVar.xsl)導入其他一些用於可重復使用目的的xsl表
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="linkURLTemplate">
<xsl:variable name="tlds">
<tld>http://</tld>
<tld>https://</tld>
<tld>www.</tld>
</xsl:variable>
<xsl:variable name="lookup" select="document('')//xsl:variable[@name='tlds']"/>
</xsl:template>
</xsl:stylesheet>
但是,當我嘗試運行employee.xml時,出現此錯誤
“ XSLT轉換期間發生錯誤:發生未知錯誤()”
你可以喂我做錯了嗎?
您的樣式表中有兩個錯誤:
您在Employee.xsl
子樣式表名稱錯誤。 所以換行
<xsl:import href="globaVar.xsl" />
至
<xsl:import href="GlobalVar.xsl" />
在子樣式表GlobalVar.xsl
您沒有在頂層定義變量,而是在模板中定義了變量。 但是,您必須在頂層定義它們才能在其他模板中使用它們。 因此,從GlobalVar.xsl
刪除xsl:template
看起來像這樣:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="tlds"> <tld>http://</tld> <tld>https://</tld> <tld>www.</tld> </xsl:variable> <xsl:variable name="lookup" select="document('')/xsl:stylesheet/xsl:variable[@name='tlds']"/> </xsl:stylesheet>
在這里,我還將lookup
變量的定義更改為
<xsl:variable name="lookup" select="document('')/xsl:stylesheet/xsl:variable[@name='tlds']"/>
以更直接地訪問此變量。
現在,您的樣式表應該可以按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.