XSL 转换:select XPath 属性 值基于当前节点 属性 值
XSL Transformation: select XPath property value based on current nodes property value
将 xml 变成 html,我想根据存储在 xml 文档中其他地方的信息为元素分配一个类名。为此,我需要将当前节点的 属性 的值插入到 XPath 中。我不知道该怎么做。
鉴于此 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>
我已经使用了下面的XSL,但是带有箭头指针的那一行不起作用:
<?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>
所需的输出将是
<style>
#cell-ID, .classname { ... }
</style>
<div class="classname" id="cell-ID">test</div>
但是类名仍然是空的。
非常感谢任何帮助。
XSLT 有一个内置的 key 机制来解析交叉引用。
考虑这个最小的例子:
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>
应用于您问题中的输入,这将产生:
结果
<html>
<body>
<div class="classname">test</div>
</body>
</html>
注意(1)的用法literal result elements and (2) attribute value template。
将 xml 变成 html,我想根据存储在 xml 文档中其他地方的信息为元素分配一个类名。为此,我需要将当前节点的 属性 的值插入到 XPath 中。我不知道该怎么做。
鉴于此 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>
我已经使用了下面的XSL,但是带有箭头指针的那一行不起作用:
<?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>
所需的输出将是
<style>
#cell-ID, .classname { ... }
</style>
<div class="classname" id="cell-ID">test</div>
但是类名仍然是空的。
非常感谢任何帮助。
XSLT 有一个内置的 key 机制来解析交叉引用。
考虑这个最小的例子:
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>
应用于您问题中的输入,这将产生:
结果
<html>
<body>
<div class="classname">test</div>
</body>
</html>
注意(1)的用法literal result elements and (2) attribute value template。