28
XML II: XSL,XPath,XSLT http://www.w3.org/ http://www-106.ibm.com/ developerworks / http:// www.alphaworks.ibm.com / xml http:// www.oasis-open.org / cover / http:// www.topxml.com / http:// www.aifb.uni-karlsruhe.de /~ mer / xml.html http:// java.sun.com / xml / jaxp / dist /1.1/ docs / tutorial / http://www.w3schools.com/ xml / http:// www.xml.com /

XML II: XSL,XPath,XSLT

Embed Size (px)

Citation preview

Page 1: XML II: XSL,XPath,XSLT

XML II: XSL,XPath,XSLT

http://www.w3.org/

http://www-106.ibm.com/developerworks/

http://www.alphaworks.ibm.com/xml

http://www.oasis-open.org/cover/

http://www.topxml.com/

http://www.aifb.uni-karlsruhe.de/~mer/xml.html

http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/

http://www.w3schools.com/xml/

http://www.xml.com/

Page 2: XML II: XSL,XPath,XSLT

Nachtrag Parsen mit SAX<?xml version="1.0"?>

<CustomerOrder>   <Customer>      <Name> Customer X </Name>      <Address> unknown  </Address>   </Customer>

   <Items>      <Item>         <ProductCode> 098 </ProductCode>         <Name> Item 1 </Name>         <Price> 32.01 </Price>      </Item>      <Item>         <ProductCode> 4093 </ProductCode>         <Name> Item 2 </Name>         <Price> 0.76 </Price>      </Item>      <Item>         <ProductCode> 543 </ProductCode>         <Name> Item 3 </Name>         <Price> 1.42 </Price>      </Item>   </Items>

</CustomerOrder>

Page 3: XML II: XSL,XPath,XSLT

