使用XHTML和XML工具,有时我们需要反转斜体<i>块。示例:
<!-- original text: -->
<p id="p1"><i>Several more</i> Homo sapiens <i>fossils were discovered</i>.</p>
<!-- same text, swapping italics: -->
<p id="p2">Several more <i>Homo sapiens</i> fossils were discovered.</p>所以,看起来是这样,
有许多方法可以将“混合斜体”文本转换为“倒斜体”:,,What the correct algorithm to invert italics in a mixed text?.
..。但是我看不出用“纯XSLT”(没有外部处理依赖)做这件事的任何方法:,您看到了吗?
发布于 2013-06-19 01:24:46
你好?有人会编辑?..。好的,我需要"100%的解决方案“,他们我正在添加一个,只为finesh,但不是”我的“。
@MichaelKay和@DanielHaley提供了很好的线索,并且接近精细的解决方案(!)。
XML输入
<html>
<p><i>Several more</i> Homo sapiens <i>fossils were discovered</i>.</p>
<p>Several more <i>Homo sapiens</i> fossils were discovered.</p>
<p>Leave me alone!</p>
<p><b><i>F</i>RAGMENT <big><i>with italics</i> and </big> withOUT</b></p>
<p><i><sup><sub><sup><sub>Deep tags</sub></sup></sub></sup> here</i>
<big><b><small>and here</small></b></big>!
</p>
</html>XSLT1.0实现
@DanielHaley显示了更好的结果(只有<p>Leave me alone!</p>没有倒置),但是@MichaelKay的解决方案更优雅:我将两者合并以产生"100%的解决方案“。现在我在我的系统上使用这个XSLT作为“交换斜体算法”..。到目前为止没有虫子(!)。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="p"/>
<xsl:template match="@*|node()"> <!-- copy all -->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="i"> <!-- remove tag i -->
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()[not(ancestor::i)]"> <!-- inlcude tag i -->
<i><xsl:copy-of select="."/></i>
</xsl:template>
</xsl:stylesheet>作为复制过程中的“按事件驱动算法”概述:
i标签:将"<i> thing </i>“中的任何东西复制为”thing“。i标记:将任何文本复制为"<i> text </i>",当文本没有进入“斜体父母(或其他祖先)”的上下文中时。文本是DOM树的终端节点。发布于 2013-06-17 20:52:30
就像这样:
<xsl:template match="i" mode="invert-italic">
<xsl:apply-templates mode="invert-italic"/>
</xsl:template>
<xsl:template match="text()[not(ancestor::i)]" mode="invert-italic">
<i><xsl:copy-of select="."/></i>
</xsl:template>
<xsl:template match="node()" mode="invert-italic">
<xsl:copy>
<xsl:copy select="@*"/>
<xsl:apply-templates mode="invert-italic"/>
</xsl:copy>
</xsl:template>https://stackoverflow.com/questions/17155956
复制相似问题