某些应用模板在 xslt 1 中未按预期工作
Some apply-templates are not working as expected in xslt 1
我的输入:
<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>
我得到的输出:<b> tag
li 内部未转换为 strong 并且 font-color 属性也未转换为样式,但 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>
我的输入:
<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>
我得到的输出:<b> tag
li 内部未转换为 strong 并且 font-color 属性也未转换为样式,但 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>