Reputation: 385
Turning xml into html, I would like to assign a classname to an element based on information that is stored somewhere else in the xml-document. To do this, I need to insert the value of a property of the current node into an XPath. I do not know how to do this.
Given this xml:
<?xml version="1.0"?>
<Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Styles>
<Style ss:ID="cell-ID" ss:Name="classname">
</Style>
</Styles>
<Worksheet>
<Cell ss:StyleID="cell-ID">test</Cell>
</Worksheet>
</Workbook>
I have used the following XSL, but the line with the arrow pointer does not work:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
exclude-result-prefixes="ss"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:element name="style">
<xsl:for-each select="Workbook/Styles/Style">
#<xsl:value-of select="@ss:ID"/>
<xsl:if test="@ss:Name">, .<xsl:value-of select="@ss:Name" /></xsl:if> { ... }
</xsl:for-each>
</xsl:element>
<xsl:for-each select="Workbook/Worksheet/Cell">
<xsl:element name="div">
<xsl:attribute name="class">default</xsl:attribute>
<xsl:if test="@ss:StyleID">
<xsl:attribute name="id">
<xsl:value-of select="@ss:StyleID"/>
</xsl:attribute>
<xsl:attribute name="class">
==> <xsl:value-of select="//Style[@ss:ID=@ss:StyleID]/@ss:Name"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The desired output would be
<style>
#cell-ID, .classname { ... }
</style>
<div class="classname" id="cell-ID">test</div>
but the classname remains empty.
Any help is highly appreciated.
Upvotes: 0
Views: 183
Reputation: 116993
XSLT has a built-in key mechanism for resolving cross-references.
Consider this minimal example:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
exclude-result-prefixes="ss">
<xsl:key name="styl" match="Style" use="@ss:ID" />
<xsl:template match="/Workbook">
<html>
<body>
<xsl:for-each select="Worksheet/Cell">
<div class="{key('styl', @ss:StyleID)/@ss:Name}">
<xsl:value-of select="."/>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Applied to the input in your question, this will produce:
Result
<html>
<body>
<div class="classname">test</div>
</body>
</html>
Note the use of (1) literal result elements and (2) attribute value template.
Upvotes: 2