Split string following a pattern using XSLT 2.0

白昼怎懂夜的黑 提交于 2019-12-11 19:43:13

问题


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

Input string

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.


回答1:


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>


来源:https://stackoverflow.com/questions/33959720/split-string-following-a-pattern-using-xslt-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!