Hi All, The following XML fragment is a simplification of a problem I'm trying to solve. <?xml version="1.0" encoding="UTF-8"?> <document> <index><![CDATA[<div class="index">]]> <partref/><![CDATA[</div>]]> <partref/> </index> <part> Part1 </part> <part> Part2 </part> </document> I want to render this as <div class="index>Part1Part2</div> Can anyone give me a start on the XLS for this please? Thanks in advance, Kirk
KP wrote: > The following XML fragment is a simplification of a problem I'm trying > to solve. > > <?xml version="1.0" encoding="UTF-8"?> > <document> > <index><![CDATA[<div class="index">]]> > <partref/><![CDATA[</div>]]> > <partref/> > </index> > <part> > Part1 > </part> > <part> > Part2 > </part> > </document> > > I want to render this as > > <div class="index>Part1Part2</div> Are you sure the 'Part2' belongs inside of the div element? Because above the second 'partref' is outside of the (escaped) div. It is also not clear where the 'div class="index"' comes from, whether it comes from the 'index' element or from the escaped 'div' element. Assuming you want to transform the 'index' element into a 'div class="index"' element then you can use <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/document"> <xsl:apply-templates select="index"/> </xsl:template> <xsl:template match="index"> <div class="index"> <xsl:apply-templates select="*"/> </div> </xsl:template> <xsl:template match="index/partref"> <xsl:apply-templates select="../../part[count(current()/preceding-sibling::partref) + 1]"/> </xsl:template> <xsl:template match="part"> <xsl:value-of select="normalize-space(.)"/> </xsl:template> </xsl:stylesheet> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
"KP" wrote in message news:f26409bd-f282-4a32-9747-abeba725f5c1@c58g2000hsc.googlegroups.com... > Hi All, > > The following XML fragment is a simplification of a problem I'm trying > to solve. > > <?xml version="1.0" encoding="UTF-8"?> > <document> > <index><![CDATA[<div class="index">]]> > <partref/><![CDATA[</div>]]> > <partref/> > </index> > <part> > Part1 > </part> > <part> > Part2 > </part> > </document> > > I want to render this as > > <div class="index>Part1Part2</div> > > Can anyone give me a start on the XLS for this please? > > Thanks in advance, > > Kirk That XML is an abomination. When you say you want to render as: <div class="index>Part1Part2</div> do you mean that the div class="index" is always like that or are you taking it from the CDATA section without the <partref/> text? Is there any additional logic as to which part elements' contents are include or is it just all of them? -- Joe Fawcett (MVP - XML) http://joe.fawcett.name
Sorry I made a small mistake in the XML - it should have been: <?xml version="1.0" encoding="UTF-8"?> <document> <index><![CDATA[<div class="index">]]> <partref/><partref/><![CDATA[</div>]]> </index> <part> Part1 </part> <part> Part2 </part> </document> The XSL example almost does what I want but I need the CDATA elements to be taken from the XML and not "hard-coded" into the XSL. This is a simplification of a much larger XML/XSL but demonstrates the simple problem I am trying to solve. Thanks for your input, Kirk
KP wrote: > Sorry I made a small mistake in the XML - it should have been: > > <?xml version="1.0" encoding="UTF-8"?> > <document> > <index><![CDATA[<div class="index">]]> > <partref/><partref/><![CDATA[</div>]]> > </index> > <part> > Part1 > </part> > <part> > Part2 > </part> > </document> > > The XSL example almost does what I want but I need the CDATA elements > to be taken from the XML and not "hard-coded" into the XSL. This is a > simplification of a much larger XML/XSL but demonstrates the simple > problem I am trying to solve. You should fix the input XML as otherwise the problem is not so simple to solve, you need disable-output-escaping: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/document"> <xsl:apply-templates select="index/node()"/> </xsl:template> <xsl:template match="index/text()"> <xsl:value-of select="." disable-output-escaping="yes"/> </xsl:template> <xsl:template match="index/partref"> <xsl:value-of select="normalize-space(../../part[count(current()/preceding-sibling::partref) + 1])"/> </xsl:template> </xsl:stylesheet> Note that disable-output-escaping is an optional serialization feature meaning it is not supported by all XSLT processors in all scenarios. Mozilla/Firefox for instance does not support it as it does not serialize the result tree. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/