XML Namespaces (jmenné prostory)

Prefixes and Equivalence of NSs (1)

Example 1. Default NS

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <body>
      <h1>Huraaaa</h1>
   </body>
</html>

Example 2. Prefixed NS

<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>

Issues related to NS

API for XML Processing (to repeat)

XML APIs Fundamental Types

Tree-based API

Programming Language Specific Models

Document Object Model (DOM)

HTML Documents Specific DOM

DOM references

Using DOM in Java

What will we need often?

Most often used interfaces are:

Example 1 - creating DOM tree from file

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());
  }
}

Example 2 - DOM tree modification

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 3 - storing a DOM tree into an XML file

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);
   }
}

Alternative tree-based models

XML Object Model (XOM)

DOM4J - practically usable tree-based model

Tree and event-based access combinations

Events → tree

Tree → events

Virtual object models

XPath - basic principles

XPath - Application Domains

 <?xml version="1.0"?>
 <a>
  <b/>
  <b>
    <c/>
  </b>
  <b>
    <c/>
  </b>
 </a>
 //b[3]
 //b[./c]
 //b[count(./*)=0]

XPath - Application Domains

   <xsl:value-of select="./c"/>

XPath - Application Domains

XPath - terms paths and locations

XPath - syntactic rules

[20] PathExpr ::= AbsolutePathExpr | RelativePathExpr
[22] AbsolutePathExpr ::= ("/" RelativePathExpr?) | ("//" RelativePathExpr)
[23] RelativePathExpr ::= StepExpr (("/" | "//") StepExpr)*
[24] StepExpr ::= AxisStep | GeneralStep
[25] AxisStep ::= (Axis? NodeTest StepQualifiers) | AbbreviatedStep

XPath - axes

Figure 1. //b/child::*

<?xml version="1.0"?>
<a>
 <b/>
 <b>
     <c/>
 </b>
 <b>
     <c/>
 </b>
</a>

Example 3. //b/descendant::*

<?xml version="1.0"?>
<a>
 <b/>
 <b>
      <c>
          <d/>
      </c>
 </b>
 <b>
      <c/>
 </b>
</a>

Example 4. //d/parent::*

<?xml version="1.0"?>
<a>
 <b/>
 <b>
        <c>
           <d/>
        </c>
 </b>
 <b>
        <c/>
 </b>
</a>

Example 5. //d/ancestor::*

<?xml version="1.0"?>
<a>
  <b/>
  <b>
      <c>
          <d/>
      </c>
  </b>
  <b>
       <c/>
  </b>
</a>

Example 6. //b/following-sibling::*

<?xml version="1.0"?>
<a>
 <b/>
 <b>
    <c>
       <d/>
    </c>
 </b>
 <b>
     <c/>
 </b>
</a>

Example 7. //b/preceding-sibling::*

<?xml version="1.0"?>
<a>
 <b/>
 <b>
      <c>
          <d/>
      </c>
 </b>
 <b>
      <c/>
 </b>
</a>

Example 8. /a/b/c/following::*

<?xml version="1.0"?>
<a>
    <b/>
    <b>
        <c>
             <d/>
        </c>
        <e/>
    </b>
    <b>
        <c/>
    </b>
</a>

Example 9. /a/b/e/preceding::*

<?xml version="1.0"?>
<a>
    <b/>
    <b>
        <c>
            <d/>
        </c>
    </b>
    <b>
        <d/>
        <e/>
    </b>
</a>

XPath - predicates

XPath - expressions

XPath - short notation - Examples

XPath - short notation (2)

Most common used short notation is at child axis

For clarity, we keep sometimes the longer form: Do not fight it at all costs!

Further Information on XPath

XPath 2.0

XPath 2.0 - examples