Handler I…public class MyHandler extends DefaultHandler {

  private Stack tagStack = new Stack();

   private Vector items = new Vector();

   private String customer;

   private CharArrayWriter contents = new CharArrayWriter();

   public void startElement( String namespaceURI,      String localName, String qName, Attributes attr ) throws SAXException {

       contents.reset();

       tagStack.push( localName );

       System.out.println( "path found: [" + getTagPath() + "]" );

   }

  

Page 4: XML II: XSL,XPath,XSLT

Handler IIpublic void endElement( String namespaceURI,

   String localName, String qName ) throws SAXException {

      if ( getTagPath().equals( "/CustomerOrder/Customer/Name" ) ) {         customer = contents.toString().trim();      }      else if ( getTagPath().equals( "/CustomerOrder/Items/Item/Name" ) ) {         items.addElement( contents.toString().trim() );      }

      // clean up the stack...      tagStack.pop();   }

public void characters( char[] ch, int start, int length )  throws SAXException {      contents.write( ch, start, length );   }

Page 5: XML II: XSL,XPath,XSLT

XML Baum

<chapter id="cmds">

<chaptitle>FileCab</chaptitle>

<para>

This chapter describes

the commands that

manage the

<tm>FileCab</tm>

inet application.

</para>

</chapter>

Page 6: XML II: XSL,XPath,XSLT

Knoten

Node Type: Inhalt

Root Wurzel Element

Element Element Knoten

Text Text zwischen den Tags

Attribute Attribut Knoten

Namespace URI des Namensraums

Processing instruction

Inhalt der PI, <? or ?>

Comment The text of the comment,

Page 7: XML II: XSL,XPath,XSLT

XSL

XML-Processor

java: XALAN

c: XP

XSLT

XSL

XSLTFormating

ObjectsXPath

Page 8: XML II: XSL,XPath,XSLT

XPath IAxis: Selects From:

ancestor The parent of the context node, the parent of the parent of the context node, the parent of the parent of the parent of the context node, and so forth back to the root node

ancestor-or-self The ancestors of the context node and the context node itself

attribute The attributes of the context node

child The immediate children of the context node

descendant The children of the context node, the children of the children of the context node, and so forth

descendant-or-self

The context node itself and its descendants

following All nodes that start after the end of the context node, excluding attribute and namespace nodes

following-sibling All nodes that start after the end of the context node and have the same parent as the context node

namespace The namespace of the context node

parent The unique parent node of the context node

preceding All nodes that finish before the beginning of the context node, excluding attribute and namespace nodes

preceding-sibling All nodes that start before the beginning of the context node and have the same parent as the context node

self The context node

. self::node()

.. parent::node()

name child::name

@name attribute::name

// /descendant-or-self::node()/

Page 9: XML II: XSL,XPath,XSLT

XPath Funktionen

Function: Return Type:

Returns:

position() number The position of the context node in the context node list; the first node in the list has position 1

last() number The number of nodes in the context node list; this is the same as the position of the last node in the list

count(node-set) number The number of nodes in node-set.

id(string1 string2 string3…) node set A node set containing all the elements anywhere in the same document that have an ID named in the argument list; the empty set if no element has the specified ID.

key(string name, Object value) node set A node set containing all nodes in this document that have a key with the specified value. Keys are set with the top-level xsl:key element.

document(string URI, string base)

node set A node set in the document referred to by the URI; the nodes are chosen from the named anchor or XPointer used by the URI. If there is no named anchor or XPointer, then the root element of the named document is the node set. Relative URIs are relative to the base URI given in the second argument. If the second argument is omitted, then relative URIs are relative to the URI of the style sheet (not the source document!).

local-name(node set) string The local name (everything after the namespace prefix) of the first node in the node set argument; can be used without any arguments to get the local name of the context node.

namespace-uri(node set) string The URI of the namespace of the first node in the node set; can be used without any arguments to get the URI of the namespace of the context node; returns an empty string if the node is not in a namespace.

name(node set) string The qualified name (both prefix and local part) of the first node in the node set argument; can be used without an argument to get the qualified name of the context node.

generate-id(node set) string A unique identifier for the first node in the argument node set; can be used without any argument to generate an ID for the context node.

Page 10: XML II: XSL,XPath,XSLT

XPath String FunktionenFunction: Return

Type: Returns:

starts-with(main_string, prefix_string)

Boolean True if main_string starts with prefix_string; false otherwise

contains(containing_string, contained_string)

Boolean True if the contained_string is part of the containing_string; false otherwise

substring(string, offset, length) String length characters from the specified offset in string; or all characters from the offset to the end of the string if length is omitted; length and offset are rounded to the nearest integer if necessary

substring-before(string, marker-string)

String The part of the string from the first character up to (but not including) the first occurrence of marker-string

substring-after(string, marker-string)

String The part of the string from the end of the first occurrence of marker-string to the end of string; the first character in the string is at offset 1

string-length(string) Number The number of characters in string

normalize-space(string) String The string after leading and trailing white space is stripped and runs of white space are replaced with a single space; if the argument is omitted the string value of the context node is normalized

translate(string, replaced_text, replacement_text)

String Returns string with occurrences of characters in replaced_text replaced by the corresponding characters from replacement_text

concat(string1, string2, . . . ) String Returns the concatenation of as many strings as are passed as arguments in the order they were passed

format-number(number, format-string, locale-string)

String Returns the string form of number formatted according to the specified format-string as if by Java 1.1's java.text.DecimalFormat class (see http://java.sun.com/products/jdk/1.1/docs/api/java.text.DecimalFormat.html); the locale-string is an optional argument that provides the name of the xsl:decimal-format element used to interpret the format-string

Page 11: XML II: XSL,XPath,XSLT

XSL Verarbeitung

Page 12: XML II: XSL,XPath,XSLT

<BOOK> <AUTHOR>Tom Wolfe</AUTHOR> <TITLE> One beer more </TITLE> <PRICE>60 Fr.-</PRICE></BOOK>

<xsl:stylesheet> <xsl:template match="TITLE"><H1> <xsl:apply-templates /></H1></xsl:template >

<xsl:template match="AUTHOR"/><xsl:template match="PRICE"/></xsl:stylesheet>

XSL Processor

<H1>One beer more</H1>

Page 13: XML II: XSL,XPath,XSLT

xsl Befehle http://www.ibiblio.org/xml/books/bible2/chapters/ch17.html

<xsl:template match="ATOM"> <xsl:apply-templates/> <xsl:call-template name="html-head"/><xsl:value-of select="NAME"/>

<xsl:for-each select="ATOM"><xsl:copy-of select=".."/>

<xsl:element name=„ATOM"> <xsl:attribute name="HREF">

<xsl:number count="*"/> <xsl:sort select="ATOMIC_NUMBER"/>

<xsl:if test="position()!=last()">, </xsl:if> <xsl:choose><xsl:variable name="id" select="ATOM">

<xsl:import href="genealogy.xsl"/>

<xsl:for-each select="ATOM"> <xsl:value-of select="."/> </xsl:for-each>

<xsl:choose>

<xsl:when test="@STATE='SOLID'">

<P style="color: black">

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

</xsl:when>

<xsl:otherwise>

<P style="color: green">

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

</xsl:otherwise>

</xsl:choose>

Page 14: XML II: XSL,XPath,XSLT

Beispiel Legi

XML Daten

<?xml version="1.0"?><Legi><Nachname>Burkhart</Nachname><Vorname>Helmar</Vorname><AnzSem>63</AnzSem><Dept>Informatik</Dept><Matr.Nr.>70-061-634</Matr.Nr.><Photo image="helmar.burkhart" /></Legi>

Page 15: XML II: XSL,XPath,XSLT

XSL I <?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0"> <xsl:template match="Legi"><html> <head> <title>UNIBS DataBase </title> </head><body bgcolor="#1b481a" text="#51d64e" aLink="#ffffff"><table border="0" cellpadding="0" cellspacing="2" width="481"><tr> <td><img src="signet3b.gif" width="57" height="80" border="0" /></td><td valign="bottom" align="center"><tt><h1>Studierenden-Datenbank der Uni

Basel</h1></tt></td> </tr> <tr><td colspan="3"> <hr size="1" color="#51d64e" noshade="true" /> </td></tr> <tr><td><xsl:apply-templates select="Photo"/></td><td><tt>

<p> <xsl:text disable-output-escaping="yes">&amp;nbsp;&amp;nbsp;</xsl:text> Name: <xsl:apply-templates select="Nachname"/></p> <p><xsl:text disable-output-escaping="yes">&amp;nbsp;&amp;nbsp;</xsl:text> Vorname: <xsl:apply-templates select="Vorname"/></p> …</td>

Page 16: XML II: XSL,XPath,XSLT

XSL II<td></td></tr><tr><td colspan="3"> <hr size="1" color="#51d64e" noshade="true" /></td></tr><tr><td colspan="3"><tt><font size="-1">Informationstag f&#x00fc;r Maturandinnen und Maturanden an der Universit&#x00e4;t Basel, 11. Januar 2002</font></tt></td> </tr> </table> </body></html></xsl:template>

<xsl:template match="Nachname"> <xsl:value-of select="."/></xsl:template>

<xsl:template match="Vorname"> <xsl:value-of select="."/></xsl:template>

<xsl:template match="Photo"> <img src="../img/{@image}" width="180" height="240" border="0" /></xsl:template>

</xsl:stylesheet>

Page 17: XML II: XSL,XPath,XSLT

HTML

Page 18: XML II: XSL,XPath,XSLT

Links automatisch generieren

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="newid.xsl"?><chapter><para>Then with expanded wings he steers his flight</para><figure><title>"Incumbent on the Dusky Air"</title><graphic fileref="pic1.jpg"/></figure><para>Aloft, incumbent on the dusky Air</para><sect1><para>That felt unusual weight, till on dry Land</para><figure><title>"He Lights"</title><graphic fileref="pic2.jpg"/></figure><para>He lights, if it were Land that ever burned</para><sect2><para>With solid, as the Lake with liquid fire</para><figure><title>"The Lake with Liquid Fire"</title><graphic fileref="pic3.jpg"/></figure></sect2></sect1></chapter>

Page 19: XML II: XSL,XPath,XSLT

XSL<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/>

<xsl:template match="chapter"> <html><body>

<b>Pictures:</b><br/> <xsl:for-each select="descendant::figure"> <a href="#{generate-id(graphic)}"> <xsl:value-of select="title"/></a><br/> </xsl:for-each> <xsl:apply-templates/> </body></html> </xsl:template>

Page 20: XML II: XSL,XPath,XSLT

XSL II

<xsl:template match="para">

<p><xsl:apply-templates/></p>

</xsl:template>

<xsl:template match="graphic">

<!-- Image and title as caption, centered. -->

<center><a name="{generate-id(.)}"><img src="{@fileref}"/></a>

<b><xsl:value-of select="../title"/></b></center>

</xsl:template>

</xsl:stylesheet>

Page 21: XML II: XSL,XPath,XSLT

HTML I

<html><body><b>Pictures:</b><br><a href="#N1000C">"Incumbent on the Dusky Air"</a><br><a href="#N1001B">"He Lights"</a><br><a href="#N1002A">"The Lake with Liquid Fire"</a><br>

<p>Then with expanded wings he steers his flight</p>

<center><a name="N1000C"><img src="pic1.jpg"></a><b>"Incumbent on the Dusky Air"</b></center>

<p>Aloft, incumbent on the dusky Air</p>

Page 22: XML II: XSL,XPath,XSLT

HTML II<p>That felt unusual weight, till on dry Land</p>

<center><a name="N1001B"><img src="pic2.jpg"></a><b>"He Lights"</b></center>

<p>He lights, if it were Land that ever burned</p>

<p>With solid, as the Lake with liquid fire</p>

<center><a name="N1002A"><img src="pic3.jpg"></a><b>"The Lake with Liquid Fire"</b></center>

</body></html>

Page 23: XML II: XSL,XPath,XSLT

Xalan

set JAVA_HOME=C:\Programme\jbuilder5\jdk1.3

set _CLASSES="C:\xml\xalan-j_2_2_D14\bin\xalan.jar;C:\xml\xerces-1_4_4\xerces.jar"

%JAVA_HOME%\bin\java -cp %_CLASSES% org.apache.xalan.xslt.Process -IN buch.xml -XSL newid.xsl -OUT chapter.html

Page 24: XML II: XSL,XPath,XSLT

Glossar<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE glossar SYSTEM "glossar.dtd"><?xml-stylesheet type="text/xsl" href="glossar.xslt"?><glossar>

<eintrag modul="2" id="jaxp"><begriff>JAXP</begriff><erklaerung>

<kurzinfo>Java API for XML Parsing</kurzinfo> <para>JAXP von java.sun.com definiert eine allgemeine Schnittstelle zur Verarbeitung von XML-

Dokumenten, indem es eine weitere Schicht über die vorhandenen APIs (<verweis auf="dom">DOM</verweis> und <verweis auf="sax">SAX</verweis>) legt. Zusätzlich wird auch für <verweis auf="xslt">XSLT-Verarbietung</verweis> ein herstellerunabhängiges API realisiert.</para><para>Die einzelnen Implementierungen von <verweis auf="dom">DOM</verweis>- und <verweis auf="sax">SAX</verweis>-Parsern bieten unterschiedliche Schnittstellenfunktionen, bzw. unterschiedliche Ausprägungen an. Schwerwiegender noch definiert die XSLT-Recommendation des <verweis auf="w3c">W3C</verweis> lediglich die Sprache und nicht wie diese implementiert werden soll. Für XSLT existiert bislang noch kein API. Von Anwendungsprogrammen können lediglich die unterschiedlichen XSLT-Prozessoren aufgerufen werden. <link href="http://java.sun.com/xml/jaxp.html" title="JAXP"/> bietet damit erstmals eine einheitliche Schnittstelle für die Verarbeitung von XSLT auf Java-Seite an.</para></erklaerung></eintrag>

….

Page 25: XML II: XSL,XPath,XSLT

HTML

Page 26: XML II: XSL,XPath,XSLT

A, B , C, …<xsl:template match="glossar"> <a name="top"/> <table BORDER="0" CELLSPACING="0" CELLPADDING="0"> <tr> <td BGCOLOR="#000064"><b><font face="Arial,Helvetica" size="+1" color="#FFFFFF"> Glossar zu XML und XML-basierten Spezifikationen&#160;</font></b></td></tr> </table> <p> <i> <font face="Arial" color="#FF0000" size="2">Das Glossar befindet sich noch im Aufbau.</font></i></p> <hr/> <!-- hier werden die links auf die Eintragsanfänge gemacht --> <xsl:for-each select="eintrag"> <xsl:sort select="begriff"/> <xsl:variable name="initiale" select="substring(begriff,1,1)"/> <xsl:if test="not(preceding::node()[substring(begriff,1,1)=$initiale])"> <a> <xsl:attribute name="href">#<xsl:value-of select="$initiale"/></xsl:attribute> <xsl:value-of select="$initiale"/> </a> <!-- Leerzeichen zwischen den Initialen nur nötig, wenn nicht der letzte Eintrag --> <xsl:if test="not(position()=last())"> <xsl:text>&#160;</xsl:text> </xsl:if> </xsl:if> </xsl:for-each> <xsl:text>&#160;</xsl:text> <a href="#_index">[ Index ]</a> <hr/>

Page 27: XML II: XSL,XPath,XSLT

Formatierung Eintrag I<xsl:template match="eintrag"> <!-- muss ein name-Eintrag generiert werden, oder gibt es schon einen Artikel mit der selben Initiale? --> <xsl:variable name="initiale" select="substring(begriff,1,1)"/> <xsl:if test="not(preceding::node()[substring(begriff,1,1)=$initiale])"> <a> <xsl:attribute name="name"><xsl:value-of select="substring(begriff,1,1)"/></xsl:attribute> </a> </xsl:if> <table cellpadding="4" cellspacing="4"> <tr> <td valign="top" width="120px"> <font size="+1"> <a> <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute> </a> <xsl:value-of select="begriff"/> </font> </td> <td valign="top"> <xsl:apply-templates/> </td> </tr> </table> <a href="#top">Top</a> <xsl:text>&#160;</xsl:text> <a href="#_index">Index</a> <hr/> </xsl:template>

Page 28: XML II: XSL,XPath,XSLT

Formatierung Eintrag II<xsl:template match="erklaerung"> <xsl:apply-templates/> </xsl:template> <xsl:template match="kurzinfo"> <font size="+1"> <xsl:apply-templates/> </font> <br/> </xsl:template> <xsl:template match="para"> <p> <xsl:apply-templates/> </p> </xsl:template> <xsl:template match="verweis"> <xsl:text>&#160;</xsl:text> <a> <xsl:attribute name="href">#<xsl:value-of select="@auf"/></xsl:attribute> <xsl:value-of select="."/> </a> <xsl:text>&#160;</xsl:text> </xsl:template>