0

Somewhat simplified, my XML looks like this:

<?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>

I need to transform this using XSLT 2.0 or 3.0 to get the following:

<?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 can be more than two senses, and span style bold can occur elsewhere, so we need to identify specifically something like tei:span[@style='bold'][matches(text(), '^\d\.')] for this.

I'm having a hard time putting this together in a stylesheet that would also extract the number for the span's text node and use it as the attribute value of the new element <sense>.

I'll be most grateful for your tips.x

Tench
  • 485
  • 3
  • 18
  • Can you extend your example to include the case where "span style bold can occur elsewhere" to show how that should be handled (assuming you don't want a `sense` element for it)? Thanks! – Tim C Feb 10 '17 at 11:52
  • I just did — span style bolds are used to make some words bold, but if they are used as sense delimiters, they always contain only a number and a period. – Tench Feb 10 '17 at 11:58

1 Answers1

2

Here is an XSLT 3.0 sample

<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>

producing the output

<?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>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110