首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“交换斜体算法”换成纯XSLT,你知道吗?

“交换斜体算法”换成纯XSLT,你知道吗?
EN

Stack Overflow用户
提问于 2013-06-17 20:11:11
回答 2查看 122关注 0票数 0

使用XHTML和XML工具,有时我们需要反转斜体<i>块。示例:

代码语言:javascript
复制
 <!-- 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>

所以,看起来是这样,

  1. 又发现了几个智人化石。
  2. 又发现了几个智人化石。

有许多方法可以将“混合斜体”文本转换为“倒斜体”:What the correct algorithm to invert italics in a mixed text?.

..。但是我看不出用“纯XSLT”(没有外部处理依赖)做这件事的任何方法:,您看到了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-19 01:24:46

你好?有人会编辑?..。好的,我需要"100%的解决方案“,他们我正在添加一个,只为finesh,但不是”我的“。

@MichaelKay和@DanielHaley提供了很好的线索,并且接近精细的解决方案(!)。

XML输入

代码语言:javascript
复制
<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作为“交换斜体算法”..。到目前为止没有虫子(!)。

代码语言:javascript
复制
 <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树的终端节点。
票数 0
EN

Stack Overflow用户

发布于 2013-06-17 20:52:30

就像这样:

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17155956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档