[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: element to root

Subject: RE: element to root
From: "Aron Bock" <aronbock@xxxxxxxxxxx>
Date: Tue, 31 May 2005 20:44:33 +0000
span style b
Jon,

I remember your similar post from a few days ago and had kept it aside to look at in time; today's repost prompted me to do so. Well, the bad news is that while I think there's an elegant, recursive, etc XSL solution, I've seen it only hazily in the past half-hour.

The good news is that this is readily amenable to a procedural solution, if you'll accept one, as implemented in the following jython script. The idea is that we write out each line, maintain a "stack" of open-tags we find, and write closing and re-opening tags at each <br/>.

The script below doesn't use an XML parser, but could readily be fitted to use a SAX parser. In which case you wouldn't need the additional simplyfying assumption that element start-tags are on lines by themselves.

Given your input below:


<p>
  <span style="style a">
    span a text 1
    <span style="style b">
      span b pre br text
      <br name="b"/>
      span b post br text
    </span>
    span a text 2
  </span>
</p>

it produces:


<p>
 <span style="style a">
   span a text 1
   <span style="style b">
     span b pre br text
   </span>
 </span>

<br name="b"/>

 <span style="style a">
   <span style="style b">
     span b post br text
   </span>
   span a text 2
 </span>
</p>

which is what you asked for:

------
<p>
  <span style="style a">
    span a text 1
    <span style="style b">
      span b pre br text
    </span>
  </span>
  <span style="style a"/>
  <br name="b"/>
  <span style="style a">
    <span style="style b">
      span b post br text
    </span>
    span a text 2
  </span>
</p>
------


And given your input from your original email (reformatted for start-tags are on lines by themselves):

<p>
 <strong>
   strong:text(top)
   <br/>
   prefix
     <span style="a style">
       span a        <span style="rgb();">
         span b
         <br/>
         text
       </span>
       text
      </span>
   strong:text(btm)    <br/>
   suffix
   <br/>
 </strong>
 Root level text with
 <br/>
 tag.
</p>

it produces this:


<p>
 <strong>
   strong:text(top)
 </strong>

<br/>

 <strong>
   prefix
     <span style="a style">
       span a
       <span style="rgb();">
         span b
       </span>
     </span>
 </strong>

<br/>

 <strong>
     <span style="a style">
       <span style="rgb();">
         text
       </span>
       text
      </span>
   strong:text(btm)
 </strong>

<br/>

 <strong>
   suffix
 </strong>

<br/>

 <strong>
 </strong>
 Root level text with

<br/>

 tag.
</p>


Here's the script (which is more illustrative than production-worthy):


import re

stack, b, linecount = [], 0, 0
f = open(r'e:\temp\test.xml')

try:
   while 1:
       line = f.readline(  ).rstrip()
       if not line: break

linecount += 1

       if linecount == 1:
           pass
       elif re.match("\s*<br.+>$", line):
           b = 1
       elif re.match("\s*<?\w+[^>]*>$", line): # start-tag
           stack.append( line )
       elif re.match("\s*</\w+[^>]*>$", line): # end-tag
           stack = stack[:len(stack)-1]

       if b:
           # write closing tags
           stack.reverse()
           for t in stack:
               m = re.match("(?P<TAB>\s*)<(?P<NAME>\w+)", t)
               print "%s</%s>" % ( m.group('TAB'), m.group('NAME') )

print line

       if b:
           # write opening tags again
           stack.reverse()
           for t in stack:
               print t
           b = 0

finally:
   f.close(  )


Regards,


--A

_________________________________________________________________
Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.