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

Final remarks

parent 2d9d98ac
No related branches found
No related tags found
No related merge requests found
......@@ -8,11 +8,11 @@
<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="user.xsd"
>
<xsl:apply-templates select="person[@id]"/>
<xsl:apply-templates select="person[@id]"/> <!-- Selecting only <person> nodes having an @id attribute -->
</users>
</xsl:template>
<xsl:template match="person[@id]">
<xsl:template match="person[@id]"> <!-- <person> elements without @id attribute cannot show up -->
<user index="{position()}" uid="user_{@id}">
<uid>
<xsl:value-of select="lastName"/>
......@@ -23,8 +23,9 @@
<xsl:value-of select="lastName"/>
</fullName>
<contact>
<xsl:choose>
<xsl:choose> <!-- <xsl:choose> acts like if(...) ... else if(...) ... else {} -->
<xsl:when test="email">
<!--<xsl:attribute ...> acts on the nearest non-XSL ancestor element (<contact>) -->
<xsl:attribute name="type" select="'email'"/>
<xsl:value-of select="email"/>
</xsl:when>
......@@ -39,14 +40,5 @@
</contact>
</user>
</xsl:template>
<!-- Just in case of forgotten rules. -->
<xsl:template match="*">
<xsl:message>
<xsl:text>Error: No template matching element '</xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:text>'</xsl:text>
</xsl:message>
</xsl:template>
</xsl:stylesheet>
\ No newline at end of file
......@@ -5,12 +5,24 @@
<title></title>
</head>
<body><h2>Objective</h2><p>A list of persons being represented by an XML
document instance shall be transferred into a different XML
representation.</p><h2>Preparations</h2><p>Download XXX.zip and import it as
a project into your Eclipse workspace.</p><h2>Description</h2><p>The
imported Eclipse project contains the following sample document instance
<code>userData.xml</code>:</p><pre>&lt;persons
<body><h2>Objective</h2><p>An XML list of persons being represented by an
XML document instance shall be transformed into a different XML
representation. The idea is to turn a business description of persons into a
user database defining system logins.</p><h2>Preparations</h2><ol>
<li>Download <a
href="https://learn.mi.hdm-stuttgart.de/iliasData/goik/sdfweergdth45n3535nr3rn3Gff/xslt.zip">xslt.zip</a>
and import it as a project into your Eclipse workspace.</li>
<li>Your resulting Project named Xslt contains a file
<code>personData.xml</code> and an XSLT skeleton file
<code>person2user.xsl</code>. Define an Xslt transformation scenario
which turns <code>personData.xml</code> into <code>user.xml</code> by
means of <code>person2user.xsl</code>. <span style="color: red;">Do not
overwrite the intended sample output data file
<code>userData.xml</code></span>.</li>
</ol><h2>Description</h2><p>The imported Eclipse project contains the
following sample document instance <code>personData.xml</code>:</p><pre
style="font-family:monospace;">&lt;persons
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="person.xsd"&gt;
......@@ -20,16 +32,19 @@
&lt;phone&gt;322-33434&lt;/phone&gt;
&lt;/person&gt;
... see your Eclipse workspace for further details
... see your Eclipse workspace file personData.xml for further details
&lt;/persons&gt;</pre><p>We want to generate the following output:</p><pre>&lt;users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
&lt;/persons&gt;</pre><p>This file is valid with respect to
<code>person.xsd</code>. We want to generate the following result as being
shown in your <code>userData.xml</code> sample output file:</p><pre
style="font-family:monospace;">&lt;users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="user.xsd"&gt;
&lt;user index="1" uid="user_23"&gt;
&lt;uid&gt;White&lt;/uid&gt;
&lt;fullName&gt;Sue White&lt;/fullName&gt;
&lt;contact type="phone"&gt;322-33434&lt;/contact&gt;
&lt;/user&gt;
... see your Eclipse workspace for further details
... see your Eclipse workspace sample output file userData.xml for further details
&lt;/users&gt;</pre><p>The following conditions shall be met:</p><ul>
<li>&lt;person&gt; entries without id attribute shall be excluded</li>
......@@ -50,12 +65,11 @@
<code>&lt;email&gt;</code> nor <code>&lt;phone&gt;</code> is
present.</li>
</ul></li>
</ul><p>Both input and output may be validated against
<code>person.xsd</code> and <code>user.xsd</code>
respectively.</p><h2>ToDo</h2><p>Start from provided file
<code>person2user.xsl</code> and extend it to implement the previously
described transformation. When you are finished just upload your file
<code>person2user.xsl</code>.</p><h2>Hints</h2><p>The function
</ul><p>The generated output file shall be valid with respect to
<code>user.xsd</code>.</p><h2>ToDo</h2><p>Start from
<code>person2user.xsl</code> and implement the previously described
transformation. When you are finished upload your final
<code>person2user.xsl</code> result.</p><h2>Hints</h2><p>The function
<code>position()</code> returns the index of an element within a given node
set.</p></body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body><h1>Solution</h1><p>A possible style sheet implementation
reads:</p><pre style="font-family:monospace;"> &lt;xsl:template match="/persons"&gt;
&lt;users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="user.xsd"
&gt;
&lt;xsl:apply-templates select="person[@id]"/&gt; &lt;!-- Selecting only &lt;person&gt; nodes having an @id attribute --&gt;
&lt;/users&gt;
&lt;/xsl:template&gt;
&lt;xsl:template match="person[@id]"&gt; &lt;!-- &lt;person&gt; elements without @id attribute cannot show up --&gt;
&lt;user index="{position()}" uid="user_{@id}"&gt;
&lt;uid&gt;
&lt;xsl:value-of select="lastName"/&gt;
&lt;/uid&gt;
&lt;fullName&gt;
&lt;xsl:value-of select="firstName"/&gt;
&lt;xsl:text&gt; &lt;/xsl:text&gt;
&lt;xsl:value-of select="lastName"/&gt;
&lt;/fullName&gt;
&lt;contact&gt;
&lt;xsl:choose&gt; &lt;!-- &lt;xsl:choose&gt; acts like if(...) ... else if(...) ... else {} --&gt;
&lt;xsl:when test="email"&gt;
&lt;!--&lt;xsl:attribute ...&gt; acts on the nearest non-XSL ancestor element (&lt;contact&gt;) --&gt;
&lt;xsl:attribute name="type" select="'email'"/&gt;
&lt;xsl:value-of select="email"/&gt;
&lt;/xsl:when&gt;
&lt;xsl:when test="phone"&gt;
&lt;xsl:attribute name="type" select="'phone'"/&gt;
&lt;xsl:value-of select="phone"/&gt;
&lt;/xsl:when&gt;
&lt;xsl:otherwise&gt;
&lt;xsl:attribute name="type" select="'none'"/&gt;
&lt;/xsl:otherwise&gt;
&lt;/xsl:choose&gt;
&lt;/contact&gt;
&lt;/user&gt;
&lt;/xsl:template&gt;</pre></body>
</html>
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