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>

Тhere 可以有两种以上的感觉,span style bold 可以出现在其他地方,所以我们需要专门识别类似tei:span[@style='bold'][matches(text(), '^\d\.')]的东西。

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

我将非常感谢您的tips.x

4

1 回答 1

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>
于 2017-02-10T11:58:00.807 回答