XSLT常见问题(四)XSLT中select和match的匹配原则

本文详细解析了XSL中select和match指令的应用场景及使用技巧。通过具体示例介绍了如何利用这两个指令选取节点、匹配模板及获取属性值等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

非常重要:只要是select或match,其节点关系跨模板后都是可以继承的

什么时候用

select当需要选取节点或者节点属性进行匹配,或者获取属性值,变量值的时候

1、节点(模板调用)

<xsl:apply-templates select="fpage | lpage" mode="content"/>

2、满足某个条件的节点(模板的定义中,for-each等)

<xsl:template match="trans-title-group" mode="content">
        <xsl:if test="trans-title[@xml:lang='en']">
            <ArticleTitle>
                <xsl:apply-templates select="trans-title[@xml:lang='en']"/>
            </ArticleTitle>
        </xsl:if>
    </xsl:template>

3、获取属性值

<xsl:variable name="pub-id-type" select="@pub-id-type"/>

4、获取变量值

<xsl:variable name="pub-id-type" select="@pub-id-type"/>

        <xsl:if test="($pub-id-type = 'pii') or ($pub-id-type = 'doi')">
            <ELocationID>
                <xsl:attribute name="EIdType">
                    <xsl:value-of select="$pub-id-type"/>
                </xsl:attribute>
                <xsl:apply-templates/>
            </ELocationID>
        </xsl:if>

match匹配模板中select选取的节点(会自动忽略条件)

<!--AuthorList-->
    <xsl:template match="contrib-group" mode="content">
        <xsl:apply-templates select="contrib" mode="content"/>
    </xsl:template>

    <xsl:template match="contrib" mode="content">
        <xsl:if test="name-alternatives/name/given-names and name-alternatives/name/surname">
            <Author>
                <xsl:apply-templates select="name-alternatives/name" mode="name"/>
                <xsl:apply-templates select="xref[@ref-type='aff']" mode="aff"/>
            </Author>
        </xsl:if>
    </xsl:template>
用在哪儿

select出现在
- 调用模板 apply-templates
- 遍历节点 for-each

<!--AuthorList-->
    <xsl:template match="contrib-group" mode="content">
        <xsl:apply-templates select="contrib" mode="content"/>
    </xsl:template>

    <xsl:template match="contrib" mode="content">
        <xsl:if test="name-alternatives/name/given-names and name-alternatives/name/surname">
            <Author>
                <xsl:apply-templates select="name-alternatives/name" mode="name"/>
                <xsl:apply-templates select="xref[@ref-type='aff']" mode="aff"/>
            </Author>
        </xsl:if>
    </xsl:template>

match出现在template定义(一般都在会被apply-templates调用的模板中,call-template调用的模板只用name参数来实现)

<!--Language-->
    <xsl:call-template name="language"/>

 <!--Language-->
    <xsl:template name="language">
        <xsl:choose>
            <xsl:when test="($upper-local-language = 'EN')">
                <Language>
                    <xsl:text>EN</xsl:text>
                </Language>
                <Language>
                    <xsl:value-of select="$upper-other-language"/>
                </Language>
            </xsl:when>
            <xsl:when test="($upper-other-language = 'EN')">
                <Language>
                    <xsl:text>EN</xsl:text>
                </Language>
                <Language>
                    <xsl:value-of select="$upper-local-language"/>
                </Language>
            </xsl:when>
            <xsl:otherwise>
                <Language>
                    <xsl:value-of select="$upper-local-language"/>
                </Language>
                <Language>
                    <xsl:value-of select="$upper-other-language"/>
                </Language>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:template>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值