0
votes

Here is the xml:

<?xml version="1.0" encoding="UTF-8"?>
<file>
    <text>
        <p>
            <sentence>I bought <fruit>kiwi</fruit> at the grocery store.</sentence>
            <sentence>I also bought <fruit>bananas</fruit> at the store.</sentence>
            <sentence base="basket">Then, I bought a basket at another store.</sentence>
            <sentence>You bought <fruit>peaches</fruit> at the grocery store.</sentence>
            <sentence>You also bought <fruit>apples</fruit> at the store.</sentence>
            <sentence>he bought <fruit>pears</fruit> at the grocery store.</sentence>
            <sentence base="basket">Then, You bought a <fruit>oranges</fruit> and a basket at another store.</sentence>
            <sentence>He also bought <fruit>lemons</fruit> at the store.</sentence>
        </p>
    </text>
</file>

Here is the xslt that needs modification:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

</xsl:template>
    <xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates select="file/text/p/sentence[fruit]”/>
        </body>
        </html>
        </xsl:template>

    <xsl:template match="sentence[fruit]”>
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>

</xsl:stylesheet>

I need to count # of sentences between <fruit> and previous and following <sentence base="basket">. For example, <fruit>apples</fruit> are two <sentence> after a <sentence base=“basket”> (-2) and two <sentence> before another <sentence base=“basket”> (+2), while <fruit>oranges</fruit> are inside a <sentence base=“basket”> (-0,+0).

Please help. Here is the output I need:

I bought kiwi at the grocery store.(none, +2)

I also bought bananas at the store.(none, +1)

You bought peaches at the grocery store. (-1, +3)

You also bought apples at the store. (-2, +2)

he bought pears the grocery store. (-3, +1)

Then, You bought a oranges and a basket at another store. (0,0)

He also bought lemons at the store. (-1, none)

1

1 Answers

1
votes

You can use this:

<?xml version="1.0" ?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="file/text/p/sentence[fruit]"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="sentence[fruit]">
        <p>
            <xsl:apply-templates/>
            <xsl:text>(</xsl:text>
            <xsl:choose>
                <xsl:when test="@base='basket'">0</xsl:when>
                <xsl:when test="not(preceding-sibling::sentence[@base='basket'])">none</xsl:when>
                <xsl:when test="preceding-sibling::sentence[@base='basket']">
                    <xsl:variable name="id" select="generate-id(preceding-sibling::sentence[@base='basket'][1])"/>
                    <xsl:value-of select="concat('-', count(preceding-sibling::sentence[not(@base='basket')][generate-id(preceding-sibling::sentence[@base='basket'][1]) = $id]) + 1)"/>
                </xsl:when>
            </xsl:choose>
            <xsl:text>,</xsl:text>
            <xsl:choose>
                <xsl:when test="@base='basket'">0</xsl:when>
                <xsl:when test="not(following-sibling::sentence[@base='basket'])">none</xsl:when>
                <xsl:when test="following-sibling::sentence[@base='basket']">
                    <xsl:variable name="id" select="generate-id(following::sentence[@base='basket'][1])"/>
                    <xsl:value-of select="concat('+', count(following-sibling::sentence[not(@base='basket')][generate-id(following-sibling::sentence[@base='basket'][1]) = $id]) + 1)"/>
                </xsl:when>
            </xsl:choose>
            <xsl:text>)</xsl:text>
        </p>
    </xsl:template>

</xsl:stylesheet>