简体   繁体   中英

How to call a variable value from one template to another template using xslt

Hi I need to call a variable value from one template to another template in an xsl file of dita ot plugin:

My Ditamap file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pubmap
  PUBLIC "urn:pubid:com.sam.doctypes:dita:pubmap" "pubmap.dtd">
<pubmap xml:lang="en-US">
  <pubtitle>
    <mainpubtitle outputclass="book">Sample Word</mainpubtitle>
  </pubtitle>
  <topicref href="topics/topic_1.dita">
    <topicmeta>
      <navtitle>Ram-Files-Raj (RFR)</navtitle>
      <metadata/>
    </topicmeta>
  </topicref>
  <topicref href="topics/topic_2.dita">
    <topicmeta>
      <navtitle>Files-Sampletitle (FST)</navtitle>
      <metadata/>
    </topicmeta>
  </topicref>
</pubmap>

My topic_1.dita file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "urn:pubid:com.sam.doctypes:dita:topic" "topic.dtd">
<topic id="topic_1" xml:lang="en-US" outputclass="Ram-Files-RajRFR"><title>Ram-Files-Raj (RFR)</title></topic>

My topic_2.dita file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "urn:pubid:com.sam.doctypes:dita:topic" "topic.dtd">
<topic id="topic_2" xml:lang="en-US" outputclass="Files-SampletitleFST"><title>Files-Sampletitle (FST)</title></topic>

My XSLT template having the needed variable "output-class":

<xsl:template match="*" mode="set-output-class">
  <xsl:param name="default"/>
  <xsl:variable name="output-class">
    <xsl:apply-templates select="." mode="get-output-class"/>
  </xsl:variable>
  <xsl:variable name="draft-revs">
    <!-- If draft is on, add revisions to default class. Simplifies processing in DITA-OT 1.6 and earlier
         that created an extra div or span around revised content, just to hold @class with revs. -->
    <xsl:if test="$DRAFT = 'yes'">
      <xsl:for-each select="*[contains(@class, ' ditaot-d/ditaval-startprop ')]/revprop">
        <xsl:value-of select="@val"/>
        <xsl:text> </xsl:text>
      </xsl:for-each>
    </xsl:if>
  </xsl:variable>
  <xsl:variable name="using-output-class">
    <xsl:choose>
      <xsl:when test="string-length(normalize-space($output-class)) > 0"><xsl:value-of select="$output-class"/></xsl:when>
      <xsl:when test="string-length(normalize-space($default)) > 0"><xsl:value-of select="$default"/></xsl:when>
    </xsl:choose>
    <xsl:if test="$draft-revs != ''">
      <xsl:text> </xsl:text>
      <xsl:value-of select="normalize-space($draft-revs)"/>
    </xsl:if>
  </xsl:variable>
  <xsl:variable name="ancestry">
    <xsl:if test="$PRESERVE-DITA-CLASS = 'yes'">
      <xsl:apply-templates select="." mode="get-element-ancestry"/>
    </xsl:if>
  </xsl:variable>
  <xsl:variable name="outputclass-attribute">
    <xsl:apply-templates select="@outputclass" mode="get-value-for-class"/>
  </xsl:variable>
  <!-- Revised design with DITA-OT 1.5: include class ancestry if requested; 
       combine user output class with element default, giving priority to the user value. -->
  <xsl:if test="string-length(normalize-space(concat($outputclass-attribute, $using-output-class, $ancestry))) > 0">
    <xsl:attribute name="class">
      <xsl:value-of select="$ancestry"/>
      <xsl:if test="string-length(normalize-space($ancestry)) > 0 and 
                    string-length(normalize-space($using-output-class)) > 0"><xsl:text> </xsl:text></xsl:if>
      <xsl:value-of select="normalize-space($using-output-class)"/>
      <xsl:if test="string-length(normalize-space(concat($ancestry, $using-output-class))) > 0 and
                    string-length(normalize-space($outputclass-attribute)) > 0"><xsl:text> </xsl:text></xsl:if>
      <xsl:value-of select="$outputclass-attribute"/>
    </xsl:attribute>
  </xsl:if>
</xsl:template>

from this above template i need to fetch the "output-class" variable to below template:

