Skip to content
Snippets Groups Projects
Commit 753870f0 authored by Goik Martin's avatar Goik Martin
Browse files

answers to questions handling

parent 698ef378
No related branches found
No related tags found
No related merge requests found
......@@ -652,7 +652,7 @@ drwxr-xr-x 4 goik fb1prof 4096 Nov 8 22:04 ..
</chapter>
 
<chapter xml:id="basicXmlDataModels">
<title>Basic XML data models</title>
<title>Basic XML Schema</title>
 
<para>We would like to support a company's organization. We start
modeling employees. Each employee shall have the following
......@@ -1093,6 +1093,34 @@ WHERE ID=1</programlisting>
</callout>
</calloutlist>
 
<para>This allows for an arbitrary number of employee data
sets:</para>
<programlisting>&lt;employeeList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd"&gt;
&lt;employee&gt;
&lt;id&gt;21&lt;/id&gt;
&lt;givenName&gt;Bob&lt;/givenName&gt;
&lt;surname&gt;Hope&lt;/surname&gt;
&lt;birthday&gt;1982-07-22&lt;/birthday&gt;
&lt;sex&gt;m&lt;/sex&gt;
&lt;email&gt;hope@exploitation.com&lt;/email&gt;
&lt;phone&gt;1123-33244&lt;/phone&gt;
&lt;/employee&gt;
&lt;employee&gt;
&lt;id&gt;22&lt;/id&gt;
&lt;givenName&gt;Gill&lt;/givenName&gt;
&lt;surname&gt;Evans&lt;/surname&gt;
&lt;birthday&gt;1973-01-15&lt;/birthday&gt;
&lt;sex&gt;f&lt;/sex&gt;
&lt;email&gt;gill@flashmaster.com&lt;/email&gt;
&lt;phone&gt;9771-43421&lt;/phone&gt;
&lt;/employee&gt;
&lt;/employeeList&gt;</programlisting>
<qandaset>
<title>Adding a grammar to <tag
class="starttag">mediaFormat</tag>.</title>
......@@ -1797,9 +1825,7 @@ WHERE ID=1</programlisting>
<para>We may reference the above schema constructing a valid XML
instance:</para>
 
<programlisting language="xml">&lt;employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employeeAttrib.xsd"
<programlisting language="xml">&lt;employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="employeeAttrib.xsd"
id = "21"
givenName = "Bob"
surname = "Hope"
......@@ -1871,11 +1897,121 @@ WHERE ID=1</programlisting>
applications may be more difficult than having the schema based on
<tag class="starttag">xs:element</tag> in the first place.</para>
 
<remark>Rule of thumb: Whenever you do consider a value to
possibly demand an inner structure choose &lt;xs:element&gt; in
favour of &lt;xs:attribute&gt;.</remark>
<remark>Rule of thumb: Whenever you do consider a value possibly
demanding an inner structure choose &lt;xs:element&gt; in favour
of &lt;xs:attribute&gt;.</remark>
</listitem>
</orderedlist>
<para><tag class="starttag">xs:attribute</tag> declarations offer an
optional attribute <code>use</code>:</para>
<programlisting>&lt;xs:element name="employee"&gt;
&lt;xs:complexType&gt;
...
&lt;xs:attribute name="email" type="xs:string" use="required"/&gt;
&lt;xs:attribute name="phone" type="xs:string" use="optional" default="12212-0"/&gt;
&lt;/xs:complexType&gt;
&lt;/xs:element&gt;</programlisting>
<qandaset>
<title>Attribute default choices</title>
<qandadiv>
<qandaentry xml:id="qanda_RequiredDefault">
<question>
<para>How does a <code>default = "..."</code> definition
coexists with <code>use="required"</code>?</para>
</question>
<answer>
<para>In the presence of <code>use="required"</code> a
<code>default = "..."</code> declaration is being
ignored.</para>
</answer>
</qandaentry>
<qandaentry xml:id="qand_IdElementOrAttribute">
<question>
<para>Which is a natural choice to implement id-like values as
being given in <xref linkend="fig_XsdEmployee"/>? Would you
prefer <tag class="starttag">xs:element</tag> or <tag
class="starttag">xs:attribute</tag>? Give reasons to explain
your choice.</para>
</question>
<answer>
<para>Database id values are simply numbers. Many relational
databases offer IDENTITY declarations to define at maximum one
element per table to become a data identity attribute,
typically the primary key. So there is and never will be an
inner structure and the value is unique per dataset. Thus <tag
class="starttag">xs:attribute</tag> in conjunction with
<code>use="required"</code> is a natural choice.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sec_Keys">
<title>Unique values</title>
<para>So far we know about types to ensure data integrity regarding
individual datasets. This however still permits all kind of
oddities:</para>
<figure xml:id="fig_DuplicateEmployeeValue">
<title>Duplicate value <code>id=21</code> for different
employees</title>
<programlisting>&lt;employeeList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd"&gt;
&lt;employee&gt;
&lt;id&gt;21&lt;/id&gt;
&lt;givenName&gt;Bob&lt;/givenName&gt;
...
&lt;/employee&gt;
&lt;employee&gt;
&lt;id&gt;21&lt;/id&gt; <emphasis role="bold">&lt;!-- Uups: id=21 again? --&gt;</emphasis>
&lt;givenName&gt;Gill&lt;/givenName&gt;
...
&lt;/employee&gt; ...</programlisting>
</figure>
<para>In analogy to<xref linkend="fig_SqlDdlEmployee"/> would like to
add the equivalent of a primary key to <xref
linkend="fig_ListOfEmployees"/>. XML Schema offers <tag
class="starttag">xs:key</tag> and <tag
class="starttag">xs:unique</tag> declarations for this purpose:</para>
<programlisting>&lt;xs:element name="employee"&gt;
&lt;xs:complexType&gt;
&lt;xs:sequence&gt;
&lt;xs:element name="id" type="xs:unsignedInt"/&gt;
&lt;xs:element name="givenName" type="xs:string"/&gt;
...
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;
&lt;/xs:element&gt;
&lt;xs:element name="employeeList"&gt;
&lt;xs:complexType&gt;
&lt;xs:sequence&gt;
&lt;xs:element ref="employee" minOccurs="0" maxOccurs="unbounded"/&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;
<emphasis role="bold">&lt;xs:key name="employeeId"&gt;</emphasis> <co
xml:id="co_Keydef"/>
<emphasis role="bold">&lt;xs:selector xpath="employee"/&gt;
&lt;xs:field xpath="id"/&gt;
&lt;/xs:key&gt;</emphasis>
&lt;/xs:element&gt;
</programlisting>
<para/>
</section>
</chapter>
 
