xsl:if that depends on if the section is inside a part/chapter or just a part

Here should go questions about transforming XML with XSLT and FOP.
josecotes
Posts: 19

xsl:if that depends on if the section is inside a part/chapter or just a part

Thu Mar 31, 2016 11:50 pm

Hi there. I have this bookmap:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<bookmap>
<part format ="dita" href="part1title.xml">
    <chapter format="ditamap" href="chapter1-1.ditamap"/>
    <chapter format="ditamap" href="chapter1-2.ditamap"/>
    <chapter format="ditamap" href="chapter1-3.ditamap"/>
</part>
<part format="ditamap" href="part2.ditamap"/>
<part format="ditamap" href="part3.ditamap"/>
<part format="ditamap" href="part4.ditamap"/>
<part format="ditamap" href="part5.ditamap"/>
<appendix format="ditamap" href="appendix1.ditamap"/>
</bookmap>


I would like to place inside templates, xsl:if commands that depend on whether elements are part/chapter or part.

I.e. I had these inside the template processTopicTitle, part of the DITA-OT distribution:

Code: Select all

<xsl:if test="bookmap/part/chapter">
    <fo:external-graphic src="thisischapter.png" />
</xsl:if>

<xsl:if test="bookmap/part">
    <fo:external-graphic src="thisispart.png" />
</xsl:if>


This is not working.

The idea is that the there is a graphic that shows up only in part/chapters, and another on the ones that are only part.

I was given this suggestion but still did not work:

Code: Select all

<xsl:choose>
    <!-- parts -->
    <xsl:when test="$map//*[contains(@class, ' bookmap/part ')]">
        <fo:external-graphic src="thisispart.png" />
    </xsl:when>
    <!-- chapters -->
    <xsl:when test="$map//*[contains(@class, ' bookmap/chapter ')]">
        <fo:external-graphic src="thisischapter.png" />
    </xsl:when>
    <!-- parts without chapters -->
    <xsl:when test="$map//*[contains(@class, ' bookmap/part ')][not(child::*[contains(@class, ' bookmap/chapter ')])">
        <fo:external-graphic src="thisispart.png" />
    </xsl:when>
</xsl:choose>


Finally, this is the section where I was putting the code in. I wanted to have a graphic inserted in the title depending if it was a part, part/chapter, as mention above:

Code: Select all

<xsl:template match="*" mode="processTopicTitle">
        <xsl:variable name="level" as="xs:integer">
          <xsl:apply-templates select="." mode="get-topic-level"/>
        </xsl:variable>
        <xsl:variable name="attrSet1">
            <xsl:apply-templates select="." mode="createTopicAttrsName">
                <xsl:with-param name="theCounter" select="$level"/>
            </xsl:apply-templates>
        </xsl:variable>
        <xsl:variable name="attrSet2" select="concat($attrSet1, '__content')"/>
        <fo:block>
            <xsl:call-template name="commonattributes"/>
            <xsl:call-template name="processAttrSetReflection">
                <xsl:with-param name="attrSet" select="$attrSet1"/>
                <xsl:with-param name="path" select="'../../cfg/fo/attrs/commons-attr.xsl'"/>
            </xsl:call-template>
            <fo:block>
                <xsl:call-template name="processAttrSetReflection">
                    <xsl:with-param name="attrSet" select="$attrSet2"/>
                    <xsl:with-param name="path" select="'../../cfg/fo/attrs/commons-attr.xsl'"/>
                </xsl:call-template>
                <xsl:if test="$level = 1">
                    <fo:marker marker-class-name="current-header">
                        <xsl:apply-templates select="." mode="getTitle"/>
                    </fo:marker>
                </xsl:if>
                <xsl:if test="$level = 2">
                    <fo:marker marker-class-name="current-h2">
                        <xsl:apply-templates select="." mode="getTitle"/>
                    </fo:marker>
                </xsl:if>
                <fo:inline id="{parent::node()/@id}"/>
               
               
               
                    <xsl:choose>
    <!-- parts -->
    <xsl:when test="$map//*[contains(@class, ' bookmap/part ')]">
        <fo:external-graphic src="thisispart.png" />
    </xsl:when>
    <!-- chapters -->
    <xsl:when test="$map//*[contains(@class, ' bookmap/chapter ')]">
        <fo:external-graphic src="thisischapter.png" />
    </xsl:when>
    <!-- parts without chapters -->
    <xsl:when test="$map//*[contains(@class, ' bookmap/part ')][not(child::*[contains(@class, ' bookmap/chapter ')])">
        <fo:external-graphic src="thisispart.png" />
    </xsl:when>
</xsl:choose>
               
               
               
               
                <fo:inline>
                    <xsl:attribute name="id">
                        <xsl:call-template name="generate-toc-id">
                            <xsl:with-param name="element" select=".."/>
                        </xsl:call-template>
                    </xsl:attribute>
                </fo:inline>
                <xsl:call-template name="pullPrologIndexTerms"/>
                <xsl:apply-templates select="." mode="getTitle"/>
            </fo:block>
        </fo:block>
    </xsl:template>
adrian
Posts: 2337

Re: xsl:if that depends on if the section is inside a part/chapter or just a part

Fri Apr 08, 2016 5:34 pm

Hi,

Your bookmap needs a DITA DOCTYPE so that the @class default attributes get filled. Otherwise, any code (xsl:if or xsl:when) in the stylesheet that depends on the @class attributes has no chance to work.

Code: Select all

<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">
<bookmap>
...
You may have just omitted it from the example you provided. But if you don't have it in your input, that's why nothing works.

Other than that I can only say that there are too many missing pieces of the puzzle to see what's happening in the actual code. This looks like part of a DITA stylesheet customization, that's as much as I can tell.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 1 guest