XSLT 标记化 - 捕获分隔符 [英] XSLT tokenize - capturing the separators

查看:32
本文介绍了XSLT 标记化 - 捕获分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一段 XSL 代码,它将一个文本标记为由interpunction 和类似字符分隔的片段.我想问一下是否有可能以某种方式捕获文本标记的字符串,例如逗号或点等.

here is a piece of code in XSL which tokenizes a text into fragments separated by interpunction and similar characters. I'd like to ask if there is a possibility to somehow capture the strings by which the text was tokenized, for example the comma or dot etc.

<xsl:stylesheet version="2.0" exclude-result-prefixes="xs xdt err fn" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:err="http://www.w3.org/2005/xqt-errors" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="GENERUJ">
    <TEXT>
        <xsl:variable name="text">
            <xsl:value-of select="normalize-space(unparsed-text(@filename, 'UTF-8'))" disable-output-escaping="yes"/>
        </xsl:variable>
        <xsl:for-each select="tokenize($text, '(\s+(&quot;|\(|\[|\{))|((&quot;|,|;|:|\s\-|\)|\]|\})\s+)|((\.|\?|!|;)&quot;?\s*)' )">
            <xsl:choose>
                <xsl:when test="string-length(.)&gt;0">
                    <FRAGMENT>
                        <CONTENT>
                            <xsl:value-of select="."/>
                        </CONTENT>
                        <LENGTH>
                            <xsl:value-of select="string-length(.)"/>
                        </LENGTH>
                    </FRAGMENT>
                </xsl:when>
                <xsl:otherwise>
                    <FRAGMENT_COUNT>
                        <xsl:value-of select="last()-1"/>
                    </FRAGMENT_COUNT>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </TEXT>
</xsl:template>

当您看到构造的标签内容、长度时,如果您明白我的意思,我想添加一个名为 SEPARATOR 的标签.我在互联网上找不到任何答案,我只是一个 xsl 转换的初学者,所以我正在寻找一个快速的解决方案.提前致谢.

As you see the constructed tags CONTENTS, LENGTH, I'd like to add one called SEPARATOR if you know what I mean. I couldnt find any answer to this on the internet and I'm just a beginner with xsl transformations so I'm looking for a quick solution. Thank you in advance.

推荐答案

tokenize() 函数不允许您发现分隔符是什么.如果您需要知道,则需要使用 xsl:analyze-string 代替.如果您使用与 tokenize() 相同的正则表达式,这会将令牌"传递给 xsl:non-matching-substring 指令,并将分隔符"传递给 xsl:matching-substring 指令.

The tokenize() function doesn't allow you to discover what the separators were. If you need to know, you will need to use xsl:analyze-string instead. If you use the same regex as for tokenize(), this passes the "tokens" to the xsl:non-matching-substring instruction and the "separators" to the xsl:matching-substring instruction.

这篇关于XSLT 标记化 - 捕获分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