Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: XML tag closing (naive way)

by tye (Cardinal)
on Jan 06, 2012 at 06:09 UTC ( #946532=note: print w/ replies, xml ) Need Help??


in reply to XML tag closing

my $xmlish = <<'END'; <whatever><book-part type='closed'> Chapter 1 </book-part> <book-part type='unclosed'> Chapter 2 <book-part type='final'> Chapter 3 </whatever> END my $prev = '/'; $xmlish =~ s{(?=<\s*(/?)\s*book-part\b)}{ ( ! $prev && ! $1 ? '</book-part>' : '', $prev= $1, )[0] }ge; print $xmlish; __END__ <whatever><book-part type='closed'> Chapter 1 </book-part> <book-part type='unclosed'> Chapter 2 </book-part><book-part type='final'> Chapter 3 </whatever>

Assumes no <book-part bits inside attribute values, comments, nor CDATA blocks, of course. Not using an XML parser since missing closing tags means you don't really have XML that a real XML parser would handle. Also note how it doesn't insert a final closing tag (you didn't provide enough information for me to know how to do that for your case).

- tye        


Comment on Re: XML tag closing (naive way)
Select or Download Code
Re^2: XML tag closing (naive way)
by maha (Novice) on Jan 06, 2012 at 09:10 UTC

    sorry tye,

    i'm not native English speaker,ur code is working but i have lots of content, tags inside the chapter,i want to close book part after the content and the ending of the content there is no constant tag to fix an origin,can i mention the ending of the content by $ symbol or u can give some more suggestion to close

    thanks for concerning my problem

      Please think of this as a constructive reply (rather than critisiscm):

      Even if you are not a native english speaker (neither am i or many other monks here), please try to use english, not ircspeak (use "you" instead of "u", "your" instead of "ur" and so on). It would make for an easier read and will also show that you are at least trying.

      This in turn will have the effect that more people will be willing to read your whole post and reply with an answer.

      "Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupidů and I went ahead anyway." (Crow in "MST3K The Movie")
Re^2: XML tag closing (naive way)
by maha (Novice) on Jan 06, 2012 at 10:34 UTC

    i got the solution tye,and its working properly

    if($line =~ /(<book-part[^>]*>)((?:(?!<book-part[^>]*>).)*)(?=<book-pa +rt[^>]*>|$)/sgi) { $line .="</book-part>\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-07-24 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (158 votes), past polls