XML

◷ Reading Time: 7 minutes

toXml

Creates XML object from a source input

source |toXml (defaultNamespace, autoPrefix)
  • source (String): A string representation of an XML, or an XML document (Mandatory)
  • defaultNamespace: the prefix of default namespace to discover and register namespaces automatically (Optional)
  • autoPrefix: true to use xpath prefixed with default namespaces, default is false (Optional)
Example:
source:= "<catalog>
  <book id='bk101'>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discount>0</discount>
  </book>
  <book id='bk102'>
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <discount>0</discount>
  </book>
</catalog>"
source|toXml () 
Result: Converts the string to XML and returns an XML object as follows.
<catalog>
  <book id='bk101'>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discount>0</discount>
  </book>
  <book id='bk102'>
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <discount>0</discount>
  </book>
</catalog>

xmlPlain

Remove all namespaces of an XML object and returns the XML object without the namespaces.

xml |xmlPlain ()
  • XML (XML): the XML object (Mandatory)
Example: 
source:= "<catalog>
  <book xmlns:bkId = 'bk101'>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discount>0</discount>
  </book>
  <book xmlns:bkId = 'bk102'>
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <discount>0</discount>
  </book>
</catalog>"
source|xmlPlain ()  
Result: Removes the namespaces from the XML object as follows.
<catalog>
  <book>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discount>0</discount>
  </book>
  <book>
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <discount>0</discount>
  </book>
</catalog>

xmlNamespaces

Returns the namespace at the root of an XML object

xml |xmlNamespaces ()
  • xml (XML): xml value to get the namespaces (Mandatory)
Example:
source:= "<catalog xmlns:bkId = 'bk101' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>
  <book xmlns:bkId = 'bk101'>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discount>0</discount>
  </book>
  <book id='bk102'>
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <discount>0</discount>
  </book>
</catalog>"
source|xmlNamespaces ()  
Result: 
[
     {
         "Prefix" : "bkId",
         "Value" : "bk101"
     },
     {
         "Prefix" : "s",
         "Value" : "http://schemas.xmlsoap.org/soap/envelope/"
     }
 ]

xmlElements

Returns list of elements of a node based on an xpath

xmlNode |xmlElements (xpath)
  • xmlNode (XML): XML object (Mandatory)
  • xpath (String): A valid xpath address in the document (Mandatory)
Example:
source:= "<catalog xmlns:bkId = 'bk101' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>
  <book xmlns:bkId = 'bk101'>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discount>0</discount>
  </book>
  <book id='bk102'>
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <discount>0</discount>
  </book>
</catalog>"
source|toXml()|xmlElements("//catalog/book")
[
  [
    <book xmlns:bkId="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <discount>0</discount>
    </book>
  ],
  [
    <book xmlns:bkId="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <discount>0</discount>
    </book>
  ]
]

xmlElement

Returns a single element of a node based on an xpath

xmlNode |xmlElement (xpath)
  • xmlNode (XML): XML object (Mandatory)
  • xpath (String): A valid xpath address in the document (Mandatory)
Example:
source:= "<catalog xmlns:bkId = 'bk101' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>
  <book xmlns:bkId = 'bk101'>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discount>0</discount>
  </book>
  <book id='bk102'>
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <discount>0</discount>
  </book>
</catalog>"
source|toXml()|xmlElement("//catalog/book")
Result:
<book xmlns:bkId="bk101">
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <discount>0</discount>
</book>

xmlAttribute

Returns an attribute element of a node based on an xpath. It also can set value of an attribute.

xmlNode |xmlAttribute (attributeName, attributeValue)
  • attributeName: (Mandatory) name of attribute
  • attributeValue: an [expression]. When this is set, it changes the value of an attribute when it exists, otherwise it adds an attribute with a name and value provided.
Examle:
source:= "<catalog xmlns:bkId = 'bk101' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>
  <book xmlns:bkId = 'bk101'>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <discount>0</discount>
  </book>
  <book id='bk102' name='testname'>
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <discount>0</discount>
  </book>
</catalog>"
source|toXml()|xmlElements("//catalog/book")[1]|xmlAttribute('id')
Result: id="bk102"

xmlAttributes

Returns all of the attributes of a collection of nodes based on an xpath. It also can set the values of all attributes.

xmlNode |xmlAttributes (attributeName, attributeValue)
  • attributeName: (Mandatory) name of attribute
  • attributeValue: an [expression]. When this is set, it changes the value of an attribute when it exists, otherwise it adds an attribute with a name and value provided.

xmlValue

Returns or sets value of element or attributes.

To return the value:

xmlNode |xmlValue ()

To set the value:

xmlNode |xmlValue (value)

xmlValues

Returns a list of values of element or attributes.

xmlNode |xmlValues ()

xmlNodes can be one or a list of nodes. Still the result will be a list with one or many values.

xmlNew

Creates a new element inside the xml node.

xmlNode |xmlNew (name)
  • name: (Mandatory) This is the name of a new element.

xmlRemove

Removes the current element and all of its children. If the node is an attribute, it removes the attribute from the node and returns the element. If the current element is a node, it deletes the node and all of its children, and will return the parent of the node.

xmlNode |xmlRemove ()

xmlParent

Returns the parent of a node.

xmlNode |xmlParent (level)
  • level: (Optional) Number of levels to navigate up the hierarchy. If not provided, default is one, which means the parent of the current xmlNode.

xmlParents

Returns all of the parents of a collection of nodes.

xmlNodes |xmlParents ()
Updated on January 14, 2021

Was this article helpful?

Related Articles