YAML - Node Properties



Each presentation node includes two major characteristics called anchor and tag. Node properties may be specified with node content, omitted from the character stream.

The basic example of node representation is as follows −

%YAML 1.1
---
!!map {
   ? &A1 !!str "foo"
   : !!str "bar",
   ? !!str &A2 "baz"
   : *a1
}

Node Anchors

The anchor property represents a node for future reference. The character stream of YAML representation in node is denoted with the ampersand (&) indicator. The YAML processor need not preserve the anchor name with the representation details composed in it. The following code explains this −

%YAML 1.1
---
!!map {
   ? !!str "First occurence"
   : &A !!str "Value",
   ? !!str "Second occurence"
   : *A
}

The output of YAML generated with anchor nodes is shown below −

---
!!map {
   ? !!str "First occurence"
   : !!str "Value",
   ? !!str "Second occurence"
   : !!str "Value",
}

Node Tags

The tag property represents the type of native data structure which defines a node completely. A tag is represented with the (!) indicator. Tags are considered as an inherent part of the representation graph. The following example of explains node tags in detail −

%YAML 1.1
---
!!map {
   ? !<tag:yaml.org,2002:str> "foo"
   : !<!bar> "baz"
}

Node Content

Node content can be represented in a flow content or block format. Block content extends to the end of line and uses indentation to denote structure. Each collection kind can be represented in a specific single flow collection style or can be considered as a single block. The following code explains this in detail −

%YAML 1.1
---
!!map {
   ? !!str "foo"
   : !!str "bar baz"
}

%YAML 1.1
---
!!str "foo bar"

%YAML 1.1
---
!!str "foo bar"

%YAML 1.1
---
!!str "foo bar\n"
Advertisements