【发布时间】:2014-03-23 08:55:24
【问题描述】:
我正在尝试使用 xslt 将 XML 文档更改为 xsl:fo。在 XML 的某些元素节点中有 HTML 样式属性,如属性节点,我想将其值更改为单独的属性节点
XML 示例是...
<books>
<book>
<price style="mycolor:red;myvalign:center;">10</price>
</book>
</books>
我想这样改
<books>
<book>
<price mycolor="red" myvalign="center">10</price>
</book>
</books>
我可以标记 'style' 属性节点的值(在此处从 stackoverflow 获得帮助)并将其呈现为键值对,但我不知道如何将这些键值对设置为调用节点的属性模板。
以下是我尝试过的 xslt,但有错误。你能帮帮我吗?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:template match="books">
<xsl:element name="books">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="book">
<xsl:element name="book">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="price">
<xsl:variable name="style" select="./@style" />
<xsl:variable name="result">
<xsl:call-template name="tokenize">
<xsl:with-param name="style" select="$style" />
</xsl:call-template>
</xsl:variable>
<xsl:element name="price">
<xsl:value-of select="$result"/>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="style" />
<xsl:variable name="styleFrag" select="substring-before($style, ';')" />
<xsl:if test="$styleFrag">
<xsl:variable name="styleKey" select="substring-before($styleFrag, ':')" />
<xsl:variable name="styleVal" select="substring-after($styleFrag, ':')" />
<xsl:variable name="concat1"><xsl:attribute name='</xsl:variable>
<xsl:variable name="concat2">'><xsl:value-of select='</xsl:variable>
<xsl:variable name="concat3">' /></xsl:attribute></xsl:variable>
<xsl:variable name="concatted" select="concat($concat1, $styleKey, $concat2, $styleVal, $concat3)" />
<xsl:value-of select="$concatted" />
<xsl:call-template name="tokenize">
<xsl:with-param name="style" select="substring-after($style,';')" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
谢谢。
【问题讨论】: