您的位置:首页 > 其它

XSLT常见问题及解决(三)xsl中variable标签在什么情况下使用

2017-07-13 18:25 459 查看
问题描述:

xsl中variable标签在什么情况下使用
解决:

当在某一个模板里要使用到 match的当前作用域的根节点之上的节点时,可以在外层定义一个该节点的变量,方便使用
变量名称是‘name’,变量值分两种情况:

1)variable标签中含有select属性,那么其值就是select的值

2)variable标签中不包含select属性,其值是标签之间的内容,下面的两个例子都是这种情况

variable标签经常与$符号配对出现

$是取值符号,用在变量取值(@是取值符号,用在属性(xml中的节点属性)取值)

xsl:

eg1:

<xsl:template name="assign-id">
<xsl:variable name="id">
<xsl:apply-templates select="." mode="id"/>
</xsl:variable>
<xsl:attribute name="id">
<xsl:value-of select="$id"/>
</xsl:attribute>
</xsl:template>

eg2:

可以与choose等标签共同使用,类似于一个有if/else的函数返回一个变量值,

<xsl:template name="named-anchor">
<!-- generates an HTML named anchor -->
<xsl:variable name="id">
<xsl:choose>
<xsl:when test="@id">
<!-- if we have an @id, we use it -->
<xsl:value-of select="@id"/>
</xsl:when>
<xsl:when test="not(preceding-sibling::*) and
(parent::alternatives | parent::name-alternatives |
parent::citation-alternatives | parent::collab-alternatives |
parent::aff-alternatives)/@id">
<!-- if not, and we are first among our siblings inside one of
several 'alternatives' wrappers, we use its @id if available -->
<xsl:value-of select="(parent::alternatives | parent::name-alternatives |
parent::citation-alternatives | parent::collab-alternatives |
parent::aff-alternatives)/@id"/>
</xsl:when>
<xsl:otherwise>
<!-- otherwise we simply generate an ID -->
<xsl:value-of select="generate-id(.)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<a id="{$id}">
<xsl:comment>named anchor</xsl:comment>
</a>
</xsl:template>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