src/kdl/xik

Search:
Group by:
Source   Edit  

XiK

This modules implements the XML-in-KDL (XiK) specification to encode and decode XML in KDL.

Checkout the official specification: https://github.com/kdl-org/kdl/blob/main/XML-IN-KDL.md.

Example:

import src/kdl/xik
import std/[xmlparser, xmltree]
import kdl

const data = """
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
</breakfast_menu>"""
assert data.parseXml().toKdl() == parseKdl("""
breakfast_menu {
  food {
    name "Belgian Waffles"
    price "$5.95"
    description "Two of our famous Belgian Waffles with plenty of real maple syrup\n"
    calories "650"
  }
  food {
    name "Strawberry Belgian Waffles"
    price "$7.95"
    description "Light Belgian waffles covered with strawberries and whipped cream\n"
    calories "900"
  }
}""")[0]

assert $data.parseXml() == $data.parseXml().toKdl().toXml()

Procs

proc toKdl(node: XmlNode; addComments = false): KdlNode {....raises: [], tags: [],
    forbids: [].}
Converts node into its KDL representation. Ignores CDATA nodes, i.e.. If addComments preserves XML comments as KDL nodes named !. Source   Edit  
proc toXml(node: KdlNode; addComments = false): XmlNode {.
    ...raises: [KdlError, Exception, ValueError], tags: [RootEffect], forbids: [].}
Converts node into its XML representation.
  • If addComments preserves comments in elements, if node is a comment ('! "something"') it DOES return it.

Example:

import std/xmltree
import kdl

assert parseKdl("! \"comment\"")[0].toXml().kind == xnComment
assert parseKdl("tag { ! \"comment\"; - \"text\" }")[0].toXml().len == 1 # Ignored the comment
assert parseKdl("tag { ! \"comment\"; - \"text\" }")[0].toXml(addComments = true).len == 2 # Added the comment
Source   Edit  
proc toXmlSingle(node: KdlNode): XmlNode {.
    ...raises: [KdlError, Exception, ValueError], tags: [RootEffect], forbids: [].}
Source   Edit