YAML: translating free-flowing text from XML

I’m studying YAML as a replacement for XML, but I’m experiencing difficulties when dealing with elements containing free-flowing text with embedded elements. For instance, the following XML document:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <text>
This is an example text, spanning multiple lines, and it has embedded elements
like <a p="value">this</a> and <b>this</b>. There is also a list:
<quote>
<text>The text of the quote, spanning multiple lines, and it has
embedded elements like <c p="value">this</c> and <b>this</b></text>
<author>The Author of this quote</author>
</quote>
Text continues here.
</text>
</code>
<code> <text> This is an example text, spanning multiple lines, and it has embedded elements like <a p="value">this</a> and <b>this</b>. There is also a list: <quote> <text>The text of the quote, spanning multiple lines, and it has embedded elements like <c p="value">this</c> and <b>this</b></text> <author>The Author of this quote</author> </quote> Text continues here. </text> </code>
    <text>
    This is an example text, spanning multiple lines, and it has embedded elements
    like <a p="value">this</a> and <b>this</b>.  There is also a list:
         <quote>
            <text>The text of the quote, spanning multiple lines, and it has
            embedded elements like <c p="value">this</c> and <b>this</b></text>
            <author>The Author of this quote</author>
        </quote>
    Text continues here.
    </text>

I don’t know how to convert the embedded elements in YAML. My understanding is that the above XML document segment translates to something like this (except for the embedded elements):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> text: >
This is an example text, spanning multiple lines, and it has embedded
elements like <a p="value">this</a> and <b>this</b>. There is also a
list:
quote:
text: >
The text of the quote, spanning multiple lines,
and it has embedded elements like <c p="value">this</c>
and <b>this</b>
author: The Author of this quote
Text continues here.
</code>
<code> text: > This is an example text, spanning multiple lines, and it has embedded elements like <a p="value">this</a> and <b>this</b>. There is also a list: quote: text: > The text of the quote, spanning multiple lines, and it has embedded elements like <c p="value">this</c> and <b>this</b> author: The Author of this quote Text continues here. </code>
    text: >
            This is an example text, spanning multiple lines, and it has embedded
            elements like <a p="value">this</a> and <b>this</b>.  There is also a
            list:
                 quote:
                    text: > 
                            The text of the quote, spanning multiple lines,
                            and it has embedded elements like <c p="value">this</c>
                            and <b>this</b>
                    author: The Author of this quote
            Text continues here.

Also, is indentation not needed in some places?

If an XML/HTML/SGML parser, for some programming language X, generically parses your kind of input (instead of generating abstracted objects), you normally have the tags translated to the mapping construct for X, with the children tags and string elements as a sequence construct for X (as these need ordering), with the attributes of the tag some special first element of that sequence (if attributes are available).

Such a hierarchy is a perfect match for YAML¹:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>text:
- |-
This is an example text, spanning multiple lines, and it has embedded elements
like
- a:
- .attribute:
p: value
- this
- and
- b: this
- '. There is also a list:'
- quote:
- text:
- |-
The text of the quote, spanning multiple lines, and it has
embedded elements like
- c:
- .attribute:
p: value
- this
- and
- b: this
- author: The Author of this quote
- Text continues here.
</code>
<code>text: - |- This is an example text, spanning multiple lines, and it has embedded elements like - a: - .attribute: p: value - this - and - b: this - '. There is also a list:' - quote: - text: - |- The text of the quote, spanning multiple lines, and it has embedded elements like - c: - .attribute: p: value - this - and - b: this - author: The Author of this quote - Text continues here. </code>
text:
- |-
  This is an example text, spanning multiple lines, and it has embedded elements
  like
- a:
  - .attribute:
      p: value
  - this
- and
- b: this
- '.  There is also a list:'
- quote:
  - text:
    - |-
      The text of the quote, spanning multiple lines, and it has
              embedded elements like
    - c:
      - .attribute:
          p: value
      - this
    - and
    - b: this
  - author: The Author of this quote
- Text continues here.

The literal block has been used here to preserve newlines in the original data, but folded could be used as well.

This YAML can be used to regenerate the orginal XML/HTML/SGML structure except for some stripped and collapsed white-space information, which normally do not affect e.g. rendering of HTML. The above YAML is not the representation of XML/HTML/SGML in YAML, just one of the possible ways of doing that.

As for your second question:

is indentation not needed in some places?

As you can see from the output, indentation is not always needed, the sequence elements under the key text: are not indented. But if you outdent, then you always end a collection (sequence/mapping).


¹ This is the output of yaml from-html --no-body input.xml with input.xml containing your XML document. yaml is a command that is part of my python package ruamel.yaml

The YAML website describes YAML in the following terms:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>YAML: YAML Ain't Markup Language
What It Is: YAML is a human friendly data serialization
standard for all programming languages.
</code>
<code>YAML: YAML Ain't Markup Language What It Is: YAML is a human friendly data serialization standard for all programming languages. </code>
YAML: YAML Ain't Markup Language

What It Is: YAML is a human friendly data serialization
  standard for all programming languages.

You probably don’t want YAML for this. YAML is meant to be used to save data out to files for future retrieval, basically as a form of serialization. The major benefit it has over XML in this regard is the fact that he lacks the clutter of XML’s angle brackets and end tags. It’s easy to read and edit for humans. XML, on the other hand, is already designed to work with an HTML-like syntax. XML’s major purpose is as a markup language.

You could look at it this way:

  • Use YAML when you want to save text or objects to a file.
  • Use XML when you want to provide markup for
    documents

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật