1 / 28

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/

rehan
Download Presentation

XML II: XSL,XPath,XSLT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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/

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

  3. 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() + "]" );   }

  4. Handler II • public 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 );   }

  5. XML Baum • <chapter id="cmds"> • <chaptitle>FileCab</chaptitle> • <para> • This chapter describes • the commands that • manage the • <tm>FileCab</tm> • inet application. • </para> • </chapter>

  6. Knoten

  7. XSL XSL • XML-Processor • java: XALAN • c: XP XSLT Formating Objects XPath XSLT

  8. XPath I

  9. XPath Funktionen

  10. XPath String Funktionen

  11. XSL Verarbeitung

  12. <BOOK> <AUTHOR>Tom Wolfe</AUTHOR> <TITLE> One beer more </TITLE> <PRICE>60 Fr.-</PRICE> </BOOK> XSL Processor <xsl:stylesheet> <xsl:template match="TITLE"> <H1> <xsl:apply-templates /> </H1> </xsl:template > <xsl:template match="AUTHOR"/> <xsl:template match="PRICE"/> </xsl:stylesheet> <H1> One beer more </H1>

  13. xsl Befehle http://www.ibiblio.org/xml/books/bible2/chapters/ch17.html • <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> • <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"/>

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

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

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

  17. HTML

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

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

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

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

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

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

  24. 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> • ….

  25. HTML

  26. 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/>

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

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

More Related