Algorithm &引用;“交换斜体算法”;对于纯XSLT,您知道一个吗?

Algorithm &引用;“交换斜体算法”;对于纯XSLT,您知道一个吗?,algorithm,xslt,xhtml,Algorithm,Xslt,Xhtml,使用XHTML和XML工具时,有时我们需要反转斜体块。例如: 又发现了几个智人化石 又发现了几个智人化石 所以,看起来像这样 又发现了几个智人化石 又发现了几个智人化石 有很多方法可以将“混合斜体”文本转换为“倒斜体”:请参见 。。。但是我看不到任何使用“纯XSLT”(没有外部处理依赖项)的方法:您看到了吗?我不确定这是否涵盖所有情况,但您可以这样做: XML输入 <html> <!-- original text: --> <p id="p

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


又发现了几个智人化石

又发现了几个智人化石

所以,看起来像这样

  • 又发现了几个智人化石

  • 又发现了几个智人化石


  • 有很多方法可以将“混合斜体”文本转换为“倒斜体”:请参见


    。。。但是我看不到任何使用“纯XSLT”(没有外部处理依赖项)的方法:您看到了吗?我不确定这是否涵盖所有情况,但您可以这样做:

    XML输入

    <html>
        <!-- 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>
        <p>Leave me alone!</p>
        <p><b><i>O</i>RIGINAL <big><i>with italics</i> and </big> withOUT</b></p>
    </html>
    
    <html>
       <!-- original text: -->
       <p id="p1">Several more<i> Homo sapiens </i>fossils were discovered<i>.</i></p>
       <!-- same text, swapping italics: -->
       <p id="p2"><i>Several more </i>Homo sapiens<i> fossils were discovered.</i></p>
       <p>Leave me alone!</p>
       <p><b>O<i>RIGINAL </i><big>with italics<i> and </i></big><i> withOUT</i></b></p>
    </html>
    
    <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

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="*[i]">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="node()" mode="swapItal"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="text()" mode="swapItal" priority="1">
            <i><xsl:value-of select="."/></i>
        </xsl:template>
    
        <xsl:template match="i" mode="swapItal">
            <xsl:apply-templates/>
        </xsl:template>
    
        <xsl:template match="@*|node()" mode="swapItal">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" mode="swapItal"/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    
    
    
    XML输出

    <html>
        <!-- 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>
        <p>Leave me alone!</p>
        <p><b><i>O</i>RIGINAL <big><i>with italics</i> and </big> withOUT</b></p>
    </html>
    
    <html>
       <!-- original text: -->
       <p id="p1">Several more<i> Homo sapiens </i>fossils were discovered<i>.</i></p>
       <!-- same text, swapping italics: -->
       <p id="p2"><i>Several more </i>Homo sapiens<i> fossils were discovered.</i></p>
       <p>Leave me alone!</p>
       <p><b>O<i>RIGINAL </i><big>with italics<i> and </i></big><i> withOUT</i></b></p>
    </html>
    
    <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>
    
    
    

    又发现了几个智人化石

    又发现了几个智人化石

    别管我

    原件,斜体,无


    渲染输入

    又发现了几个智人化石

    又发现了几个智人化石

    别管我

    原件,斜体,无


    渲染输出

    又发现了几个智人化石

    又发现了几个智人化石

    别管我

    原件,斜体,无

    类似这样的内容:

    <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>
    

    喂?其他人将编辑。。。好的,我需要“100%的解决方案”,我正在添加一个,仅用于finesh,但不是“我的”

    @MichaelKay和@DanielHaley提供了很好的线索和近乎完美的解决方案(!)

    XML输入

    <html>
        <!-- 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>
        <p>Leave me alone!</p>
        <p><b><i>O</i>RIGINAL <big><i>with italics</i> and </big> withOUT</b></p>
    </html>
    
    <html>
       <!-- original text: -->
       <p id="p1">Several more<i> Homo sapiens </i>fossils were discovered<i>.</i></p>
       <!-- same text, swapping italics: -->
       <p id="p2"><i>Several more </i>Homo sapiens<i> fossils were discovered.</i></p>
       <p>Leave me alone!</p>
       <p><b>O<i>RIGINAL </i><big>with italics<i> and </i></big><i> withOUT</i></b></p>
    </html>
    
    <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显示出更好的结果(只有
    别管我!

    没有反转),但是@MichaelKay的解决方案更优雅:我将两者合并以产生这个“100%解决方案”。现在我在我的系统上使用这个XSLT作为“交换斜体算法”。。。到现在为止没有bug(!)

    
    
    在复制过程中概述为“按事件驱动算法”:

    • 移除
      i
      标记:将“
      东西
      的任何东西复制为“东西”

    • 包含
      i
      标记:当文本不在“斜体父项(或另一祖先)”上下文中时,将任何文本复制为“
      文本
      ”。PS:text是DOM树的终端节点


    您确定移动
    i
    元素是正确的解决方案吗?在CSS级别做不是更好吗?是的,我的问题更大,看。。。“CSS级别问题”(!),另请参阅。更新了Good的更复杂示例,非常感谢!它运行,我尝试在这里和这里使用
    深度标记

    ,唯一的问题是丢失了一个产生“hereand”的空间(可能是
    xsl:strip-space
    问题)。问:为什么“别管我!”不是倒过来的?我看不出段落的定义是“检查斜体字的单位”。@PeterKrauss-“别管我!”没有倒转,因为没有斜体字。我想要一个没有任何内容的
    ,以确保它不会被修改。我将看一看你的例子,看看我是否能理解为什么空间会被删除。@PeterKrauss-是的,你是对的。这是
    xsl:strip空间
    。您可以添加以下内容:
    ,它会很好地工作。非常感谢您提供的线索,这表明
    not(祖先::i)
    选择器和身份转换是基础。我想这就是XSLT1。。。不幸的是,它不是在没有修改的情况下运行的,它只生成文本值,没有标记。我正在测试我的适应能力。。。