<xsl:template match="/*[df:class(., 'map/map')]">
    <xsl:param name="doDebug" as="xs:boolean" tunnel="yes" select="false()"/>

    <xsl:variable name="effectiveCoverGraphicUri" as="xs:string">
      <xsl:apply-templates select="." mode="get-cover-graphic-uri"/>
    </xsl:variable>

    <!-- FIXME: Add mode to get effective front cover topic URI so we
         can generate <guide> entry for the cover page. Also provides
         extension point for synthesizing the cover if it's not
         explicit in the map.
    -->

    <xsl:apply-templates select="." mode="report-parameters">
      <xsl:with-param name="effectiveCoverGraphicUri" select="$effectiveCoverGraphicUri" as="xs:string" tunnel="yes"/>
    </xsl:apply-templates>

    <xsl:variable name="graphicMap" as="element()">
      <xsl:apply-templates select="." mode="generate-graphic-map">
        <xsl:with-param name="effectiveCoverGraphicUri" select="$effectiveCoverGraphicUri" as="xs:string" tunnel="yes"/>
        <xsl:with-param name="uplevels" select="$uplevels" as="xs:string" tunnel="yes" />
      </xsl:apply-templates>
    </xsl:variable>

    <xsl:message> + [INFO] Collecting data for index generation, enumeration, etc....</xsl:message>

    <xsl:variable name="collected-data" as="element()">
      <xsl:call-template name="mapdriven:collect-data"/>
    </xsl:variable>

    <xsl:if test="$doDebug">
      <xsl:message> + [DEBUG] Writing file <xsl:sequence select="relpath:newFile($outdir, 'collected-data.xml')"/>...</xsl:message>
      <xsl:result-document href="{relpath:newFile($outdir, 'collected-data.xml')}"
        format="indented-xml"
        >
        <xsl:sequence select="$collected-data"/>
      </xsl:result-document>
    </xsl:if>

    <xsl:result-document href="{relpath:newFile($outdir, 'graphicMap.xml')}" format="graphic-map"
      >
      <xsl:sequence select="$graphicMap"/>
    </xsl:result-document>
    <xsl:call-template name="make-meta-inf"/>
    <xsl:call-template name="make-mimetype"/>

    <xsl:message> + [INFO] Gathering index terms...</xsl:message>

    <xsl:apply-templates select="." mode="generate-content">
      <xsl:with-param name="collected-data" as="element()" select="$collected-data" tunnel="yes"/>
    </xsl:apply-templates>
    <xsl:if test="$epubtrans:isEpub3">
      <xsl:if test="$doDebug">
        <xsl:message> + [DEBUG] generating EPUB3 nav</xsl:message>
      </xsl:if>
      <xsl:apply-templates select="." mode="epubtrans:generate-nav">
        <xsl:with-param name="collected-data" as="element()" select="$collected-data" tunnel="yes"/>
      </xsl:apply-templates>
      <xsl:if test="$doDebug">
        <xsl:message> + [DEBUG] after generate-nav</xsl:message>
      </xsl:if>
    </xsl:if>
    <!-- NOTE: The generate-toc mode is for the EPUB2 toc.ncx, not the HTML toc -->
    <xsl:if test="$epubtrans:isEpub2 or $epubtrans:isDualEpub">
      <xsl:if test="$doDebug">
        <xsl:message> + [DEBUG] generating EPUB2 toc.ncx...</xsl:message>
      </xsl:if>
      <xsl:apply-templates select="." mode="generate-toc">
        <xsl:with-param name="collected-data" as="element()" select="$collected-data" tunnel="yes"/>
      </xsl:apply-templates>
      <xsl:if test="$doDebug">
        <xsl:message> + [DEBUG] after generate-toc</xsl:message>
      </xsl:if>
    </xsl:if>
    <xsl:apply-templates select="." mode="generate-index">
      <xsl:with-param name="collected-data" as="element()" select="$collected-data" tunnel="yes"/>
    </xsl:apply-templates>
    <xsl:if test="$doDebug">
      <xsl:message> + [DEBUG] after generate-index</xsl:message>
    </xsl:if>
    <xsl:apply-templates select="." mode="generate-book-lists">
      <xsl:with-param name="collected-data" as="element()" select="$collected-data" tunnel="yes"/>
    </xsl:apply-templates>
    <xsl:if test="$doDebug">
      <xsl:message> + [DEBUG] after generate-book-lists</xsl:message>
    </xsl:if>
    <xsl:apply-templates select="." mode="generate-opf">
      <xsl:with-param name="graphicMap" as="element()" tunnel="yes" select="$graphicMap"/>
      <xsl:with-param name="collected-data" as="element()" select="$collected-data" tunnel="yes"/>
      <xsl:with-param name="effectiveCoverGraphicUri" select="$effectiveCoverGraphicUri" as="xs:string" tunnel="yes"/>
    </xsl:apply-templates>
    <xsl:if test="$doDebug">
      <xsl:message> + [DEBUG] after generate-opf</xsl:message>
    </xsl:if>
    <xsl:if test="$doDebug">
      <xsl:message> + [DEBUG] Generating graphic copy Ant script...</xsl:message>
    </xsl:if>
    <xsl:apply-templates select="." mode="generate-graphic-copy-ant-script">
      <xsl:with-param name="graphicMap" as="element()" tunnel="yes" select="$graphicMap"/>
    </xsl:apply-templates>
    <xsl:if test="$doDebug">
      <xsl:message> + [DEBUG] after generate-graphic-copy-ant-script</xsl:message>
    </xsl:if>
<xsl:variable name="sample" 
      select="relpath:newFile($outdir, 'sample.xhtml')" 
      as="xs:string"/>
