我需要用一个特定的(排印正确的)版本来替换省略号的所有变体。这个符号可以出现在几乎任何文本节点中。它可以是“…”或者“.”或"…“(HTML实体)有空格/字符/标签结束之前/之后。
布局的最佳方式是点和细空间:“. . ”。
下面的解决方案查找测试文件中的所有省略号(并正确修正它们),除非每个节点有多个省略号(最后一段标记)。那么近,却又那么遥远。
由于无法清楚地看到它是否将空格改为细空格,所以我添加了文本行。显然,在最后的解决方案中,这个问题将被删除。
XSLT:
<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> </xsl:text></xsl:if>
<xsl:text>. . .</xsl:text>
<xsl:text>[[FIXED TEXT]]</xsl:text>
<xsl:if test="regex-group(2)"><xsl:text> </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 . . . $2')"/>-->
</xsl:template>
<xsl:template match="text()[matches(.,'…')]">
<xsl:analyze-string select="." regex="(\w?\.?)\s?…\s?(\w?)">
<xsl:matching-substring>
<xsl:if test="regex-group(1)"><xsl:value-of select="regex-group(1)"/><xsl:text> </xsl:text></xsl:if>
<xsl:text>. . .</xsl:text>
<xsl:text>[[FIXED SYM]]</xsl:text>
<xsl:if test="regex-group(2)"><xsl:text> </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?(\w?)', '$1 . . . $2')"/>-->
</xsl:template>
</xsl:stylesheet>XML测试文件:
<?xml version="1.0" encoding="UTF-8"?>
<sec>
<label>1</label><title>Introduction . . . </title>
<p>Ellipsis <italic>Correct</italic> (periods and thin spaces): . . . 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): … text</p>
<p>Middle of text…with no space</p>
<p>Ellipsis followed by punctuation….</p>
<p>No spaces ending para with period.…</p>
<p>ending para with period and space. …</p>
<p>…Start of paragraph</p>
<p>… Start para with space</p>
<p>end of paragraph…</p>
<p>end of para with space …</p>
<p>Multiple things … within the same . . . paragraph?...to see if it works. ... And what about a ...? Question or ...! Exclamation point?</p>
</sec>发布于 2014-08-01 16:53:04
我认为
<xsl:template match="text()[matches(.,'…') or matches(.,'\.\s?\.\s?\.')]">
<xsl:analyze-string select="replace(., '…', ' . . . ')" 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> </xsl:text></xsl:if>
<xsl:text>. . .</xsl:text>
<xsl:text>[[FIXED TEXT]]</xsl:text>
<xsl:if test="regex-group(2)"><xsl:text> </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上运行,然后执行第二个操作:
<xsl:template match="text()[matches(.,'…') or matches(.,'\.\s?\.\s?\.')]">
<xsl:variable name="rep1">
<xsl:analyze-string select="." regex="(\w?\.?)\s?…\s?(\w?)">
<xsl:matching-substring>
<xsl:if test="regex-group(1)"><xsl:value-of select="regex-group(1)"/><xsl:text> </xsl:text></xsl:if>
<xsl:text>. . .</xsl:text>
<xsl:text>[[FIXED SYM]]</xsl:text>
<xsl:if test="regex-group(2)"><xsl:text> </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> </xsl:text></xsl:if>
<xsl:text>. . .</xsl:text>
<xsl:text>[[FIXED TEXT]]</xsl:text>
<xsl:if test="regex-group(2)"><xsl:text> </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>https://stackoverflow.com/questions/25084720
复制相似问题