Newbie: Almost empty output when translating from DocBook to XML,why?

Discussion in 'XML' started by J Bondo, Jan 17, 2009.

  1. J Bondo

    J Bondo Guest

    Dear group,

    I'm new to both DocBook and XSLT and am having troubles understanding
    why my XSLT stylesheet doesn't work. I'm using xsltproc on my Mac to
    generate a plist file. For some reason, none of my templates, except
    for <xsl:template match="/">, are being executed. I've tried all kinds
    of combinations for the search pattern. What am I doing wrong? See
    file listings below.

    Any help is appreciated, thanks in advance,
    Joachim

    ---

    Executing

    $ xsltproc -o Test.plist test.xsl test.docbook

    gives this output:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://
    www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict/>
    </plist>

    ---

    test.docbook:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V5.0//EN" "http://
    www.oasis-open.org/docbook/xml/5.0b5/dtd/docbook.dtd">

    <book xmlns="http://docbook.org/ns/docbook" version="5.0"
    xml:lang="en" xml:id="my-doc-id">

    <info>
    <title>My Book Title</title>

    <revhistory>
    <revision>
    <revnumber>1st revision</revnumber>
    <date>January 17, 2009</date>
    </revision>
    </revhistory>
    </info>

    <chapter>
    <title>First Chapter</title>
    <section>
    <title>First section in chapter 1</title>
    <para>First paragraph of section 1 in chapter 1</para>
    </section>
    </chapter>

    <!-- General -->
    <chapter>
    <title>Second Chapter</title>
    <section>
    <title>Section 1 of chapter 2</title>
    <para>First paragraph in section 1, chapter 2</para>
    <para>Second paragraph in section 1, chapter 2</para>
    </section>
    </chapter>

    </book>

    test.xsl:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" >

    <xsl:eek:utput encoding="UTF-8" indent="yes" method="xml"
    doctype-public="-//Apple//DTD PLIST 1.0//EN"
    doctype-system="http://www.apple.com/DTDs/PropertyList-1.0.dtd" />

    <xsl:template match="/">
    <plist version="1.0">
    <dict>
    <xsl:apply-templates />
    </dict>
    </plist>
    </xsl:template>

    <!-- TEMPLATES -->

    <xsl:template match="book/info">
    <key>Title</key>
    <string><xsl:value-of select="title" /></string>
    <key>Date</key>
    <string><xsl:value-of select="revhistory/revision[1]/date" /></
    string>
    <key>Chapters</key>
    <array>
    <xsl:apply-templates />
    </array>
    </xsl:template>

    <xsl:template match="chapter">
    <dict>
    <key>Title</key>
    <string><xsl:value-of select="title" /></string>
    <key>Sections</key>
    <array>
    <xsl:apply-templates />
    </array>
    </dict>
    </xsl:template>

    <xsl:template match="section">
    <dict>
    <key>Title</key>
    <string><xsl:value-of select="title" /></string>
    </dict>
    </xsl:template>

    <xsl:template match="*">
    </xsl:template>

    </xsl:stylesheet>
    J Bondo, Jan 17, 2009
    #1
    1. Advertising

  2. Re: Newbie: Almost empty output when translating from DocBook toXML, why?

    J Bondo wrote:
    > Dear group,
    >
    > I'm new to both DocBook and XSLT and am having troubles understanding
    > why my XSLT stylesheet doesn't work. I'm using xsltproc on my Mac to
    > generate a plist file. For some reason, none of my templates, except
    > for <xsl:template match="/">, are being executed. I've tried all kinds
    > of combinations for the search pattern. What am I doing wrong? See
    > file listings below.
    >
    > Any help is appreciated, thanks in advance,
    > Joachim
    >
    > ---
    >
    > Executing
    >
    > $ xsltproc -o Test.plist test.xsl test.docbook
    >
    > gives this output:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://
    > www.apple.com/DTDs/PropertyList-1.0.dtd">
    > <plist version="1.0">
    > <dict/>
    > </plist>
    >


    When following your example on my laptop, I get the same result, but also a warning:

    [Peifer@LAPTOP7664:~]> xsltproc -o Test.plist test.xsl test.docbook
    test.docbook:3: warning: failed to load external entity "-//OASIS//DTD DocBook V5.0//EN"
    www.oasis-open.org/docbook/xml/5.0b5/dtd/docbook.dtd">

    Does this ring a bell?

    Another rule, which has often helped me, is:
    If no (reasonable) code seems to work at all, then you probably have a namespace issue.

    Hope this helps, Hermann
    Hermann Peifer, Jan 18, 2009
    #2
    1. Advertising

  3. Re: Newbie: Almost empty output when translating from DocBook toXML, why?

    J Bondo wrote:

    > <book xmlns="http://docbook.org/ns/docbook" version="5.0"
    > xml:lang="en" xml:id="my-doc-id">
    >
    > <info>
    > <title>My Book Title</title>


    Because of the xmlns="http://docbook.org/ns/docbook" the elements are in
    the namespace http://docbook.org/ns/docbook yet your XSLT 1.0 stylesheet


    > <xsl:template match="book/info">


    tries to match elements named 'info' in _no_ namespace.

    So your stylesheet needs to bind a prefix to the namespace URI
    http://docbook.org/ns/docbook and use that prefix in match patterns and
    XPath expressions to qualify element names:

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0"
    xmlns:db="http://docbook.org/ns/docbook">

    <xsl:template match="db:book/db:info">

    <xsl:value-of select="db:title"/>

    and so on for all other match patterns and XPath expressions supposed to
    match respectively select elements in that namespace.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 18, 2009
    #3
  4. J Bondo

    J Bondo Guest

    Thanks Hermann, thanks Martin,

    I wasn't aware of the namespace issue. Thanks a lot for clearing that
    up.
    However, I've tried to implement your suggestions, but with the same
    result as before. See below (have simplified files further).

    Thanks for taking the time,
    Joachim

    $ xsltproc --verbose -o Test.plist test.xsl test.docbook
    creating dictionary for stylesheet
    reusing dictionary from test.xsl for stylesheet
    Added namespace: xsl mapped to http://www.w3.org/1999/XSL/Transform
    Added namespace: db mapped to http://docbook.org/ns/docbook
    exclude result prefix db
    exclude result prefix db
    xsltPrecomputeStylesheet: removing ignorable blank node
    xsltParseStylesheetProcess : found stylesheet
    xsltCompilePattern : parsing '/'
    xsltCompilePattern : parsed /, default priority 0.500000
    added pattern : '/' priority 0.500000
    xsltCompilePattern : parsing 'db:info'
    xsltCompilePattern : parsed db:info, default priority 0.000000
    added pattern : 'db:info' priority 0.000000
    xsltCompilePattern : parsing 'db:chapter'
    xsltCompilePattern : parsed db:chapter, default priority 0.000000
    added pattern : 'db:chapter' priority 0.000000
    xsltCompilePattern : parsing '*'
    xsltCompilePattern : parsed *, default priority -0.500000
    added pattern : '*' priority -0.500000
    parsed 4 templates
    Resolving attribute sets references
    Registered 0 modules
    Creating sub-dictionary from stylesheet for transformation
    reusing transformation dict for output
    Registering global variables
    Registering global variables from test.xsl
    xsltProcessOneNode: applying template '/' for /
    xsltApplyOneTemplate: copy node plist
    xsltApplyOneTemplate: copy node dict
    xsltApplyTemplates: list of 1 nodes
    xsltProcessOneNode: applying template '*' for book
    freeing transformation dictionnary
    freeing dictionary from stylesheet

    ---

    test.docbook:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V5.0//EN" "http://
    www.oasis-open.org/docbook/xml/5.0b5/dtd/docbook.dtd">
    <book xmlns="http://docbook.org/ns/docbook" version="5.0"
    xml:lang="en" xml:id="my-doc-id">
    <info>
    <title>My Book Title</title>
    </info>
    <chapter>
    <title>First Chapter</title>
    <para></para>
    </chapter>
    <chapter>
    <title>Second Chapter</title>
    <para></para>
    </chapter>
    </book>

    test.xsl:

    <?xml version="1.0" encoding="UTF-8"?>

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:db="http://docbook.org/ns/docbook"
    exclude-result-prefixes="db"
    version="1.0" >

    <xsl:eek:utput encoding="UTF-8" indent="yes" method="xml"
    doctype-public="-//Apple//DTD PLIST 1.0//EN"
    doctype-system="http://www.apple.com/DTDs/PropertyList-1.0.dtd"/>

    <xsl:template match="/">
    <plist version="1.0">
    <dict>
    <xsl:apply-templates/>
    </dict>
    </plist>
    </xsl:template>

    <xsl:template match="db:info">
    <key>Title</key>
    <string><xsl:value-of select="db:title"/></string>
    <key>Chapters</key>
    <array>
    <xsl:apply-templates/>
    </array>
    </xsl:template>

    <xsl:template match="db:chapter">
    <dict>
    <key>Title</key>
    <string><xsl:value-of select="db:title"/></string>
    </dict>
    </xsl:template>

    <xsl:template match="*">
    </xsl:template>

    </xsl:stylesheet>

    On Jan 18, 1:16 pm, Martin Honnen <> wrote:
    > J Bondo wrote:
    > > <book xmlns="http://docbook.org/ns/docbook" version="5.0"
    > > xml:lang="en" xml:id="my-doc-id">

    >
    > > <info>
    > >    <title>My Book Title</title>

    >
    > Because of the xmlns="http://docbook.org/ns/docbook" the elements are in
    > the namespacehttp://docbook.org/ns/docbookyet your XSLT 1.0 stylesheet
    >
    > >    <xsl:template match="book/info">

    >
    > tries to match elements named 'info' in _no_ namespace.
    >
    > So your stylesheet needs to bind a prefix to the namespace URIhttp://docbook.org/ns/docbookand use that prefix in match patterns and
    > XPath expressions to qualify element names:
    >
    >    <xsl:stylesheet
    >      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >      version="1.0"
    >      xmlns:db="http://docbook.org/ns/docbook">
    >
    >     <xsl:template match="db:book/db:info">
    >
    >        <xsl:value-of select="db:title"/>
    >
    > and so on for all other match patterns and XPath expressions supposed to
    > match respectively select elements in that namespace.
    >
    > --
    >
    >         Martin Honnen
    >        http://JavaScript.FAQTs.com/
    J Bondo, Jan 18, 2009
    #4
  5. Re: Newbie: Almost empty output when translating from DocBook toXML, why?

    <xsl:template match="*">
    </xsl:template>


    That says by default when applying templates to an element you should
    make no output and don't process the descendents of the element.

    You almost certainly do not want that. Certainly if you dnt hav ethat
    template you will have a better chance of getting some output.

    in particular your document element os book but you don't have a
    template for db:book, so this template will fire producing no output and
    stopping all further processing.


    David
    David Carlisle, Jan 18, 2009
    #5
  6. J Bondo

    J Bondo Guest

    Great, that was the last piece of the puzzle! I thought <xsl:template
    match="*"> was a fall-back, and not a catch-all.

    I hope this thread can help other newbies too :)

    Thanks,
    Joachim

    On Jan 19, 12:48 am, David Carlisle <>
    wrote:
    >         <xsl:template match="*">
    >         </xsl:template>
    >
    > That says by default when applying templates to an element you should
    > make no output and don't process the descendents of the element.
    >
    > You almost certainly do not want that. Certainly if you dnt hav ethat
    > template you will have a better chance of getting some output.
    >
    > in particular your document element os book but you don't have a
    > template for db:book, so this template will fire producing no output and
    > stopping all further processing.
    >
    > David
    J Bondo, Jan 19, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio Amato
    Replies:
    2
    Views:
    1,252
    Michael Wiedmann
    Nov 18, 2004
  2. Lukas
    Replies:
    3
    Views:
    772
    spiff
    Nov 10, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,656
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    1
    Views:
    504
    Joseph Kesselman
    Nov 27, 2007
  5. Replies:
    1
    Views:
    618
    Peter Flynn
    Jan 20, 2008
Loading...

Share This Page