<xsl:result-document href="{$sample}" format="html5">
      <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
          <title>sample</title>
          <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        </head>
        <body>
          <section id="minitoc">
            <ul>
              <li>
                <xsl:choose>
    <xsl:when test="exists($outputclass = 'Ram-Files-RajRFR')">
      <h1><xsl:value-of select="//topic[@outputclass='Ram-Files-RajRFR']"/></h1><hr/>
    </xsl:when>
    <xsl:when test="exists($outputclass = 'Files-SampletitleFST')">
      <h2><xsl:value-of select="//topic[@outputclass='Ram-Files-RajRFR']"/></h2><hr/>
    </xsl:when>
    <xsl:otherwise>

    </xsl:otherwise>
  </xsl:choose></li>
            </ul>
          </section>
        </body>      </html>
    </xsl:result-document>
  </xsl:template>

Please suggest output-class variable fetching in '/*[df:class(., 'map/map')]' template.

Thanks in advance.

<xsl:template match="*" mode="set-output-class"> template returns attribute()? . So if you want to get this result as is, following code in your <xsl:template match="/*[df:class(., 'map/map')]"> template will work:

<xsl:variable name="classAttr" as="attribute()?">
    <xsl:apply-templates select="." mode="set-output-class"/>
</xsl:variable>

Or if you want get the string value:

<xsl:variable name="classValue" as="xs:string" select="string($classAttr)"/>

will be sufficient.

i need topic file output classes attribute value for review

I'm not sure for EPUB transform. But following code may be one of the solution.

    <xsl:variable name="topicOutputClasses" as="attribute()*">
        <xsl:for-each select="*[contains(@class,' map/topicref ')][exists(@href)]">
            <xsl:variable name="topicRef" as="element()" select="."/>
            <xsl:variable name="href" as="xs:string" select="$topicRef/@href/string(.)"/>
            <xsl:variable name="topicDoc" as="document-node()" select="document($href,$topicRef)"/>
            <xsl:sequence select="$topicDoc/*[contains(@class,' topic/topic ')]/@outputclass"/>
        </xsl:for-each>
    </xsl:variable>

how to compare the particular outputclass from the all the output classes

Changing previous variable as xs:string* will make it possible to use in your xsl:when/@test as expected.

<xsl:variable name="outputclasse" as="xs:string*">
    <xsl:for-each select="descendant::*[contains(@class,' map/topicref ')][exists(@href)]">
        <xsl:variable name="topicRef" as="element()" select="."/>
        <xsl:variable name="href" as="xs:string" select="$topicRef/@href/string(.)"/>
        <xsl:variable name="topicDoc" as="document-node()" select="document($href,$topicRef)"/>
        <xsl:sequence select="$topicDoc/*[contains(@class,' topic/topic ')]/@outputclass/string(.)"/>
    </xsl:for-each>
</xsl:variable>

You can use this variable with general comparison operator "=" such like this:

<xsl:when test="$outputclass = 'Ram-Files-RajRFR'">

its not fetching the value inside h1 using this <h1><xsl:value-of select="//topic[@outputclass='Ram-Files-RajRFR']"/></h1>

If you succeeded to get the @outputclass value from topic files, following code will be also work.

[Variables in your map template]

<xsl:variable name="outputclasses" as="xs:string*">
    <xsl:for-each select="descendant::*[contains(@class,' map/topicref ')][exists(@href)]">
        <xsl:variable name="topicRef" as="element()" select="."/>
        <xsl:variable name="href" as="xs:string" select="$topicRef/@href/string(.)"/>
        <xsl:variable name="topicDoc" as="document-node()" select="document($href,$topicRef)"/>
        <xsl:if test="exists($topicDoc/*[contains(@class,' topic/topic ')]/@outputclass)">
            <xsl:sequence select="$topicDoc/*[contains(@class,' topic/topic ')]/@outputclass/string(.)"/>
        </xsl:if>
    </xsl:for-each>
</xsl:variable>

<xsl:variable name="topicHrefs" as="xs:string*">
    <xsl:for-each select="descendant::*[contains(@class,' map/topicref ')][exists(@href)]">
        <xsl:variable name="topicRef" as="element()" select="."/>
        <xsl:variable name="href" as="xs:string" select="$topicRef/@href/string(.)"/>
        <xsl:variable name="topicDoc" as="document-node()" select="document($href,$topicRef)"/>
        <xsl:if test="exists($topicDoc/*[contains(@class,' topic/topic ')]/@outputclass)">
            <xsl:sequence select="$href"/>
        </xsl:if>
    </xsl:for-each>
</xsl:variable>

[Get the topic content]

<ul>
    <li>
        <xsl:choose>
            <xsl:when test="$outputclasses = 'Ram-Files-RajRFR'">
                <xsl:variable name="index" as="xs:integer" select="index-of($outputclasses,'Ram-Files-RajRFR')"/>
                <xsl:variable name="href" as="xs:string" select="$topicHrefs[$index]"/>
                <xsl:variable name="topicDoc" as="document-node()" select="document($href,.)"/>
                <xsl:value-of select="$topicDoc/*[contains(@class,' topic/topic ')]"/>
            </xsl:when>
        </xsl:choose>
    </li>
</ul>

This code is redundant. You can customize it as you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM