[英]Creating attribute nodes from HTML style attribute value by XSL
我正在嘗試使用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>
謝謝。
更改
<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:template match="price">
<xsl:variable name="style" select="./@style" />
<price>
<xsl:call-template name="tokenize">
<xsl:with-param name="style" select="$style" />
</xsl:call-template>
<xsl:value-of select="."/>
</price>
</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:attribute name="{normalize-space($styleKey)}">
<xsl:value-of select="$styleValue"/>
</xsl:attribute>
<xsl:call-template name="tokenize">
<xsl:with-param name="style" select="substring-after($style,';')" />
</xsl:call-template>
</xsl:if>
</xsl:template>
未經測試,但應該顯示方法。
這不是更簡單嗎?
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@style">
<xsl:call-template name="tokenize-style">
<xsl:with-param name="style" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize-style">
<xsl:param name="style"/>
<xsl:if test="contains($style, ';')">
<xsl:variable name="declaration" select="substring-before($style, ';')" />
<xsl:attribute name="{normalize-space(substring-before($declaration, ':'))}">
<xsl:value-of select="substring-after($declaration, ':')"/>
</xsl:attribute>
<!-- recursive call -->
<xsl:call-template name="tokenize-style">
<xsl:with-param name="style" select="substring-after($style, ';')" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.