首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSLT将省略符号的变化更改为标准

使用XSLT将省略符号的变化更改为标准
EN

Stack Overflow用户
提问于 2014-08-01 16:28:31
回答 1查看 270关注 0票数 1

我需要用一个特定的(排印正确的)版本来替换省略号的所有变体。这个符号可以出现在几乎任何文本节点中。它可以是“…”或者“.”或"…“(HTML实体)有空格/字符/标签结束之前/之后。

布局的最佳方式是点和细空间:“. . ”。

下面的解决方案查找测试文件中的所有省略号(并正确修正它们),除非每个节点有多个省略号(最后一段标记)。那么近,却又那么遥远。

由于无法清楚地看到它是否将空格改为细空格,所以我添加了文本行。显然,在最后的解决方案中,这个问题将被删除。

XSLT:

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">

<!-- Identity template for all other elements and attributes. -->
<xsl:template match="@*|node()" name="default" mode="#all">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates mode="#current"/>
    </xsl:copy>
</xsl:template>


<xsl:template match="text()[matches(.,'\.\s?\.\s?\.')]">
    <xsl:analyze-string select="." regex="(\w?\.?)\s?\.\s?\.\s?\.\s?(\w?)">
        <xsl:matching-substring>
            <xsl:if test="regex-group(1)"><xsl:value-of select="regex-group(1)"/><xsl:text>&#x2009;</xsl:text></xsl:if>
            <xsl:text>.&#x2009;.&#x2009;.</xsl:text>
            <xsl:text>[[FIXED TEXT]]</xsl:text>
            <xsl:if test="regex-group(2)"><xsl:text>&#x2009;</xsl:text><xsl:value-of select="regex-group(2)"/></xsl:if>
        </xsl:matching-substring>
        <xsl:non-matching-substring><xsl:value-of select="current()"/></xsl:non-matching-substring>
    </xsl:analyze-string>
<!--        <xsl:value-of select="replace(.,'(\w?\.?)\s?\.\s?\.\s?\.\s?(\w?)', '$1&#x2009;.&#x2009;.&#x2009;.&#x2009;$2')"/>-->
</xsl:template>

<xsl:template match="text()[matches(.,'&#x2026;')]">
    <xsl:analyze-string select="." regex="(\w?\.?)\s?&#x2026;\s?(\w?)">
        <xsl:matching-substring>
            <xsl:if test="regex-group(1)"><xsl:value-of select="regex-group(1)"/><xsl:text>&#x2009;</xsl:text></xsl:if>
            <xsl:text>.&#x2009;.&#x2009;.</xsl:text>
            <xsl:text>[[FIXED SYM]]</xsl:text>
            <xsl:if test="regex-group(2)"><xsl:text>&#x2009;</xsl:text><xsl:value-of select="regex-group(2)"/></xsl:if>
        </xsl:matching-substring>
        <xsl:non-matching-substring><xsl:value-of select="current()"/></xsl:non-matching-substring>
    </xsl:analyze-string>
<!--        <xsl:value-of select="replace(.,'(\w?\.?)\s?&#x2026;\s?(\w?)', '$1&#x2009;.&#x2009;.&#x2009;.&#x2009;$2')"/>-->
</xsl:template>

</xsl:stylesheet>

XML测试文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<sec>
    <label>1</label><title>Introduction . . . </title>
    <p>Ellipsis <italic>Correct</italic> (periods and thin spaces):&#x2009;.&#x2009;.&#x2009;.&#x2009;text</p>
    <p>Ellipsis (periods and spaces): . . . text</p>
    <p>What about periods. . .with no spaces around?</p>
    <p>. . . starts paragraph</p>
    <p>text ends paragraph. . . .</p>
    <p>This is typical text ending a sentence ending in a period. . . . New sentence</p>
    <p>Ellipsis (just periods): ... text</p>
    <p>No...spaces around ellipsis.</p>
    <p>...No spaces start</p>
    <p>ends paragraph....</p>
    <p>para end....No spaces</p>
    <p>Ellipsis (symbol): &#x2026; text</p>
    <p>Middle of text&#x2026;with no space</p>
    <p>Ellipsis followed by punctuation&#x2026;.</p>
    <p>No spaces ending para with period.&#x2026;</p>
    <p>ending para with period and space. &#x2026;</p>
    <p>&#x2026;Start of paragraph</p>
    <p>&#x2026; Start para with space</p>
    <p>end of paragraph&#x2026;</p>
    <p>end of para with space &#x2026;</p>
    <p>Multiple things &#x2026; within the same . . . paragraph?...to see if it works. ... And what about a ...? Question or ...! Exclamation point?</p>
</sec>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-01 16:53:04

我认为

代码语言:javascript
复制
<xsl:template match="text()[matches(.,'&#x2026;') or matches(.,'\.\s?\.\s?\.')]">
    <xsl:analyze-string select="replace(., '&#x2026;', '&#x2009;.&#x2009;.&#x2009;.&#x2009;')" regex="(\w?\.?)\s?\.\s?\.\s?\.\s?(\w?)">
        <xsl:matching-substring>
            <xsl:if test="regex-group(1)"><xsl:value-of select="regex-group(1)"/><xsl:text>&#x2009;</xsl:text></xsl:if>
            <xsl:text>.&#x2009;.&#x2009;.</xsl:text>
            <xsl:text>[[FIXED TEXT]]</xsl:text>
            <xsl:if test="regex-group(2)"><xsl:text>&#x2009;</xsl:text><xsl:value-of select="regex-group(2)"/></xsl:if>
        </xsl:matching-substring>
        <xsl:non-matching-substring><xsl:value-of select="."/></xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>

应该表现出将两种替换结合在一起。

或者您可以简单地在变量中的analyze string上运行,然后执行第二个操作:

代码语言:javascript
复制
<xsl:template match="text()[matches(.,'&#x2026;') or matches(.,'\.\s?\.\s?\.')]">
 <xsl:variable name="rep1">
    <xsl:analyze-string select="." regex="(\w?\.?)\s?&#x2026;\s?(\w?)">
        <xsl:matching-substring>
            <xsl:if test="regex-group(1)"><xsl:value-of select="regex-group(1)"/><xsl:text>&#x2009;</xsl:text></xsl:if>
            <xsl:text>.&#x2009;.&#x2009;.</xsl:text>
            <xsl:text>[[FIXED SYM]]</xsl:text>
            <xsl:if test="regex-group(2)"><xsl:text>&#x2009;</xsl:text><xsl:value-of select="regex-group(2)"/></xsl:if>
        </xsl:matching-substring>
        <xsl:non-matching-substring><xsl:value-of select="current()"/></xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:variable>
    <xsl:analyze-string select="$rep1" regex="(\w?\.?)\s?\.\s?\.\s?\.\s?(\w?)">
        <xsl:matching-substring>
            <xsl:if test="regex-group(1)"><xsl:value-of select="regex-group(1)"/><xsl:text>&#x2009;</xsl:text></xsl:if>
            <xsl:text>.&#x2009;.&#x2009;.</xsl:text>
            <xsl:text>[[FIXED TEXT]]</xsl:text>
            <xsl:if test="regex-group(2)"><xsl:text>&#x2009;</xsl:text><xsl:value-of select="regex-group(2)"/></xsl:if>
        </xsl:matching-substring>
        <xsl:non-matching-substring><xsl:value-of select="current()"/></xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25084720

复制
相关文章

相似问题

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