<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body>
<h1>Huraaaa</h1>
</body>
</html>
xmlns:prefix="URI"
.
:
) is denoted as
Qualified Name, QName.
xmlns=
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body>
<h1>Huraaaa</h1>
</body>
</html>
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<xhtml:body>
<xhtml:h1>Huraaaa</xhtml:h1>
</xhtml:body>
</xhtml:html>
xi:include
and include
even
if they belong to the same NS and should thus have the same
interpretation/meaning for applications.
org.w3c.dom
.
Most often used interfaces are:
Element
corresponds to the element in a logical document structure. It allows
us to access name of the element, names of attributes, child nodes
(including textual ones). Useful methods:
Node getParentNode()
- returns the parent node
String getTextContent()
- returns textual content of the element.
NodeList getElementsByTagName(String name)
- returns the list
of ancestors (child nodes and their ancestors) with the given name.
Node
super interface of Element
, corresponds to the general node in a logical
document structure, may contain element, textual node, comment, etc.
NodeList
a list of nodes (a result of calling getElementsByTagName
for example).
It offers the following methods for its processing:
int getLength()
- returns the number of nodes in a list
Node item(int index)
- returns the node at position index
Document
corresponds to the document node (its a parent of a root element)
public class Task1 {
private Task1(URL url) throws SAXException,
ParserConfigurationException, IOException {
// We create new instance of factory class
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// We get new instance of DocumentBuilder using the factory class.
DocumentBuilder builder = factory.newDocumentBuilder();
// We utilize the DocumentBuilder to process an XML document
// and we get document model in form of W3C DOM
Document doc = builder.parse(url.toString());
}
}
public class Task1 {
private Document doc;
// Method for a salary modification.
// If the person’s salary is less then
// minimum, the salary will increased to minimum.
// No action is performed for the other persons.
public void adjustSalary(double minimum) {
// get the list of salaries
NodeList salaries = doc.getElementsByTagName("salary");
for (int i = 0; i < salaries.getLength(); i++) {
// get the salary element
Element salaryElement = (Element) salaries.item(i);
// get payment
double salary = Double.parseDouble(
salaryElement.getTextContent());
if (salary < minimum) {
// modify the text node/content of element
salaryElement.setTextContent(String.valueOf(minimum));
}
}
}
}
Example of the method storing a DOM tree into a file (see Homework 1). The procedure utilizes a transformation we do not know yet. Let use it as a black box.
public class Task1 {
private Document doc;
public void serializetoXML(File output) throws IOException,
TransformerConfigurationException {
// We create new instance of a factory class.
TransformerFactory factory
= TransformerFactory.newInstance();
Transformer transformer
= factory.newTransformer();
// The input is the document placed in a memory
DOMSource source = new DOMSource(doc);
// The transformation output is the output file
StreamResult result = new StreamResult(output);
// Let’s make the transformation
transformer.transform(source, result);
}
}
<?xml version="1.0"?>
<a>
<b/>
<b>
<c/>
</b>
<b>
<c/>
</b>
</a>
//b[3]
//b[./c]
//b[count(./*)=0]
<xsl:value-of select="./c"/>
[20] PathExpr ::= AbsolutePathExpr | RelativePathExpr
[22] AbsolutePathExpr ::= ("/" RelativePathExpr?) | ("//" RelativePathExpr)
[23] RelativePathExpr ::= StepExpr (("/" | "//") StepExpr)*
[24] StepExpr ::= AxisStep | GeneralStep
[25] AxisStep ::= (Axis? NodeTest StepQualifiers) | AbbreviatedStep
child
- contains direct child nodes of CN
descendant
- contains all descendants of CN except attributes.
parent
- contains the CN parent nod (if it exists)
ancestor
- contains all ancestors of CN - means parents, grandparents, etc to a root element (if the CN is not the root element itself)
following-sibling
- contains all following siblings of CN (the axis is empty for NS and attributes)
preceding-sibling
- dtto, but it contains the preceding sibling.
following
- contains all nodes following the CN (except the attributes, child nodes and NS nodes)
preceding
- dtto, but contains preceding nodes (except ancestors, attributes, NS)
attribute
- contains attributes (for elements only)
namespace
- contains all NS nodes of CN (for elements only)
self
- the CN itself
descendant-or-self
- contains the union of descendant and self axes
ancestor-or-self
- contains the union of ancestor and self axes
<?xml version="1.0"?>
<a>
<b/>
<b>
<c/>
</b>
<b>
<c/>
</b>
</a>
<?xml version="1.0"?>
<a>
<b/>
<b>
<c>
<d/>
</c>
</b>
<b>
<c/>
</b>
</a>
<?xml version="1.0"?>
<a>
<b/>
<b>
<c>
<d/>
</c>
</b>
<b>
<c/>
</b>
</a>
<?xml version="1.0"?>
<a>
<b/>
<b>
<c>
<d/>
</c>
</b>
<b>
<c/>
</b>
</a>
<?xml version="1.0"?>
<a>
<b/>
<b>
<c>
<d/>
</c>
</b>
<b>
<c/>
</b>
</a>
<?xml version="1.0"?>
<a>
<b/>
<b>
<c>
<d/>
</c>
</b>
<b>
<c/>
</b>
</a>
<?xml version="1.0"?>
<a>
<b/>
<b>
<c>
<d/>
</c>
<e/>
</b>
<b>
<c/>
</b>
</a>
<?xml version="1.0"?>
<a>
<b/>
<b>
<c>
<d/>
</c>
</b>
<b>
<d/>
<e/>
</b>
</a>
para
selects all child nodes of context node with name para
*
selects all element children of the context node
text()
selects all text node children of the context node
@name
selects the name attribute of the context node
@*
selects all the attributes of the context node
para[1]
selects the first para
child of the context node
para[last()]
selects the last para
child of the context node
*/para
selects all para grandchildren of the context node
/doc/chapter[5]/section[2]
selects the second section of the fifth chapter of the doc
chapter//para
- selects all descendants of element chapter
with name para
//para
- selects all elements para
in the document
//olist/item
- selects all elements item
with parent element olist
.//para
selects all descendant nodes of CN with name para
..
selects the parent node of CN
../@lang
selects a lang attribute of CN parent node
Most common used short notation is at child axis
child::article/child::para
.
para[@type="warning"]
instead of
child::para[attribute::type="warning"]
//
instead of
/descendant-or-self::node()/
.
and ..
For clarity, we keep sometimes the longer form: Do not fight it at all costs!