[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: XSLT 2.0 grouping and text nodes and mixed text
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 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>
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|