xsl-list
[Top] [All Lists]

Re: [xsl] XSLT 2.0 grouping and text nodes and mixed text

2009-12-08 21:26:53
At 2009-12-08 16:52 -0700, Kenneth Reid Beesley wrote:
Relative newbie question:  XSLT 2.0, grouping with text nodes and
mixed text

For a newbie you've done *very* well and you could have fooled me. You had only one small oversight.

I'm trying to convert a non-XML dictionary into XML. I've got it mostly done, but I want to input elements that currently look like
...
and transform them into this
...
i.e. each original <hop> element in <examples> is followed by plain text or mixed text, not containing <hop> or <examples> elements. I want to group the nodes after each <hop> element and wrap them in <eng>...</eng> tags. I tried the following, and a few other variations, but the desired <eng> element comes out an empty <eng/>

Yes, because you failed to include the text nodes in the population being grouped.

        <xsl:template match="examples">
                <examples>
                <xsl:for-each-group select="*" group-starting-with="hop">

Simply change the above to select="node()" in order to select all node children and not just element children.

Actually, there is an edge case involved because of the very first empty text node. I've added an <xsl:if> to the loop so that any content before the very first <hop> is ignored. The running example is below.

I hope this helps.

. . . . . . . . . . . Ken

T:\ftemp>type beesley.xml
<examples>
   <hop>source sentence 1</hop>
   translation of source sentence 1
   <hop>source sentence 2</hop>
   translation of source sentence 2
   <hop>source sentence 3</hop>
   a translation can be <emph>mixed</emph> text
</examples>

T:\ftemp>xslt2 beesley.xml beesley.xsl
<?xml version="1.0" encoding="UTF-8"?>
<examples>
   <example-pair>
      <hop>source sentence 1</hop>
      <eng>
   translation of source sentence 1
   </eng>
   </example-pair>
   <example-pair>
      <hop>source sentence 2</hop>
      <eng>
   translation of source sentence 2
   </eng>
   </example-pair>
   <example-pair>
      <hop>source sentence 3</hop>
      <eng>
   a translation can be <emph>mixed</emph> text
</eng>
   </example-pair>
</examples>
T:\ftemp>type beesley.xsl
<?xml version="1.0" encoding="UTF-8"?>

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

        <xsl:output method="xml" indent="yes"/>

        <xsl:template match="examples">
                <examples>
                <xsl:for-each-group select="node()"
                                    group-starting-with="hop">
                  <xsl:if test="self::hop">
                        <example-pair>
                                <!-- copy the hop element-->
                                <xsl:copy-of select="."/>
<!-- and then surround the mixed text following the hop element
                                        with eng tags -->
                                <eng>
<xsl:apply-templates select="current-group() except ."/>
                                </eng>
                        </example-pair>
                  </xsl:if>
                </xsl:for-each-group>
                </examples>
        </xsl:template>

        <!-- default copying of the document -->
        <xsl:template match="@*|node()">
                <xsl:copy>
                        <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
        </xsl:template>

</xsl:stylesheet>

T:\ftemp>


--
XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19
Vote for your XML training:   http://www.CraneSoftwrights.com/s/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>