......
Kennedy's birthday, maybe othe pr´erson havin email and skype
regular expression for RFC E-Mail
contact ERManager for boolean 'false' problem
/qanda.gen.xml
<lastAnswer id="q2"/>
<?xml version="1.0" encoding="UTF-8"?>
<book version="5.0" xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<info>
<title/>
<author>
<personname><firstname/><surname/></personname>
<affiliation>
<orgname/>
</affiliation>
</author>
<pubdate/>
</info>
<part xml:id="part">
<title/>
<chapter xml:id="q">
<title>Some questions</title>
<qandaset>
<title>First question</title>
<qandadiv>
<qandaentry xml:id="q1">
<question>
<para>My first question</para>
</question>
<answer>
<para>My first answer</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</chapter>
<chapter xml:id="chap2">
<title>More questions</title>
<qandaset>
<title>Second question</title>
<qandadiv>
<qandaentry xml:id="q2">
<question>
<para>My Second question</para>
</question>
<answer>
<para>My Second answer</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<qandaset>
<title>Third question</title>
<qandadiv>
<qandaentry xml:id="q3">
<question>
<para>My Third question</para>
</question>
<answer>
<para>My Third answer</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</chapter>
</part>
</book>
......@@ -4,11 +4,9 @@
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://docbook.org/ns/docbook">
<xsl:variable name="generateIdPrefix">__sdwordm4342cs__</xsl:variable>
<xsl:param name="generateIdPrefix">__sdwordm4342cs__</xsl:param>
<xsl:output indent="no"/>
<xsl:variable name="lastAnswerId" select="document('lastAnswerId.xml',/)/lastAnswer/@id" />
<xsl:param name="lastAnswerId" select="document('lastAnswerId.xml',/)/lastAnswer/@id" />
......@@ -51,22 +49,17 @@
</para>
</xsl:template>
<xsl:template match="db:question">
<question>
<xsl:apply-templates select="*|text()"/>
<xsl:if test="preceding::db:qandaentry[@xml:id=$lastAnswerId]">
<para> Solution id='<xsl:value-of select="parent::db:qandaentry/@xml:id"/>' not yet published.</para>
</xsl:if>
</question>
</xsl:template>
<xsl:template match="db:answer">
<xsl:variable name="parentQandaentry" select="parent::qandaentry"/>
<xsl:if test="following::db:qandaentry[@xml:id=$lastAnswerId] or parent::db:qandaentry[@xml:id=$lastAnswerId]">
<xsl:copy-of select="."/>
</xsl:if>
<db:answer>
<xsl:choose>
<xsl:when test="following::db:qandaentry[@xml:id=$lastAnswerId] or parent::db:qandaentry[@xml:id=$lastAnswerId]">
<xsl:copy-of select="*"/>
</xsl:when>
<xsl:otherwise>
<para>Answer id='<xsl:value-of select="parent::db:qandaentry/@xml:id"/>' not yet published.</para>
</xsl:otherwise>
</xsl:choose>
</db:answer>
</xsl:template>
<xsl:template match="db:part">
......@@ -84,7 +77,6 @@
<col width="40%" />
<tr>
<th>Exercise title / page</th>
<th>Status of completion</th>
</tr>
<xsl:for-each select=".//db:qandaset/db:qandadiv/db:qandaentry">
......@@ -96,7 +88,6 @@
</xsl:attribute>
</xref>
</td>
<td/>
</tr>
</xsl:for-each>
......
<?xml version="1.0" encoding="UTF-8"?>
<employeeList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd">
<employee>
<id>21</id>
<givenName>John</givenName>
<middleInitial>Fitzgerald</middleInitial>
<surname>Kennedy</surname>
<birthday>1928-07-22</birthday>
<sex>m</sex>
<email>kennedy@whitehouse.org</email>
</employee>
<employee>
<id>21</id>
<givenName>Gill</givenName>
<surname>Evans</surname>
<birthday>1973-01-15</birthday>
<sex>f</sex>
<email>gill@flashmaster.com</email>
<phone>9771-43421</phone>
</employee>
</employeeList>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:unsignedInt"/>
<xs:element name="givenName" type="xs:string"/>
<xs:element name="middleInitial" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="surname" type="xs:string"/>
<xs:element name="birthday" type="xs:string"/>
<xs:element name="sex" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="phone" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="employeeList">
<xs:complexType>
<xs:sequence>
<xs:element ref="employee" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:key name="employeeId">
<xs:selector xpath="employee"/>
<xs:field xpath="id"/>
</xs:key>
</xs:element>
</xs:schema>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment