webbenny Posted October 12, 2011 Share Posted October 12, 2011 (edited) Hi everybody! I try to transform a xml file. I've the very basic problem, that I need to close and reopen tags in xslt (ok, this maybe is not the problem even if I think it is.. ). My input structure is like: <?xml version="1.0" encoding="UTF-8"?><misc><list> <listitem> <para>some text..</para> </listitem> <listitem> <para>Here is some more text. <note> <para>This is my notice in a para..</para> </note> find even more here ;-) </para> </listitem></list></misc> ...and I wanna get a result like this because I'm not allowed to use <note> in a <para>: <?xml version="1.0" encoding="UTF-8"?><misc><list><listitem> <para>some text..</para></listitem><listitem> <para>Here is some more text.</para></listitem></list><note><para>This is my notice in a para..</para></note><list continue="true"><listitem>find even more here ;-)</listitem></list></misc> Here is what I'm working on.. <?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"><xsl:template match="list"><xsl:choose> <xsl:when test="descendant::note"> <!-- here I need something like split the list? --> </xsl:when> <xsl:otherwise> <xsl:element name="list"> <xsl:apply-templates/> </xsl:element> </xsl:otherwise></xsl:choose></xsl:template><xsl:template match="listitem"><xsl:element name="listitem"></xsl:element></xsl:template></xsl:stylesheet> Thanks for any approach!webbenny Edited October 13, 2011 by webbenny 1 Link to comment Share on other sites More sharing options...
Martin Honnen Posted October 12, 2011 Share Posted October 12, 2011 What you have posted as the desired result is not a well-formed XML document as it lacks a single root element containing all other elements? Are you sure you want that kind of result? Link to comment Share on other sites More sharing options...
webbenny Posted October 13, 2011 Author Share Posted October 13, 2011 Yes, you are very right. I'm very sorry, it was only a copy-and-paste snippet from the whole document. I've added a <misc> root element. So it should be well formed.. Link to comment Share on other sites More sharing options...
Martin Honnen Posted October 13, 2011 Share Posted October 13, 2011 Here is some approach using XSLT 2.0 and for-each-group group-starting-with: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* , node()"/> </xsl:copy> </xsl:template> <xsl:template match="list"> <xsl:for-each-group select="listitem/*/node()" group-starting-with="note"> <xsl:choose> <xsl:when test="self::note"> <xsl:copy-of select="."/> <list continue="true"> <xsl:apply-templates select="current-group() except ."/> </list> </xsl:when> <xsl:otherwise> <list> <xsl:apply-templates select="current-group()"/> </list> </xsl:otherwise> </xsl:choose> </xsl:for-each-group> </xsl:template> <xsl:template match="para/text()"> <para> <xsl:value-of select="."/> </para> </xsl:template> </xsl:stylesheet> That stylesheet, when applied with an XSLT 2.0 processor like Saxon 9.3 outputs <?xml version="1.0" encoding="UTF-8"?><misc> <list> <para>some text..</para> <para>Here is some more text. </para> </list> <note> <para>This is my notice in a para..</para> </note> <list continue="true"> <para> find even more here ;-) </para> </list></misc> Does that help?I am afraid so far the problem is kind of underspecified, I don't know whether the list elements can contain anything other than para and note elements. Link to comment Share on other sites More sharing options...
webbenny Posted May 31, 2012 Author Share Posted May 31, 2012 Does that help?I am afraid so far the problem is kind of underspecified, I don't know whether the list elements can contain anything other than para and note elements. Hmm.. Yes you're right! I was looking for a very generic expression of my problem.. The main cause is, that I've a source structure which allows some nested xml elements but the result xml doesn't allow this element inside another element. so I've to close and reopen it. Do you recommend to use for-each-group (which helped me a lot in this case!) or is there another posibility? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now