使用XSLT 2.0按照模式分割字符串 [英] Split string following a pattern using XSLT 2.0

查看:103
本文介绍了使用XSLT 2.0按照模式分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要使用XSLT 2.0解析的字符串

I have a string that needs to be parsed using XSLT 2.0

输入字符串

Hoffmann, Rüdiger (Universtiy-A, SomeCity, (SomeCountry); University-B, SomeCity, (SomeCountry)); Author, X; Author, B. (University-C, SomeCity (SomeCountry))

预期输出
Hoffmann, Rüdiger (Universtiy-A, SomeCity, (SomeCountry); University-B, SomeCity, (SomeCountry))
Author, X
Author, B. (University-C, SomeCity (SomeCountry))

Expected output
Hoffmann, Rüdiger (Universtiy-A, SomeCity, (SomeCountry); University-B, SomeCity, (SomeCountry))
Author, X
Author, B. (University-C, SomeCity (SomeCountry))

结构为-作者姓名,后跟他的大学.但是,一位作者可能拥有两所大学.大学之间和两组作者之间的分隔符是相同的. (在这种情况下为分号).

The structure is - author name, followed by his university. But, one author could have two universities. And the delimiter between universities and between two sets of author is the same one. (semi-colon in this case).

我需要基于作者隶属组的分隔符对它进行拆分,而忽略隶属之间的分号.

I need to split it based on the delimiter for author-affiliation group, ignoring the semicolon between affiliations.

我相信可以在正则表达式的帮助下完成此工作,但是我自己构建正则表达式的经验不足.

I believe it can be done with the help of regex, but I have not much experience building regex myself.

推荐答案

只要大学列表和全国各地的括号始终存在,您就可以在它们上进行匹配:

As long as the parentheses around the list of universities and around the country are always present you could match on them:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="xs mf">

    <xsl:output method="text"/>
    <xsl:param name="authors">Author, A. (Universtiy-A, SomeCity, (SomeCountry); University-B, SomeCity, (SomeCountry));Author, B. (University-C, SomeCity (SomeCountry))</xsl:param>

    <xsl:template match="/">
        <xsl:value-of select="mf:split($authors)" separator="&#10;"/>
    </xsl:template>

    <xsl:function name="mf:split" as="xs:string*">
        <xsl:param name="input" as="xs:string"/>
        <xsl:analyze-string select="$input" regex="[^;)]*?\([^(]*?\([^(]*?\)\)">
            <xsl:matching-substring>
                <xsl:sequence select="."/>
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:function>
</xsl:transform>

这篇关于使用XSLT 2.0按照模式分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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