Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
5.4.0 - English

Change language to:
Français - 日本語 - Português - Русский

Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function

Scilab help >> XML Management > XML Objects

XML Objects

Describe the properties of the different XML objects

Description

The nodes and their properties can be accessed and modified.

XML Document

A XML Document has two properties: root and url

  • root: the root element of the document, it is a XML Element;

  • url: the url, as a string, of the document if it exist or an empty string if it is undefined.

doc = xmlReadStr("<root><a att=""foo"" rib=""bar""><b>Hello</b></a></root>");
doc.root = doc.root.children(1);
xmlDump(doc)
doc.url = TMPDIR+"/foo.xml";
doc
xmlWrite(doc);
xmlDelete(doc);

XML Element

A XML Element has seven properties:

  • name: the tag name;

  • namespace: a XML Namespace object;

  • content: the node content as a string. For example, the content of the node A in <A>hello <B>world</B> will be the string "hello world";

  • type: the type of the node as a string. It can be on of the following values: "XML_ELEMENT_NODE", "XML_ATTRIBUTE_NODE", "XML_TEXT_NODE", "XML_CDATA_SECTION_NODE", "XML_ENTITY_REF_NODE", "XML_ENTITY_NODE", "XML_PI_NODE", "XML_COMMENT_NODE", "XML_DOCUMENT_NODE", "XML_DOCUMENT_TYPE_NODE", "XML_DOCUMENT_FRAG_NODE", "XML_NOTATION_NODE", "XML_HTML_DOCUMENT_NODE", "XML_DTD_NODE", "XML_ELEMENT_DECL", "XML_ATTRIBUTE_DECL", "XML_ENTITY_DECL", "XML_NAMESPACE_DECL", "XML_XINCLUDE_START", "XML_XINCLUDE_END", "XML_DOCB_DOCUMENT_NODE".

  • parent: the parent XML Element;

  • attributes: the node attributes as a XML Attributes object;

  • children: the children elements as a XML Node List object.

  • line: the line definition of this XML Element.

doc = xmlReadStr("<root xmlns:bar=""http://www.scilab.org/""><bar:a att=""foo"" rib=""bar""><b>Hello</b><c> world</c></bar:a></root>");
first = doc.root.children(1);
b = first.children(1);

// Add a new attribute named "new_attribute"
first.attributes.new_attribute = "value";

// Display the first child namespace
first.namespace

// Display the node content
first.content

// b has a parent
b.parent

// You can add a new child to first
first.children(3) = b

// non-integer index can be used to make insertion
first.children(1.5) = "<d> Scilab</d>"

// First child has been defined at line...
b.line

xmlDump(first)
xmlDelete(doc);

XML Attributes

A XML Attributes is a kind of hashtable which maps attributes name to attributes value. An attribute value can be accessed or modified in using the attribute name as field of this object or in using an index between 1 and attributes size.

doc = xmlReadStr("<root xmlns:bar=""http://www.scilab.org/""><bar:a att=""foo"" rib=""bar""><b>Hello</b><c> world</c></bar:a></root>");
first = doc.root.children(1);

// Read an attribute
first.attributes.att

// Set an empty attribute
first.attributes.att = "";

// Add a new attribute
first.attributes.hello = "world";

// Use an index
first.attributes(1) = "Bonjour";
first.attributes(1)

xmlDump(first)
xmlDelete(doc);

XML Namespace

A XML Namespace has two properties: href and prefix

  • href: the namespace href as a string;

  • prefix: the prefix to use for this namespace, as a string.

doc = xmlReadStr("<root xmlns:bar=""http://www.scilab.org/""><bar:a att=""foo"" rib=""bar""><b>Hello</b><c> world</c></bar:a></root>");
ns = doc.root.children(1).namespace;
ns.href
ns.prefix

xmlDelete(doc);

XML Node List

A XML Node List is a type used to enumerate the children of an element. Each element can be accessed with an integer index. Since this is a list, it is possible to make insertion of new element in it, in using double index.

The size of the list can be retrieved in using 'size' field.

The name or the contents of each node of the list can be retrieved in using 'name' or 'content' field.

doc = xmlReadStr("<root><a>Hello</a><b> world</b></root>");
c = doc.root.children;

// We check that we have two elements
c.size

// Read the first element
xmlDump(c(1))

// Replace an element by another one
c(1) = "<c>Hello</c>"

// Insert a new element between the first and the second
c(1.5) = "<d> Scilab</d>" // 1.5 or 1.234...

// Insert a new element at the tail or at the head of the list
c(0) = "<e>Head </e>"
c(217) = "<f> Tail</f>"

xmlDump(c)

// Get the nodes name
c.name

// Get the nodes contents
c.content

xmlDelete(doc);

XML XPath result set

A XML Node Set is an object returned by a XPath query. It is not possible to insert new elements or to replace existing ones. It is just possible to get them in using an integer index.

The size of the set can be retrieved with field 'size'.

doc = xmlReadStr("<root><a><b>Hello</b></a><a>World</a></root>");
s = xmlXPath(doc, "//a")
s.size

s(1).content
s(2).content

// Or ...
s.content

xmlDelete(doc);

XML Validation file

A XML Validation file is an object used to validate a document. It is possible to validate in using DTD, Relax NG or schema.

doc = xmlRead("SCI/modules/xml/tests/unit_tests/library.xml");
dtd = xmlDTD("SCI/modules/xml/tests/unit_tests/library.dtd");
schema = xmlSchema("SCI/modules/xml/tests/unit_tests/library.xsd");
rng = xmlRelaxNG("SCI/modules/xml/tests/unit_tests/library.rng");

// Validation
xmlValidate(doc, dtd);
xmlValidate(doc, rng);
xmlValidate(doc, schema);

xmlDelete("all");

See Also

  • xmlRead — Read a XML stream from a local or distant file
  • xmlReadStr — Read a XML tree from a string
  • xmlElement — Create a new XML element
  • xmlDocument — Create a new XML document
  • xmlNs — Create a new XML Namespace
  • xmlDTD — Create a XML DTD object
  • xmlSchema — Create a XML Schema object
  • xmlRelaxNG — Create a XML Relax NG object

History

VersionDescription
5.4.0 XML module introduced.
Report an issue
<< XML Management XML Management xmlAddNs >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Oct 01 17:34:52 CEST 2012