Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does YAML represent XML attributes?

Tags:

xml

yaml

How can I convert this XML to YAML and back again without losing information?

<foo bar='one' baz='two'>Lorem Ipsum</foo>

My best guess is something like this, but it looks too verbose to me:

foo:
  attr: 
    - bar: one
    - baz: two
  value: Lorem Ipsum

Second question: Is there syntax similar to this for attributes in YAML?

foo ( bar: one, baz: two ): Lorem Ipsum
like image 571
Gavin Avatar asked Feb 07 '15 06:02

Gavin


People also ask

Is YAML XML?

XML is an eXtensible Markup Language while Yaml is a human-readable data-serialization language. Let us look at the comparison of both languages in terms of their file structure, syntax and usage.

Is Yml better than XML?

The biggest difference, though, is that XML is meant to be a markup language and YAML is really more of a data format. Representing simple, hierarchical data tends to be more gracefully done in YAML, but actual marked-up text is awkward to represent.

What is YAML file format?

YAML is a digestible data serialization language often used to create configuration files with any programming language. Designed for human interaction, YAML is a strict superset of JSON, another data serialization language.

How does YAML file work?

YAML files store information, so they do not include actions and decisions. Unlike XML or JSON, YAML presents data in a way that makes it easy for a human to read. The simple syntax does not impact the language's capabilities. Any data or structure added to an XML or JSON file can also be stored in YAML.


Video Answer


1 Answers

If you can assume you can't have duplicate attribute names, your guess can be simplified to use a map:

foo:
  attr: 
    bar: one
    baz: two
  value: Lorem Ipsum

If you wanted a barely more compact form, you could use YAML flow style, which is similar to JSON and the closest thing to your second question that is available:

foo: {attr: {bar: one, baz: two}, value: lorum ipsum}

... it isn't very common though.

like image 176
Xavier Shay Avatar answered Sep 24 '22 00:09

Xavier Shay