如何将属性应用于后代元素? XSLT

时间:2013-01-17 13:30:40

标签: xslt-2.0

看看以下代码:

<xsl:template match="tocline[@toclevel='2']">
    <xsl:copy>
        <xsl:copy-of select="@*"/>

        <xsl:for-each select="descendant::toctitle">
            <xsl:if test="position() = last()">
                <xsl:attribute name="last">
                    <xsl:value-of select="'true'"/>
                </xsl:attribute>
            </xsl:if>
        </xsl:for-each>

    <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

此模板将属性应用于tocline元素。我希望它将该属性应用于节点集中的最后一个toctitle,它可以位于不同的级别。

使用此示例:

        <tocline id="d1e11" toclevel="1">
            <toctitle>Section 1. Legislative Powers</toctitle>
            <tocline id="d1e40" toclevel="2">
                <toctitle>Separation of Powers and Checks and Balances</toctitle>
                <tocline id="d1e51" toclevel="3">
                    <toctitle>The Theory Elaborated and Implemented</toctitle>
                </tocline>
                <tocline id="d1e189" toclevel="3">
                    <toctitle>Judicial Enforcement</toctitle>
                </tocline>
            </tocline>
        </tocline>

我想要这个:

        <tocline id="d1e11" toclevel="1">
            <toctitle>Section 1. Legislative Powers</toctitle>
            <tocline id="d1e40" toclevel="2">
                <toctitle>Separation of Powers and Checks and Balances</toctitle>
                <tocline id="d1e51" toclevel="3">
                    <toctitle>The Theory Elaborated and Implemented</toctitle>
                </tocline>
                <tocline id="d1e189" toclevel="3">
                    <toctitle last="true">Judicial Enforcement</toctitle>
                </tocline>
            </tocline>
        </tocline>

但我明白了:

        <tocline id="d1e11" toclevel="1">
            <toctitle>Section 1. Legislative Powers</toctitle>
            <tocline id="d1e40" toclevel="2" last="true">
                <toctitle>Separation of Powers and Checks and Balances</toctitle>
                <tocline id="d1e51" toclevel="3">
                    <toctitle>The Theory Elaborated and Implemented</toctitle>
                </tocline>
                <tocline id="d1e189" toclevel="3">
                    <toctitle>Judicial Enforcement</toctitle>
                </tocline>
            </tocline>
        </tocline>

2 个答案:

答案 0 :(得分:0)

此转化:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match=
 "toctitle
    [. is (ancestor::tocline[@toclevel eq '2'][1]//toctitle)[last()]]">
    <toctitle last="true">
      <xsl:apply-templates select="@*|node()"/>
    </toctitle>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<tocline id="d1e11" toclevel="1">
    <toctitle>Section 1. Legislative Powers</toctitle>
    <tocline id="d1e40" toclevel="2">
        <toctitle>Separation of Powers and Checks and Balances</toctitle>
        <tocline id="d1e51" toclevel="3">
            <toctitle>The Theory Elaborated and Implemented</toctitle>
        </tocline>
        <tocline id="d1e189" toclevel="3">
            <toctitle>Judicial Enforcement</toctitle>
        </tocline>
    </tocline>
</tocline>

会产生想要的正确结果:

<tocline id="d1e11" toclevel="1">
   <toctitle>Section 1. Legislative Powers</toctitle>
   <tocline id="d1e40" toclevel="2">
      <toctitle>Separation of Powers and Checks and Balances</toctitle>
      <tocline id="d1e51" toclevel="3">
         <toctitle>The Theory Elaborated and Implemented</toctitle>
      </tocline>
      <tocline id="d1e189" toclevel="3">
         <toctitle last="true">Judicial Enforcement</toctitle>
      </tocline>
   </tocline>
</tocline>

答案 1 :(得分:0)

我添加了一个密钥,这就是我想出的:

<xsl:key name="l2id" match="tocline[@toclevel eq '2']" use="@id"/>

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="toctitle[. is (ancestor-or-self::tocline/key('l2id',@id)/descendant-or-self::toctitle[last()])]">
    <xsl:copy>
        <xsl:attribute name="last">
            <xsl:value-of select="'true'"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>