我的输入:
<li>
<span font-color="#2b91bf">FIRST FACT</span>
</li>
<li>
<span font-color="#2b91bf">SECOND FACT</span>
</li>
<li>
<span font-color="#2b91bf">THIRD FACT</span>
<b>hi</b>
</li>
<b> bold text <b>
所需输出:
<ul><li>
<span style="color:#2574A9;">FIRST FACT</span>
</li>
<li>
<span style="color:#2574A9;">SECOND FACT</span>
</li>
<li>
<span style="color:#2574A9;">THIRD FACT</span>
<strong>hi</strong>
</li>
</ul>
<strong>bold text</strong>
我得到的输出:li内的<b> tag
不转换为强,也不转换为样式,但li标签外的属性或标签被正确转换。这是因为<xsl:copy-of>
吗?
<ul><li>
<span font-color="#2b91bf">FIRST FACT</span>
</li>
<li>
<span font-color="#2b91bf">SECOND FACT</span>
</li>
<li>
<span font-color="#2b91bf">THIRD FACT</span>
<b>hi</b>
</li></ul>
<b> bold text <b>
我使用下面的xsl来处理上面的代码片段。
<xsl:template match="b">
<strong>
<xsl:apply-templates/>
</strong>
<xsl:template match="span">
<span>
<xsl:attribute name="style">
<xsl:choose>
<xsl:when test="contains(./@font-color, '#4f5967')">color:#4D4F52;</xsl:when>
<xsl:when test="contains(./@font-color, '#acb3bf')">color:#6B6E70;</xsl:when>
<xsl:when test="contains(./@font-color, '#2b91bf')">color:#2574A9;</xsl:when>
<xsl:when test="contains(./@font-color, '#0077a7')">color:#21219A;</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="contains(./@font-face, 'baskerville')"
>font-family:'EB Garamond', serif;</xsl:when>
<xsl:when test="contains(./@font-face, 'courier')"
>font-family:'Space Mono', monospace;</xsl:when>
<xsl:when test="contains(./@font-face, 'museo')"
>font-family:'Work Sans', sans-serif;</xsl:when>
<xsl:when test="contains(./@font-face, 'proxima')"
>font-family:Overlock, sans-serif;</xsl:when>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates/>
</span>
</xsl:template>
<—- below lines of code will add ul tag as parent to those li tags which neither has it’s parent as ol nor ul —->
<xsl:key name="li-group" match="*[not(self::ol | self::ul)]/li[preceding-sibling::*[1][self::li]]" use="generate-id(preceding-sibling::li[not(preceding-sibling::*[1][self::li])][1])"/>
<xsl:template match="*[not(self::ol | self::ul)]/li[not(preceding-sibling::*[1][self::li])]">
<ul>
<xsl:copy-of select=". | key('li-group', generate-id())"/>
</ul>
</xsl:template>
<xsl:template match="*[not(self::ol | self::ul)]/li[preceding-sibling::*[1][self::li]]"/>
可以用<xsl:apply-templates select=". | key('li-group', generate-id())" mode="li"/>
代替<xsl:copy-of select=". | key('li-group', generate-id())"/>
,然后
<xsl:template match="li" mode="li">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>