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

J

J Bondo

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>
 
H

Hermann Peifer

J said:
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
 
M

Martin Honnen

J said:
<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.
 
J

J Bondo

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>
 
D

David Carlisle

<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

J Bondo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top