您好我需要在dita ot插件的xsl文件中将一个模板中的变量值调用到另一个模板:
我的Ditamap文件:
<?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>
我的topic_1.dita文件
<?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>
我的topic_2.dita文件
<?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>
我的XSLT模板具有所需的变量&#34;输出类&#34;:
<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>
从上面这个模板我需要获取&#34;输出类&#34;变量到模板下面:
<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>
请在&#39; / * [df:class(。,&#39; map / map&#39;)]&#39;中建议输出类变量。模板。
提前致谢。
答案 0 :(得分:2)
<xsl:template match="*" mode="set-output-class">
模板返回attribute()?
。因此,如果您希望按原样获得此结果,则<xsl:template match="/*[df:class(., 'map/map')]">
模板中的以下代码将起作用:
<xsl:variable name="classAttr" as="attribute()?">
<xsl:apply-templates select="." mode="set-output-class"/>
</xsl:variable>
或者如果你想获得字符串值:
<xsl:variable name="classValue" as="xs:string" select="string($classAttr)"/>
就足够了。
我需要主题文件输出类属性值进行审核
我不确定EPUB转换。但是下面的代码可能是解决方案之一。
<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>
如何比较所有输出类的特定输出类
将以前的变量更改为xs:string *将使您可以在xsl:when / @ test中使用。
<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>
您可以将此变量与一般比较运算符“=”一起使用,如下所示:
<xsl:when test="$outputclass = 'Ram-Files-RajRFR'">
它没有使用此
获取h1中的值<h1><xsl:value-of select="//topic[@outputclass='Ram-Files-RajRFR']"/></h1>
如果您成功从主题文件中获取@outputclass值,则以下代码也可以正常工作。
[地图模板中的变量]
<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>
[获取主题内容]
<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>
此代码是多余的。您可以根据需要自定义它。