0
一個唯一的ID查找下面XSL以下行:XSL不生成跨度
<xsl:variable name="id" select="concat('CDTableCell', position())" />
<span id="{$id}"
我以後變換沒獲得一個唯一的ID爲每個跨度。有任何想法嗎?顯示相同的ID。
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>
<head>
<script>
function mycolorcontrastfx(bgColor)
{
return '#000000';
}
</script>
</head>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title:
// Credit for help: http://stackoverflow.com/a/43546704/139698
// As well as W3Schools
<xsl:variable name="id" select="concat('CDTableCell', position())" />
<span id="{$id}" style="background-color:#ff0000">
<xsl:value-of select="."/>
</span>
<br />
<script>
var tdElem = document.getElementById('<xsl:value-of select="$id" />')
var bgColor = tdElem.style.backgroundColor;
var textColor = mycolorcontrastfx(bgColor);
tdElem.style.color = textColor;
</script>
</xsl:template>
</xsl:stylesheet>
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="blackorwhite.xslt"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
</cd>
<cd>
<title>Hide your heart</title>
</cd>
</catalog>