[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] xhtml via xslt failure


Subject: Re: [xsl] xhtml via xslt failure
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxx>
Date: Mon, 16 Dec 2013 10:40:44 -0500

Hi,

Yes, lots to learn here.

The first thing to learn is how to use templates.

Try splitting your single template (matching 'author') into two, one
to match the document element ('bookstore') and one to match the
'author' elements you wish to list:

<xsl:template match="bookstore">
   <html lang='en' xml:lang='en'>
    <head>
      <meta name='note' content='xhtml document created by xml
transformation' />
      <title>Test output web page</title>
    </head>
    <body>
       <xsl:apply-templates select="//author"/>
    </body>
  </html>
</xsl:template>

Why match 'bookstore'? Because the bookstore element corresponds
structurally to the 'html' element you will generate. You want one
HTML document; you have one 'bookstore'.

Then:

<xsl:template match="author">
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

This will generate a 'p' element in your result whenever an 'author'
element is processed.

All 'author' elements will be processed by virtue of the
xsl:apply-templates select="//author" in the first template.

One problem remains: the results need to be in the XHTML namespace.

The simplest way of accomplishing this is to bind unprefixed names in
your XSLT to the namespace: place xmlns="http://www.w3.org/1999/xhtml"
at the *top level* of your stylesheet so that all elements in the
stylesheet with unprefixed names (such as 'htmtl', 'meta', and 'p')
inherit this declaration:

<xsl:stylesheet version="1.1"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/1999/xhtml">

BTW why are you trying to use XSLT version 1.1 (which was never
produced as a W3C Recommendation)?

I hope this helps. You will have to do more research and experiment to
understand why this works. Focus on "XSLT processing model" and
probably "XSLT built-in templates".

Cheers, Wendell

Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^


