XSLT选择和转换节点(使用正则表达式匹配)和跟随兄弟节点直到下一个类似节点

时间:2017-02-10 11:15:35

标签: xslt xslt-2.0 xslt-3.0

有点简化,我的XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<dict>
    <entry>
        <form>word</form>
        <gram>noun</gram>
        <span style="bold">1.</span>
        <def>this is a definition in the first sense.</def> – <cit type="example">
            <quote>This is a <span style="bold">quote</span> for the first sense. </quote>
        </cit>
        <span style="bold">2.</span>
        <def>This is a definition for the second sense</def> – <cit type="example">
            <quote>This is a quote for the second sense.</quote>
        </cit>
    </entry>    
</dict>

我需要使用XSLT 2.0或3.0对其进行转换以获得以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<dict>
    <entry>
        <form>word</form>
        <gram>noun</gram>
        <sense n="1">
            <def>this is a definition in the first sense.</def> – <cit type="example">
                <quote>This is a <span style="bold">quote</span> for the first sense. </quote>
            </cit>
        </sense>
        <sense n="2">
            <def>This is a definition for the second sense</def> – <cit type="example">
                <quote>This is a quote for the second sense.</quote>
            </cit>
        </sense>
    </entry>
</dict>

可以有两种以上的感官,并且span样式粗体可以在其他地方出现,因此我们需要为此特别识别类似tei:span[@style='bold'][matches(text(), '^\d\.')]的内容。

我很难将它放在一个样式表中,该样式表还会提取span文本节点的数字,并将其用作新元素<sense>的属性值。

我非常感谢你的提示。

1 个答案:

答案 0 :(得分:2)

这是一个XSLT 3.0示例

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:output indent="yes"/>

    <xsl:template match="entry">
        <xsl:copy>
            <xsl:for-each-group select="node()" group-starting-with="span[@style = 'bold'][matches(., '^[0-9]+\.$')]">
                <xsl:choose>
                    <xsl:when test="self::span[@style = 'bold'][matches(., '^[0-9]+\.$')]">
                        <sense nr="{replace(., '[^0-9]+', '')}">
                            <xsl:apply-templates select="current-group() except ."/>
                        </sense>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

产生输出

<?xml version="1.0" encoding="UTF-8"?>
<dict>
    <entry>
        <form>word</form>
        <gram>noun</gram>
        <sense nr="1">
        <def>this is a definition in the first sense.</def> – <cit type="example">
            <quote>This is a <span style="bold">quote</span> for the first sense. </quote>
        </cit>
        </sense>
        <sense nr="2">
        <def>This is a definition for the second sense</def> – <cit type="example">
            <quote>This is a quote for the second sense.</quote>
        </cit>
    </sense>
    </entry>    
</dict>