diff --git a/XmlCross/xmlcross.xml b/XmlCross/xmlcross.xml index db7da594d7042e48e8bc4e57d4ba0f37f937da2d..e4a6e9efe1d997fb24c96a0ad6530cb359412e37 100644 --- a/XmlCross/xmlcross.xml +++ b/XmlCross/xmlcross.xml @@ -208,7 +208,7 @@ <para>Single source publishing aims at creating different output formats from a given document source:</para> - <figure xml:id="xmlc_fig_MultiFormat"> + <figure xml:id="xmlc_fig_sourceToMultiFormat"> <title>Single source publishing</title> <mediaobject> @@ -2423,6 +2423,9 @@ xmlns="http://docbook.org/ns/docbook"> <qandadiv> <qandaentry> <question> + <para>In this exercise we develop a simple grammar aiming at + <quote>book</quote> style documents.</para> + <para>Read the <link xlink:href="http://relaxng.org/tutorial-20011203.html">RELAX NG Tutorial</link>. You may want to import the examples into your @@ -2436,6 +2439,7 @@ xmlns="http://docbook.org/ns/docbook"> schema suiting the subsequent document sample:</para> <programlisting language="xml"><book lang="en"> + <title>My first book</title> <chapter> <title>Introduction</title> <paragraph>Some text.</paragraph> @@ -2476,8 +2480,9 @@ xmlns="http://docbook.org/ns/docbook"> </listitem> <listitem> - <para>A <tag class="starttag">book</tag> element must have at - least one <tag class="starttag">chapter</tag>.</para> + <para>A <tag class="starttag">book</tag> element contains a + <tag class="starttag">title</tag> followed by at least one + <tag class="starttag">chapter</tag>.</para> </listitem> <listitem> @@ -2541,5 +2546,297 @@ xmlns="http://docbook.org/ns/docbook"> </qandaset> </chapter> + <chapter annotations="slide" xml:id="xmlc_chap_transform"> + <title>Transforming documents</title> + + <figure xml:id="xmlc_fig_problemConvert"> + <title>Format conversion problem</title> + + <para>Problem regarding <xref + linkend="xmlc_fig_sourceToMultiFormat"/>:</para> + + <informaltable border="0"> + <tr> + <td valign="top"><programlisting language="xml"><book version="5.1" ...> + ... + <chapter> + <title>Introduction</title> + <para>First section.</para> + </chapter> ... +</book></programlisting></td> + + <td valign="top"><programlisting language="xml"><html> + <head>...</head> + <body> + <h1>Introduction</h1> + <p>First section.</p> ... + </body> +</html></programlisting></td> + </tr> + </informaltable> + </figure> + + <figure xml:id="xmlc_fig_convertByTemplates"> + <title><xref linkend="glo_XSL"/> template rules</title> + + <programlisting language="xml"><xsl:template match="/book"> + <html> + <head> ... </head> + <body> + <h1> + <xsl:value-of select="title"/> + </h1> + </body> + </html> +</xsl:template></programlisting> + </figure> + + <figure xml:id="xmlc_fig_xsl_para"> + <title>Example: Formatting <tag class="starttag">title</tag> + elements</title> + + <informaltable border="1"> + <tr> + <td rowspan="3"><programlisting language="xml"><xsl:template match="title"> + <h1> + <xsl:value-of select="."/> + </h1> +</xsl:template></programlisting></td> + + <td><programlisting language="xml"><title>Some content</title> </programlisting></td> + </tr> + + <tr> + <td><para>gets converted to:</para></td> + </tr> + + <tr> + <td><programlisting language="xml"><h1>Some content</h1> </programlisting></td> + </tr> + </informaltable> + </figure> + + <qandaset defaultlabel="qanda" xml:id="xmlc_qanda_xslMyDocbook"> + <title>Formatting <tag class="starttag">book</tag> instances</title> + + <qandadiv> + <qandaentry> + <question> + <para>In <xref linkend="xmlc_qandaBookGrammar"/> you developed a + grammar being capable to describe <xref linkend="glo_XML"/> + document instances. This exercise aims at transforming arbitrary + instances into <xref linkend="glo_HTML"/>.</para> + + <para>Start by reading the beginning of the <link + xlink:href="https://www.w3schools.com/xml/xsl_intro.asp">XSLT + Tutorial</link> and follow the subsequent steps:</para> + + <orderedlist> + <listitem> + <para>Provide a reasonable instance example like:</para> + + <programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?> +<?xml-model href="mybook.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?> +<book> + <title>Introducing Java</title> + <chapter> + <title>General</title> + <paragraph>Java is a programming language offering:</paragraph> + <itemizedlist> + + <listitem> + <paragraph>Procedural elements</paragraph> + <itemizedlist> + <listitem> + <paragraph>Control structures: if/else, switch</paragraph> + </listitem> + <listitem> + <paragraph>Loops: while, do ... while, for</paragraph> + </listitem> + <listitem> + <paragraph>prcedures (static methods)</paragraph> + </listitem> + </itemizedlist> + </listitem> + <listitem> + <paragraph>OO support albeit limited (no multiple inheritance implementation support)</paragraph> + </listitem> + <listitem> + <paragraph>Functional elements</paragraph> + </listitem> + + </itemizedlist> + </chapter> + <chapter> + <title>JDK</title> + <paragraph>The Java developers kit (JDK) provides both a runtime environment + and a compiler.</paragraph> + </chapter> +</book></programlisting> + </listitem> + + <listitem> + <para>Create a <link + xlink:href="https://www.oxygenxml.com/doc/versions/18/ug-editor/topics/defining-new-transformation-scenario.html">XML + Transformation with XSLT</link> starting from the following + skeleton:</para> + + <programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?> +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + xmlns:xs="http://www.w3.org/2001/XMLSchema" + exclude-result-prefixes="xs" + version="2.0"> + + <xsl:output method="xml" indent="yes"/> + <xsl:template match="/book"> + <html> + <head> + <title> + <xsl:value-of select="title"/> + </title> + </head> + <body> + <h1> + <xsl:value-of select="title"/> + </h1> + <!-- See match="chapter" template below --> + <xsl:apply-templates select="chapter"/> + </body> + </html> + </xsl:template> + + <xsl:template match="chapter"> + <!-- TODO: Implement me! --> + </xsl:template> + + + <!-- TODO: More templates have to be defined + addressing <title>, <paragraph>, ... + --> + + + <!-- Protecting against missing template definitions --> + <xsl:template match="*"> + <p style="color:red;"> + <xsl:text>No template defined for element '</xsl:text> + <xsl:value-of select="name(.)"/> + <xsl:text>'</xsl:text> + </p> + </xsl:template> + +</xsl:stylesheet></programlisting> + </listitem> + + <listitem> + <para>Test your transformation. The output should be quite + limited containing only your document's title string:</para> + + <programlisting language="xml"><html> + <head> + <title>Introducing Java</title> + </head> + <body> + <h1>Introducing Java</h1> + </body> +</html></programlisting> + </listitem> + + <listitem> + <para>Extend the XSL stylesheet in a step- by- step fashion + until finally producing:</para> + + <programlisting language="xml"><?xml version="1.0" encoding="UTF-8"?> +<html> + <head> + <title>Introducing Java</title> + </head> + <body> + <h1>Introducing Java</h1> + <h2>General</h2> + <p>Java is a programming language offering:</p> + <ul> + + <li> + <p>Procedural elements</p> + <ul> + <li> + <p>Control structures: if/else, switch</p> + </li> + <li> + <p>Loops: while, do ... while, for</p> + </li> + <li> + <p>prcedures (static methods)</p> + </li> + </ul> + </li> + <li> + <p>OO Support albeit limited (no multiple inheritance implementation support)</p> + </li> + <li> + <p>Functional elements</p> + </li> + + </ul> + + <h2>JDK</h2> + <p>The Java developers kit (JDK) provides both a runtime environment + and a compiler.</p> + </body> +</html></programlisting> + </listitem> + </orderedlist> + + <tip> + <para>Follow the embedded comment hints provided inside the + <xref linkend="glo_XSL"/> skeleton. You'll have to define more + <tag class="starttag">xsl:template match="..."</tag> rules and + <tag class="starttag">xsl:apply-templates ...</tag> calls at + appropriate places.</para> + </tip> + </question> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="xmlc_qanda_xslMyDocbookLangColor"> + <title>Providing red background indicating foreign phrases</title> + + <qandadiv> + <qandaentry> + <question> + <para>Extend the previous exercise by providing highlighting to + indicate uses of foreign languages. Let's consider:</para> + + <programlisting language="xml"><paragraph>I am normal</paragraph> <!-- Default language --> +<paragraph lang="rf">Je suis Français</paragraph> <!-- Non-default language --></programlisting> + + <para>The intended output being:</para> + + <programlisting language="xml"><p>I am normal</p> +<p <emphasis role="bold">style="background-color: red;"</emphasis>>Je suis Français</p></programlisting> + + <tip> + <para>You may want to define two templates like:</para> + + <programlisting language="xml"><!-- Default language --> +<xsl:template match="paragraph"> + ... +</xsl:template> + +<!-- Non-default language --> +<xsl:template match="paragraph[@lang]"> + ... +</xsl:template></programlisting> + + <para>Read the documentation regarding the + <code>paragraph[@lang]</code> XPath syntax.</para> + </tip> + </question> + </qandaentry> + </qandadiv> + </qandaset> + </chapter> + <xi:include href="../Doc/Common/glossary.xml" xpointer="element(/1)"/> </book>