แปลง xml

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.

เขียนโปรแกรมเพื่อแปลง

<node>  
  <node level="0" type="c" name="toplevel"/>  
  <node level="1" type="i" name="1. item"/>  
  <node level="1" type="c" name="2. container"/>  
  <node level="2" type="i" name="2.1 item"/>  
  <node level="2" type="i" name="2.2 item"/>  
  <node level="1" type="i" name="3.  item"/>
  <node level="1" type="c" name="4. container"/>  
  <node level="2" type="i" name="4.1 item"/>  
  <node level="2" type="c" name="4.2 container"/>  
  <node level="3" type="i" name="4.2.1 item"/> 
</node>

เป็น

<node>
 <node type="c" name="toplevel">
  <node type="i" name="1. item"/>
  <node type="c" name="2. container"/>
   <node type="i" name="2.1 item"/>
   <node type="i" name="2.2 item"/>
  </node>
  <node type="i" name="3. item"/>
  <node type="c" name="4. container">
   <node type="i" name="4.1 item"/>
   <node type="c" name="4.2 container"/>
    <node type="i" name="4.2.1 item"/>
   </node>
  </node>
 </node>
</node>
sugree's picture

อูย โจทย์นี้ต้อง SAX ซะละมั๊ง มาจองที่ไว้ก่อน

veer's picture

http://codenone.com/node/141

ต้องย่อหน้าถูกด้วยเปล่า?

sugree's picture

เพราะไอ้ indent นี่แหละ นั่งแกะโค้ดอยู่นาน ทำไม _packing ต้องเป็น 0 หว่า

ruby
http://www.codenone.com/node/146
(แบบไม่สนใจย่อหน้า)

โจทย์ผิดนิดนึงหรือปล่าวครับตรง ผลลัพธ์ที่ต้องการนะครับ
บรรทัด

><node type="c" name="4.2 container"/></code> อันนี้ควรจะเป็น
<code [type="xml"]><node type="c" name="4.2 container"></code> หรือปล่าวครับ เพราะว่าผมดูจาก
close tag แล้วมันมากกว่า open tag อยู่หนึ่งอันนะครับ

ใช่ครับ เกินมาตัว

ผมยกโจทย์นี้ มาจาก post ของ
HOWTO: convert flat list w/ level information to a hierarchialone?
ในนั้นแสดง algorithm ที่เขียนด้วย haskell
ซึ่งแสดงการใช้ pure functional solve โจทย์ข้อนี้
แกะโปรแกรมแล้วเหนื่อยใจ เพราะ recursive เยอะเหลือเกิน
(สำหรับคนที่จะลองแกะ ,ตัวโปรแกรมเขามี parameter res เกินมาตัว
ignore หรือตัดทิ้งได้เลย)


แต่ที่สนใจก็คือ solution ที่เป็น xsl
เพราะที่เคยใช้ xsl ก็มักจะเป็นงานพื้นๆ ไม่เคยคิดว่า
มันจะ solve โจทย์แบบนี้ได้
แถมยังมี trick แปลกๆ เช่น การใช้ค่า accu ที่ initalize จาก ../

veer's picture

ผมลอง xsl อยู่พักนึงเหมือนกัน แล้วก็ ..... ทำไม่ไหว

ขนาดแกะ code เขา พอจะเข้าใจแล้ว
ถ้าลองให้เขียนเองโดยไม่ดู code เขาเลย ก็คงไม่รอดเหมือนกัน

sugree's picture

ถ้ามันยากขนาดนี้ แล้วมันเร็วขนาดไหน

:) เป็นคำถามที่ดีมาก
คำตอบคือ ไม่เร็วเลยครับ
ที่มันยาก เพราะว่า nature ของโจทย์ กับ constrain ของภาษา
+ วิธีคิดของเรา(ที่ยังคิดแบบ recursive ไม่คล่อง)

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น