On Sun, Dec 15, 2013 at 2:24 PM, e-letter <inpost@xxxxxxxxx> wrote:
> Readers,
>
>   This post was mistakenly posted in an x-query mailing list, where it
>   was suggested to post here. For any help that can be provided, thanks
>   in advance.
>
>   The objective is to learn how to select nodes in an xml file and use
>   xpath and xslt to select nodes of interest and display in a new file.
>
>   An xml file was created:
>
>    <?xml version=3D"1.0" encoding=3D"utf-8"?>
>     <!-- Edited by XMLSpy=C2=AE, taken from http://www.w3schools.com-->
>     <?xml-stylesheet type=3D'text/xsl" href=3D"xstylesheetexample.xslt" ?>
>     <bookstore>
>             <book category=3D"COOKING">
>                     <title lang=3D"en">Everyday Italian</title>
>                    <author>Giada De Laurentiis</author>
>                     <year>2005</year>
>                     <price>30.00</price>
>             </book>
>            <book category=3D"CHILDREN">
>                     <title lang=3D"en">Harry Potter</title>
>                    <author>J K. Rowling</author>
>                     <year>2005</year>
>                     <price>29.99</price>
>             </book>
>            <book category=3D"WEB">
>                     <title lang=3D"en">XQuery Kick Start</title>
>                    <author>James McGovern</author>
>                     <author>Per Bothner</author>
>                     <author>Kurt Cagle</author>
>                     <author>James Linn</author>
>                     <author>Vaidyanathan Nagarajan</author>
>                     <year>2003</year>
>                     <price>49.99</price>
>             </book>
>            <book category=3D"WEB">
>                     <title lang=3D"en">Learning XML</title>
>                    <author>Erik T. Ray</author>
>                     <year>2003</year>
>                     <price>39.95</price>
>             </book>
>     </bookstore>
>
>     An xml stylesheet was created:
>
>    <xsl:stylesheet version=3D"1.1"
>   xmlns:xsl=3D"http://www.w3.org/1999/XSL/Transform">
>             <xsl:import href=3D"xqueryexampledata.xml"/>
>             <xsl:output
>                     method=3D"xml"
>                     doctype-public=3D"-//W3C//DTD XHTML Basic 1.1//EN"
>
>    doctype-system=3D"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"
>                     encoding=3D"utf-8"
>                     indent=3D"no"
>                     omit-xml-declaration=3D"yes"
>                     media-type=3D"text/xml"
>                     standalone=3D"yes"
>                     version=3D"1.1"
>                     />
>             <xsl:template
>                     match=3D"author">
>                     <html xmlns=3D'http://www.w3.org/1999/xhtml' lang=3D'en'
>    xml:lang=3D'en'>
>                             <head>
>                                     <meta
>                                             name=3D'note' content=3D'xhtml
>   document created by xml transformation' />
>                                     <title>Test output web page</title>
>                             </head>
>                             <body>
>                                     <p>
>                                             <xsl:apply-templates/>
>                                     </p>
>                             </body>
>                     </html>
>             </xsl:template>
>     </xsl:stylesheet>
>
>     The text editor 'jedit' is used with xml plugins xslt, xquery, xml.
>     The xslt transformer plugin is used to select the above xml file and
>     xslt file for subsequent transformation, to create an output xhtml
>     file.
>
>     The result of this transformation is the creation of a new xhtml file
>     (as stated in the stylesheet above):
>
>
>
>                     Everyday Italian
>                     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
>     "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
>    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D"en"
>     lang=3D"en"><head><meta content=3D"xhtml document created by xml
>     transformation" name=3D"note" /><title>Test output web
>    page</title></head><body><p>Giada De Laurentiis</p></body></html>
>                     2005
>                     30.00
>
>
>                     Harry Potter
>                    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D=
>  "en"
>     lang=3D"en"><head><meta content=3D"xhtml document created by xml
>     transformation" name=3D"note" /><title>Test output web
>    page</title></head><body><p>J K. Rowling</p></body></html>
>                     2005
>                     29.99
>
>
>                     XQuery Kick Start
>                    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D=
>  "en"
>     lang=3D"en"><head><meta content=3D"xhtml document created by xml
>     transformation" name=3D"note" /><title>Test output web
>    page</title></head><body><p>James McGovern</p></body></html>
>                    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D=
>  "en"
>     lang=3D"en"><head><meta content=3D"xhtml document created by xml
>     transformation" name=3D"note" /><title>Test output web
>    page</title></head><body><p>Per Bothner</p></body></html>
>                    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D=
>  "en"
>     lang=3D"en"><head><meta content=3D"xhtml document created by xml
>     transformation" name=3D"note" /><title>Test output web
>    page</title></head><body><p>Kurt Cagle</p></body></html>
>                    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D=
>  "en"
>     lang=3D"en"><head><meta content=3D"xhtml document created by xml
>     transformation" name=3D"note" /><title>Test output web
>    page</title></head><body><p>James Linn</p></body></html>
>                    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D=
>  "en"
>     lang=3D"en"><head><meta content=3D"xhtml document created by xml
>     transformation" name=3D"note" /><title>Test output web
>    page</title></head><body><p>Vaidyanathan Nagarajan</p></body></html>
>                     2003
>                     49.99
>
>
>                     Learning XML
>                    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D=
>  "en"
>     lang=3D"en"><head><meta content=3D"xhtml document created by xml
>     transformation" name=3D"note" /><title>Test output web
>    page</title></head><body><p>Erik T. Ray</p></body></html>
>                     2003
>                     39.95
>
>
>    Clearly, there's a lot to learn in specifying the transformation correct=
> ly!
>
>     The ideal output is:
>
>     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
>     "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
>            <html xmlns=3D'http://www.w3.org/1999/xhtml' lang=3D'en' xml:lan=
>  g=3D'en'>
>                     <head>
>                             <meta
>                                     content=3D"xhtml document created by
>    xml transformation" name=3D"note"
>  />
>                             <title>Test output web page</title>
>                     </head>
>                     <body>
>                             <p>Giada De Laurentiis</p>
>                             <p>J K. Rowling</p>
>                             <p>James McGovern</p>
>                             <p>Per Bothner</p>
>                             <p>Kurt Cagle</p>
>                             <p>James Linn</p>
>                             <p>Vaidyanathan Nagarajan</p>
>                             <p>Erik T. Ray</p>
>                     </body>
>             </html>


Current Thread