lang.xml
Module lang.xml
API
ballerina/lang.xml Ballerina library
Module overview
The lang.xml
module corresponds to the xml
basic type.
Functions
'map
function 'map(xml x, function(Element|Comment|ProcessingInstruction|Text) returns (XmlType)
func) returns xml
Applies a function to each item in an xml sequence, and returns an xml sequence of the results.
Each item is represented as a singleton value.
xml x = xml `<book>Hamlet</book><book>Macbeth</book>`; x.map(function (xml xmlContent) returns xml => xml `<book kind="play">${xmlContent.children()}</book>` ) ⇒ <book kind="play">Hamlet</book><book kind="play">Macbeth</book>
Parameters
- x xml - the xml value
- func
function(Element|Comment|ProcessingInstruction|Text) returns (XmlType)
- a function to apply to each child oritem
Return Type
- xml - new xml value containing result of applying parameter
func
to each child oritem
children
Returns the children of elements in an xml value.
When parameter x
is of type Element
, it is equivalent to function getChildren
.
This is equivalent to elements(x).map(getChildren)
.
xml x = xml `<books><book><title>Hamlet</title></book><book><title>Macbeth</title></book></books>`; x.children() ⇒ <book><title>Hamlet</title></book><book><title>Macbeth</title></book>
Parameters
- x xml - xml value
Return Type
- xml - xml sequence containing the children of each element x concatenated in order
concat
Concatenates xml and string values.
xml bookA = xml `<book>Sherlock Holmes</book>`; xml bookB = xml `<book>Hamlet</book>`; xml:concat(bookA, bookB, xml `<book>Macbeth</book>`) ⇒ <book>Sherlock Holmes</book><book>Hamlet</book><book>Macbeth</book> bookA.concat(bookB) ⇒ <book>Sherlock Holmes</book><book>Hamlet</book> bookB.concat("Novel") ⇒ <book>Hamlet</book>Novel xml:concat("Hello", "World") ⇒ HelloWorld xml[] subjects = [xml `<subject>English</subject>`, xml `<subject>Math</subject>`, xml `<subject>ICT</subject>`]; xml:concat(...subjects) ⇒ <subject>English</subject><subject>Math</subject><subject>ICT</subject>
Return Type
- xml - an xml sequence that is the concatenation of all the parameter
xs
; an empty xml sequence if the parameterxs
is empty
createComment
Creates a new xml comment item.
xml:createComment("Example comment") ⇒ <!--Example comment-->
Parameters
- content string - the content of the comment to be constructed.
Return Type
- Comment - an xml sequence consisting of a comment with parameter
content
as the content
createElement
Creates a new xml element item.
xml:createElement( "book", {genre: "Mystery", year: "1892"}, xml `<title>Sherlock Holmes</title><author>Arthur Conan Doyle</author>` ) ⇒ <book genre="Mystery" year="1892"><title>Sherlock Holmes</title><author>Arthur Conan Doyle</author></book> xml:createElement("person") ⇒ <person/> xml:createElement("student", {id: "1209"}) ⇒ <student id="1209"/> xml:createElement("employee", children = xml `<name>John</name>`) ⇒ <employee><name>John</name></employee>
Parameters
- name string - the name of the new element
- children xml (default xml``) - the children of the new element
Return Type
- Element - an xml sequence consisting of only a new xml element with name
name
, attributesattributes
, and childrenchildren
The element's attribute map is a newly created map, into which any attributes specified by theattributes
map are copied.
createProcessingInstruction
function createProcessingInstruction(string target, string content) returns ProcessingInstruction
Creates a new xml processing instruction item.
xml:createProcessingInstruction("sort", "descending") ⇒ <?sort descending?>
Parameters
- target string - the target part of the processing instruction to be constructed
- content string - the content part of the processing instruction to be constructed
Return Type
- ProcessingInstruction - an xml sequence consisting of a processing instruction with parameter
target
as the target and parametercontent
as the content
createText
Constructs an xml value of type Text.
The constructed sequence will be empty when the length of parameter data
is zero.
xml:createText("Hello!") ⇒ Hello!
Parameters
- data string - the character data of the Text item
Return Type
- Text - an xml sequence that is either empty or consists of one text item
data
Returns a string with the character data of an xml value.
The character data of an xml value is as follows:
- the character data of a text item is a string with one character for each character information item represented by the text item;
- the character data of an element item is the character data of its children;
- the character data of a comment item is the empty string;
- the character data of a processing instruction item is the empty string;
- the character data of an empty xml sequence is the empty string;
- the character data of the concatenation of two xml sequences x1 and x2 is the concatenation of the character data of x1 and the character data of x2.
xml x = xml `<book>Jane Eyre</book>`; x.data() ⇒ Jane Eyre
Parameters
- x xml - the xml value
Return Type
- string - a string consisting of all the character data of parameter
x
elementChildren
Selects element children of an xml value.
This is equivalent to children(x).elements(nm)
.
xml x = xml `<book><play>Hamlet</play></book><book><novel>Macbeth</novel></book>`; x.elementChildren() ⇒ <play>Hamlet</play><novel>Macbeth</novel> x.elementChildren("novel") ⇒ <novel>Macbeth</novel>
Parameters
- x xml - the xml value
- nm string? (default ()) - the expanded name of the elements to be selected, or
()
for all elements
Return Type
- xml - an xml sequence consisting of child elements of elements in parameter
x
; if parameternm
is()
, returns a sequence of all such elements; otherwise, include only elements whose expanded name is parameternm
elements
Selects elements from an xml value.
If parameter nm
is ()
, selects all elements;
otherwise, selects only elements whose expanded name is parameter nm
.
xml x = xml `<!--Mystery--><novel>Sherlock Holmes</novel><!--Drama--><play>Hamlet</play> <novel>Jane Eyre</novel><play>Macbeth</play>`; x.elements() ⇒ <novel>Sherlock Holmes</novel><play>Hamlet</play><novel>Jane Eyre</novel><play>Macbeth</play> x.elements("novel") ⇒ <novel>Sherlock Holmes</novel><novel>Jane Eyre</novel>
Parameters
- x xml - the xml value
- nm string? (default ()) - the expanded name of the elements to be selected, or
()
for all elements
Return Type
- xml - an xml sequence consisting of all the element items in parameter
x
whose expanded name is parameternm
, or, if parameternm
is()
, all element items in parameterx
filter
function filter(xml x, function(Element|Comment|ProcessingInstruction|Text) returns (boolean)
func) returns xml
Selects the items from an xml sequence for which a function returns true.
Each item is represented as a singleton value.
xml x = xml `<novel>Sherlock Holemes</novel><play>Hamlet</play><novel>Invisible Man</novel><play>Romeo and Juliet</play>`; x.filter(x => x is xml:Element && x.getName() == "play") ⇒ <play>Hamlet</play><play>Romeo and Juliet</play>
Parameters
- x xml - xml value
- func
function(Element|Comment|ProcessingInstruction|Text) returns (boolean)
- a predicate to apply to each item to test whether it should be selected
Return Type
- xml - new xml sequence containing items in parameter
x
for which functionfunc
evaluates to true
forEach
function forEach(xml x, function(Element|Comment|ProcessingInstruction|Text) returns (() )
func)
Applies a function to each item in an xml sequence.
Each item is represented as a singleton value.
xml books = xml `<book>Sherlock Holmes</book><book>Invisible Man</book>`; xml titles = xml ``; books.forEach(function (xml xmlItem) { titles += xml `<novel>${xmlItem.data()}</novel>`; }); titles ⇒ <novel>Sherlock Holmes</novel><novel>Invisible Man</novel>
Parameters
- x xml - the xml value
- func
function(Element|Comment|ProcessingInstruction|Text) returns (() )
- a function to apply to each item in parameterx
fromString
Constructs an xml value from a string.
This parses the string using the content
production of the
XML 1.0 Recommendation.
xml:fromString("<book>Hamlet</book><book>Sherlock Holmes</book>") ⇒ <book>Hamlet</book><book>Sherlock Holmes</book> xml:fromString("<a>b") ⇒ error
Parameters
- s string - a string in XML format
get
Returns the item of an xml sequence with given index.
This differs from x[i]
in that it panics if
parameter x
does not have an item with index parameter i
.
xml x = xml `<book><title>Macbeth</title></book><book><title>Hamlet</title></book>`; x.get(1) ⇒ <book><title>Hamlet</title></book> x.get(15) ⇒ panic
Return Type
- Element|Comment|ProcessingInstruction|Text - the item with index parameter
i
in parameterx
getAttributes
Returns the map representing the attributes of an xml element.
This includes namespace attributes. The keys in the map are the expanded names of the attributes.
xml:Element e = xml `<person id="1012" employed="yes"><name>John</name></person>`; e.getAttributes() ⇒ {"id":"1012","employed":"yes"}
Parameters
- x Element - xml element
getChildren
Returns the children of an xml element.
xml:Element e = xml `<books><book>Hamlet</book><book>Macbeth</book></books>`; e.getChildren() ⇒ <book>Hamlet</book><book>Macbeth</book>
Parameters
- elem Element - xml element
Return Type
- xml - children of parameter
elem
getContent
function getContent(ProcessingInstruction|Comment x) returns string
Returns the content of a processing instruction or comment item.
xml:ProcessingInstruction procInstruction = xml `<?sort ascending?>`; procInstruction.getContent() ⇒ ascending xml:Comment comment = xml `<!--Employees by department-->`; comment.getContent() ⇒ Employees by department
Parameters
- x ProcessingInstruction|Comment - xml item
Return Type
- string - the content of parameter
x
getDescendants
Returns the descendants of an xml element.
The descendants of an element are the children of the element together with, for each of those children that is an element, the descendants of that element, ordered so that each element immediately precedes all its descendants. The order of the items in the returned sequence will thus correspond to the order in which the first character of the representation of the item would occur in the representation of the element in XML syntax.
xml:Element e = xml `<person><name>John Doe</name><age>30</age></person>`; e.getDescendants() ⇒ <name>John Doe</name>John Doe<age>30</age>30
Parameters
- elem Element - xml element
Return Type
- xml - descendants of parameter
elem
getName
Returns a string giving the expanded name of an xml element.
xml:Element e = xml `<person age="30">John</person>`; e.getName() ⇒ person
Parameters
- elem Element - xml element
Return Type
- string - element name
getTarget
function getTarget(ProcessingInstruction x) returns string
Returns the target part of the processing instruction.
xml:ProcessingInstruction p = xml `<?sort ascending?>`; p.getTarget() ⇒ sort
Parameters
- x ProcessingInstruction - xml processing instruction item
Return Type
- string - target part of parameter
x
iterator
function iterator(xml x) returns object {
public isolated function next() returns record {| ItemType value; |}?;
}
Returns an iterator over the xml items of an xml sequence.
Parameters
- x xml - xml sequence to iterate over
Return Type
- object { public isolated function next() returns record {| ItemType value; |}?; } - iterator object
Each item is represented by an xml singleton.
Each item is represented by an xml singleton.
length
Returns number of xml items in an xml value.
xml `<book><title>Sherlock Holmes</title></book><!--Novel-->`.length() ⇒ 2
Parameters
- x xml - xml item
Return Type
- int - number of xml items in parameter
x
setChildren
Sets the children of an xml element.
This panics if it would result in the element structure becoming cyclic.
xml:Element employees = xml `<employees><employee>David</employee><employee>Peter</employee></employees>`; employees.setChildren(xml `<employee>Alice</employee><employee>Bob</employee>`); employees ⇒ <employees><employee>Alice</employee><employee>Bob</employee></employees> xml:Element student = xml `<student id="1205">John</student>`; student.setChildren("Jane"); student ⇒ <student id="1205">Jane</student>
setName
Changes the name of an XML element.
xml:Element e = xml `<person>John</person>`; e.setName("student"); e ⇒ <student>John</student>
slice
Returns a subsequence of an xml value.
xml x = xml `<book>HTML</book><book>Invisible Man</book><book>David Copperfield</book><book>Jane Eyre</book>`; x.slice(2) ⇒ <book>David Copperfield</book><book>Jane Eyre</book> x.slice(1, 3) ⇒ <book>Invisible Man</book><book>David Copperfield</book>
Parameters
- x xml - the xml value
- startIndex int - start index, inclusive
- endIndex int (default x.length()) - end index, exclusive
Return Type
- xml - a subsequence of parameter
x
consisting of items with index >= parameterstartIndex
and < parameterendIndex
strip
Strips the insignificant parts of the an xml value.
Comment items, processing instruction items are considered insignificant. After removal of comments and processing instructions, the text is grouped into the biggest possible chunks (i.e., only elements cause division into multiple chunks) and a chunk is considered insignificant if the entire chunk is whitespace.
xml x = xml `<?publication year="1604"?><!--This is a play--> <book author="William Shakespeare"><title>Othello</title></book>`; x.strip() ⇒ <book author="William Shakespeare"><title>Othello</title></book>
Parameters
- x xml - the xml value
Return Type
- xml -
x
with insignificant parts removed
text
Selects all the items in a sequence that are of type xml:Text
.
xml x = xml `John<!-- middle name --><mname>Alex</mname> Doe`; x.text() ⇒ John Doe
Parameters
- x xml - the xml value
Return Type
- Text - an xml sequence consisting of selected text items
Constants
lang.xml: base
The expanded name of the xml:base
attribute.
lang.xml: lang
The expanded name of the xml:lang
attribute.
lang.xml: space
The expanded name of the xml:space
attribute.
lang.xml: XML_NAMESPACE_URI
The namespace URI bound to the xml
prefix.
lang.xml: XMLNS_NAMESPACE_URI
The namespace URI bound to the xmlns
prefix.
Union types
lang.xml: ItemType
ItemType
A type parameter that is a subtype of any singleton or empty xml sequence. Has the special semantic that when used in a declaration all uses in the declaration must refer to same type.
Xml types
lang.xml: Element
Element
Type for singleton elements. Built-in subtype of xml.
lang.xml: ProcessingInstruction
ProcessingInstruction
Type for singleton processing instructions. Built-in subtype of xml.
lang.xml: Comment
Comment
Type for singleton comments. Built-in subtype of xml.
lang.xml: Text
Text
Type for zero or more text characters. Built-in subtype of xml. Adjacent xml text items are automatically concatenated, so an xml sequence belongs to this type if it is a singleton test sequence or the empty sequence.
lang.xml: XmlType
XmlType
A type parameter that is a subtype of xml
.
Has the special semantic that when used in a declaration
all uses in the declaration must refer to same type.