Walter Kuhn
Walter Kuhn

Reputation: 493

Including several docbook articles into a book: error with identical IDs

Having a master DocBook "book.xml", I want to include several DocBook articles, say x.xml and y.xml. (I do this by using

  <xi:include href="generated_schemata/x/x.xml"/>

Those articles x and y are generated from OxygenXML and document XSD schema (x.xsd and y.xsd). Inside the different schema, I have the same element name (say "A").

docbkx complains (and I understand why) that, while it tries to include the article x and y into book.xml for rendering a PDF, that there are multiple instances of IDs, namely for A:

Failed to execute goal com.agilejava.docbkx:docbkx-maven-plugin:2.0.17:generate-pdf (default) on project docbook: 
Failed to transform to fo: org.apache.fop.fo.ValidationException: Property ID "A" (found on "fo:block") previously used; ID values must be unique within a document!

Any idea on automated separation of those instances? Maybe by automatically adding a prefix?

(I understand, I could manually change x.xml and y.xml so that there are no conflicts; however, due to frequent changes, this is a source of errors. Or, I could change a schema specification, which is not an option).

Upvotes: 0

Views: 192

Answers (3)

Walter Kuhn
Walter Kuhn

Reputation: 493

Both solutions - from Eduard Tibet and from Radu Coravu - in combination work perfectly. So, in short words, I describe my solution - still not perfect but working: my master file "books.xml" uses several include, such as:

<xi:include href="generated_schemata/services/services.xml" ns3:root="one"/>
<xi:include href="generated_schemata/systems/systems.xml" ns3:root="two"/>

Both include files have been generated by OxygenXML and are the documentations for schemata.

my maven configuration includes:

<plugin>
            <groupId>com.agilejava.docbkx</groupId>
            <artifactId>docbkx-maven-plugin</artifactId>
            <version>2.0.17</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate-html</goal>
                        <goal>generate-pdf</goal>
                    </goals>
                    <phase>package</phase>
                </execution>
            </executions>
            <configuration>
                <paperType>A4</paperType>
                <xincludeSupported>true</xincludeSupported>
                <useExtensions>1</useExtensions>
                <fop1Extensions>1</fop1Extensions>
                <generatedSourceDirectory>${project.build.directory}/xinclude</generatedSourceDirectory>
                <includes>book.xml</includes>
                <foCustomization>src/docbkx/xsl/fo.xsl</foCustomization> 
                <xhtmlCustomization>src/docbkx/xsl/xhtml.xsl</xhtmlCustomization>
                <htmlCustomization>src/docbkx/xsl/xhtml.xsl </htmlCustomization>
                <chapterAutolabel>1</chapterAutolabel> 
                <sectionAutolabel>1</sectionAutolabel> 
                <sectionAutolabelMaxDepth>5</sectionAutolabelMaxDepth>                 <sectionLabelIncludesComponentLabel>true</sectionLabelIncludesComponentLabel>
                <postProcess>
                    <copy todir="${env.GENQ_HOME}/doc/extra/html/generated_schemata/services/img">
                        <fileset dir="src/docbkx/generated_schemata/services/img/">
                            <include name="**/*.css"/>
                            <include name="**/*.js"/>
                            <include name="**/*.png"/>
                            <include name="**/*.gif"/>
                            <include name="**/*.jpg"/>
                            <include name="**/*.jpeg"/>
                        </fileset>
                    </copy>
                    <copy todir="${env.GENQ_HOME}/doc/extra/html/generated_schemata/systems/img">
                        <fileset dir="src/docbkx/generated_schemata/systems/img/">
                            <include name="**/*.css"/>
                            <include name="**/*.js"/>
                            <include name="**/*.png"/>
                            <include name="**/*.gif"/>
                            <include name="**/*.jpg"/>
                            <include name="**/*.jpeg"/>
                        </fileset>
                    </copy>                     
                </postProcess>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>net.sf.docbook</groupId>
                    <artifactId>docbook-xml</artifactId>
                    <version>5.1b4-all</version>
                    <classifier>resources</classifier>
                    <type>zip</type>
                    <scope>runtime</scope>
                </dependency>
                <dependency>
                    <groupId>net.sf.offo</groupId>
                    <artifactId>fop-hyph</artifactId>
                    <version>2.0</version>
                </dependency>
            </dependencies>
        </plugin>

and finally, the additional XSL consists of:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:fo="http://docbook.org/ns/docbook">
<!-- use the default xslt from docbkx -->
<xsl:import href="urn:docbkx:stylesheet"/>
<!-- change the way, the TOC is done, cf. http://www.sagehill.net/docbookxsl/TOCcontrol.html -->
<xsl:param name="generate.toc" select="'book toc'"/>
<xsl:param name="fop1.extensions" select="1"/>

<xsl:attribute-set name="monospace.verbatim.properties">
    <xsl:attribute name="wrap-option">wrap</xsl:attribute>
    <xsl:attribute name="hyphenation-character">\</xsl:attribute>
    <xsl:attribute name="font-family">Courier</xsl:attribute>
    <xsl:attribute name="font-size">9pt</xsl:attribute>
    <xsl:attribute name="keep-together.within-column">always</xsl:attribute>
</xsl:attribute-set>

<xsl:template name="object.id">
    <xsl:param name="object" select="."/>
    <xsl:variable name="id" select="@id"/>
    <xsl:variable name="xid" select="@xml:id"/>
    <xsl:variable name="preceding.id"
                  select="count(preceding::*[@id = $id])"/>
    <xsl:variable name="preceding.xid"
                  select="count(preceding::*[@xml:id = $xid])"/>
    <xsl:choose>
        <xsl:when test="$object/@id and $preceding.id != 0">
            <xsl:value-of select="concat($object/@id, $preceding.id)"/>
        </xsl:when>
        <xsl:when test="$object/@id">
            <xsl:value-of select="$object/@id"/>
        </xsl:when>
        <xsl:when test="$object/@xml:id and $preceding.xid != 0">
            <xsl:value-of select="concat($object/@xml:id, $preceding.xid)"/>
        </xsl:when>
        <xsl:when test="$object/@xml:id">
            <xsl:value-of select="$object/@xml:id"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="generate-id($object)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="table">
    <fo:table-and-caption>
        <fo:table-caption>
            <xsl:apply-templates select="caption"/>
        </fo:table-caption>
        <xsl:apply-templates select="tbody"/>
    </fo:table-and-caption>
</xsl:template>

<xsl:template match="caption">
    <fo:block>
        <xsl:apply-templates/>
    </fo:block>
</xsl:template>

<xsl:template match="tbody">
    <fo:table space-before="0.5em" space-after="0.5em" table-layout="fixed">
        <fo:table-header>
            <xsl:apply-templates select="tr[1]"/>
        </fo:table-header>
        <fo:table-body>
            <xsl:apply-templates select="tr[position() &gt; 1]"/>
        </fo:table-body>
    </fo:table>
</xsl:template>

</xsl:stylesheet>

Hope, someone may profit from this.

Any suggestions on improvements?

Upvotes: 0

Radu Coravu
Radu Coravu

Reputation: 1924

Not sure if it helps your particular situation but Oxygen 19.1 has support for XInclude 1.1 and in xi:include 1.1 there is a feature which would allow you to change the ID attribute on the included element:

https://www.w3.org/TR/xinclude-11/#attribute-copying

But this would work only when publishing from Oxygen, not when using an external build system.

Upvotes: 1

Eduard Tibet
Eduard Tibet

Reputation: 396

Here is one recipe from Bob Stayton how modify xsl to avoid duplicate IDs in output: http://www.sagehill.net/docbookxsl/DuplicateIDs.html#ModifyIdValues

Upvotes: 1

Related Questions