split an element into two or more elements depending on its children

Rhinnatte

New Member
So, I have been trying to brush up on my \[code\]XSLT\[/code\] the past few days. I am very unfamiliar with it, spending most of my past using \[code\]XQuery\[/code\] to transform my XML. I am stuck on a rather simple problem, but looking around I have not found a clear solution. Simply, I want to split some elements into two depending on its children. For example, if my XML looks like the following: \[code\]<?xml version="1.0" encoding="UTF-8"?> <root> <p> Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t- bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger bacon filet mignon pork chop tail. <note.ref id="0001"><super>1</super></note.ref> <note id="0001"> <p> You may need to consult a latin butcher. Good Luck. </p> </note> Pork loin ribeye bacon pastrami drumstick sirloin, shoulder pig jowl. Salami brisket rump ham, tail hamburger strip steak pig ham hock short ribs jerky shank beef spare ribs. Capicola short ribs swine beef meatball jowl pork belly. Doner leberkas short ribs, flank chuck pancetta bresaola bacon ham hock pork hamburger fatback. </p> </root>\[/code\]after I run my \[code\]xsl\[/code\] I am left with something like the following:\[code\]<html><body> <p> Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t- bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger bacon filet mignon pork chop tail. <span class="noteRef" id="0001"><sup>1</sup></span> <div id="note-0001"> <p> You may need to consult a latin butcher. Good Luck. </p> </div> Pork loin ribeye bacon pastrami drumstick sirloin, shoulder pig jowl. Salami brisket rump ham, tail hamburger strip steak pig ham hock short ribs jerky shank beef spare ribs. Capicola short ribs swine beef meatball jowl pork belly. Doner leberkas short ribs, flank chuck pancetta bresaola bacon ham hock pork hamburger fatback. </p></body></html>\[/code\]The problem with this is obviously an \[code\]HTML\[/code\] \[code\]<p>\[/code\] cannot have a \[code\]<div>\[/code\] as a child, let a lone another \[code\]<p>\[/code\] as a grandchild. This is just invalid. A browser, such as chromium, may render the first paragraph ending when it hits the \[code\]<div>\[/code\], wrapping, appropriately, the note in its own \[code\]<p>\[/code\], but leaving the text after the note orpahened. So that any CSS applied to the \[code\]<p>\[/code\] will fail to be applied. How would I split one \[code\]<p>\[/code\] element into two depending on the elements descendants? I have abstracted my question slightly, so the following \[code\]XSL\[/code\] of what I have tried could be slightly off. \[code\]<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" exclude-result-prefixes="xs xd" version="2.0"><xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html></xsl:template> <xsl:template match="p"> <p> <xsl:apply-templates/> </p> </xsl:template <xsl:template match="note.ref"> <span class="noteRef" id="{@id}"> <xsl:apply-templates/> </span> </xsl:template> <xsl:template match="super"> <sup> <xsl:apply-templates/> </sup> </xsl:template> <xsl:template match="note"> <div id="note-{@id}"> <xsl:apply-templates/> </div> </xsl:template></xsl:stylesheet>\[/code\]
 
Back
Top