Re: [xsl] Grouping multiple tags into one parent tag

Subject: Re: [xsl] Grouping multiple tags into one parent tag
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 15 Feb 2009 13:23:06 -0500
At 2009-02-15 13:11 -0500, Chad Chelius wrote:
Thank you all for your responses. Ken this is exactly what I was
trying to do! The only thing is, I am also using:

<xsl:output method="xml" cdata-section-elements="field"/>

To wrap the contents of the field element in a cdata tag. After using
the template rule that you provided, that cdata-section-element no
longer works. I'm not sure why.

Your transformation does not create any text nodes to be wrapped, it is creating only <p> element children.


Again, I'm suspecting an example from you would be critically important to understand your problem.

Perhaps you want the following?

 <field name="body">
  <![CDATA[
   <p>content here</p>
   <p>content here</p>
   <p>content here</p>
  ]]>
 </field>

Then you want:

  <xsl:output method="xml" cdata-section-elements="field"/>
  <xsl:template match="body">
    <xsl:if test="not(preceding-sibling::body)">
       <field name="body">
          <xsl:for-each select="../body">
             <xsl:text>&lt;p></xsl:text>
                <xsl:apply-templates/>
             <xsl:text>&lt;/p>
</xsl:text>
          </xsl:for-each>
       </field>
    </xsl:if>
  </xsl:template>

... because you are asking the stylesheet to produce text, not elements ... the text just happens to look like elements when wrapped in a CDATA section.

I hope the example below helps.

. . . . . . . . Ken

t:\ftemp>type chad.xml
<test>
<body>content here</body>
<body>content there</body>
<body>content everywhere</body>
</test>

t:\ftemp>xslt chad.xml chad.xsl
<?xml version="1.0" encoding="utf-8"?>
<field name="body"><![CDATA[<p>content here</p>
<p>content there</p>
<p>content everywhere</p>
]]></field>
t:\ftemp>type chad.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

  <xsl:output method="xml" cdata-section-elements="field"/>
  <xsl:template match="body">
    <xsl:if test="not(preceding-sibling::body)">
       <field name="body">
          <xsl:for-each select="../body">
             <xsl:text>&lt;p></xsl:text>
                <xsl:apply-templates/>
             <xsl:text>&lt;/p>
</xsl:text>
          </xsl:for-each>
       </field>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
t:\ftemp>

--
Upcoming hands-on XSLT, UBL & code list hands-on training classes:
Brussels, BE 2009-03;  Prague, CZ 2009-03, http://www.xmlprague.cz
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@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread