Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: XML tag closing (naive way)

by tye (Sage)
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        

Replies are listed 'Best First'.
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"; }
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")

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946532]
[marto]: I don't think their inability to make any sense is related to alcohol
[Discipulus]: their?
[karlgoethebier]: marto: "their inability.." Perhaps the result of a wrong education?
[karlgoethebier]: or no education?
[choroba]: their = his or her
[Discipulus]: anyway once I proposed an automatic font decrease for post below -1, stepping every 5 votes: i'm still thinks it's a good idea but dunno if feasible
[shmem]: ...emulated dual in english

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2017-05-23 08:16 GMT
Find Nodes?
    Voting Booth?