Home > Software engineering >  xsl:value of not working in docbook xml files
xsl:value of not working in docbook xml files

Time:07-13

I have the following docbook xml file in version 5.

<?xml version="1.0" encoding="UTF-8"?>
<book version="5.0" xmlns="http://docbook.org/ns/docbook" xmlns:xi="http://www.w3.org/2001/XInclude">
    <info>
        <title>Title1</title>
        <cover>
            <para>
                <para>a paragraph</para>
            </para>
        </cover>
    </info>
    <!-- Part A -->
    <part>
        <title>Part A</title>
        <chapter label="1">
            <title role="HEAD-1">Chapter1 from PartA</title>
            <para role="richtext">
                <para role="richtext"> Text in paragraph </para>
            </para>
        </chapter>
        <chapter label="2">
            <title role="HEAD-1">Chapter2 from partA</title>
            <para>Another paragraph</para>
        </chapter>
    </part>
    <!-- Part B -->
    <part>
        <title>Part B</title>
        <chapter label="1">
            <title role="HEAD-1">Chapter1 from PartB</title>
            <section>
                <title role="HEAD-2">Section1 from Chapter1 of PartB</title>
                <para>a paragraph <ulink url="http://alink2">a link2</ulink></para>
            </section>
            <section>
                <title role="HEAD-2">Section2 from Chapter1 of PartB</title>
                <para>a paragraph <ulink url="http://alink2">a link2</ulink></para>
            </section>
        </chapter>
    </part>
</book>

I'm trying to convert it to html through xslt v3 and by modifying specific elements ( I have used the saxon xslt library for java), i.e.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:db="http://docbook.org/ns/docbook"
        xmlns:m="http://docbook.org/ns/docbook/modes"
        xmlns:h="http://www.w3.org/1999/xhtml"
        xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xsl xs db m h"
        version="3.0">

    <xsl:import href="https://cdn.docbook.org/release/xsltng/current/xslt/docbook.xsl" />


    <xsl:template match="db:part" mode="m:docbook">
        

        <xsl:variable name="title1" select="title" />
        <xsl:variable name="title2" select="./title"/>
        <xsl:variable name="title3" select="./db:title"/>
        <xsl:variable name="title4" select="/db:title"/>
        <xsl:variable name="title5" select="db:title"/>
        <xsl:variable name="title6" select="xs:title"/>
        <xsl:variable name="title7" select="xsl:title"/>
        <xsl:variable name="title8" select="/title"/>
        <xsl:variable name="title9" select="./h:title"/>
        <xsl:variable name="title10" select="h:title"/>




        <section >
            <h1 >
                title1:<xsl:value-of select = "$title1"/>
                title2:<xsl:value-of select = "$title2"/>
                title3:<xsl:value-of select = "$title3"/>
                title4:<xsl:value-of select = "$title4"/>
                title5:<xsl:value-of select = "$title5"/>
                title6:<xsl:value-of select = "$title6"/>
                title7:<xsl:value-of select = "$title7"/>
                title8:<xsl:value-of select = "$title8"/>
                title9:<xsl:value-of select = "$title9"/>
                title10:<xsl:value-of select = "$title10"/>
            </h1>
            <xsl:next-match/>
        </section>
    </xsl:template>
</xsl:stylesheet>

I'm trying to get the title but for some reason it doesn't have any value. I have tried different ways, thinking of a wrong namespace but it doesn't seem to be something that.

Any ideas?

CodePudding user response:

I don't know what the stylesheets of DocBook are supposed to do with your source input XML before they hit your template but if you do an <xsl:message select="."/> in your template you will see that the structure has changed, you get e.g.

<part xmlns="http://docbook.org/ns/docbook"
       xmlns:xi="http://www.w3.org/2001/XInclude">
   <info>
      <title>Part A</title>
   </info>
   <chapter label="1">
      <info>
         <title role="HEAD-1">Chapter1 from PartA</title>
      </info>
      <para role="richtext">
         <para role="richtext"> Text in paragraph </para>
      </para>
   </chapter>
   <chapter label="2">
      <info>
         <title role="HEAD-1">Chapter2 from partA</title>
      </info>
      <para>Another paragraph</para>
   </chapter>
</part>

where the title has been wrapped into an info element. So at that point any child selection db:title will not select anything, you need e.g. db:info/db:title, I think.

  • Related