Xslt 在不应该调用模板的位置调用模板
我有以下两个XML案例 案例1:Xslt 在不应该调用模板的位置调用模板,xslt,Xslt,我有以下两个XML案例 案例1: <para>Rent is the sum of money paid by the Tenant to the Landlord for the exclusive use of premises. The Landlord and Tenant signs a <page num="4"/>tenancy agreement which has to be stamped with the tax authorities as req
<para>Rent is the sum of money paid by the Tenant to the Landlord for the exclusive use of premises. The Landlord and Tenant signs a <page num="4"/>tenancy agreement which has to be stamped with the tax authorities as required under the Stamp Duties Act. The stamping of a tenancy agreement gives it validity but if the tenancy agreement is not stamped that does not mean</para>
租金是承租人向出租人支付的专用于房屋的款项。业主和承租人签署租赁协议,该协议必须按照《印花税法》的要求加盖税务机关的印章。在租赁协议上加盖印花可使其有效,但如果租赁协议未加盖印花,则并不意味着
案例2:
<para><page num="5"/>The Writ of Distress proceedings is an effective way to recover arrears in rent but regard must be had to the Landlord/Tenant relationship and the effect of publicity of such proceedings to the image of the building amongst other things.</para>
扣押令诉讼是追讨欠租的有效方式,但必须考虑业主/承租人关系以及此类诉讼对建筑物形象的影响等。
下面是XSLT
<xsl:template match="para">
<xsl:apply-templates select="child::node()[(self::page)]"/>
<li class="item">
<div class="para">
<span class="item-num">
<xsl:value-of select="../@num"></xsl:value-of>
</span>
<xsl:apply-templates select="child::node()[not(self::page)]"/>
</div>
</li>
</xsl:template>
<xsl:template match="page">
<xsl:processing-instruction name="pb">
<xsl:text>label='</xsl:text>
<xsl:value-of select="./@num"/>
<xsl:text>'</xsl:text>
<xsl:text>?</xsl:text>
</xsl:processing-instruction>
<a name="{concat('pg_',./@num)}"/>
<xsl:apply-templates/>
</xsl:template>
标签
'
?
我认为您的意思是,当页面是段落下的第一个子节点时,您希望执行额外的处理。您的应用模板需要如下所示
<xsl:apply-templates select="node()[1][self::page]" />
<xsl:apply-templates select="node()[1][self::page]" mode="first"/>
<xsl:template match="page" mode="first">
但是,听起来您也希望在页面元素上执行其他处理。您可能需要两个模板匹配此处的页面,但其中一个模板带有“模式”,以将其与正常处理区分开来
这样说吧
<xsl:apply-templates select="node()[1][self::page]" />
<xsl:apply-templates select="node()[1][self::page]" mode="first"/>
<xsl:template match="page" mode="first">
像这样搭配
<xsl:apply-templates select="node()[1][self::page]" />
<xsl:apply-templates select="node()[1][self::page]" mode="first"/>
<xsl:template match="page" mode="first">
这将包含输出处理指令的代码
对于页面元素的“正常”处理,只需使用另一个模板匹配页面,而不使用模式
<xsl:template match="page">
<a name="{concat('pg_',./@num)}"/>
<xsl:apply-templates/>
</xsl:template>
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="para">
<xsl:apply-templates select="node()[1][self::page]" mode="first"/>
<li class="item">
<div class="para">
<span class="item-num">
<xsl:value-of select="../@num"></xsl:value-of>
</span>
<xsl:apply-templates />
</div>
</li>
</xsl:template>
<xsl:template match="page">
<a name="{concat('pg_',./@num)}"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="page" mode="first">
<xsl:processing-instruction name="pb">
<xsl:text>label='</xsl:text>
<xsl:value-of select="./@num"/>
<xsl:text>'</xsl:text>
</xsl:processing-instruction>
</xsl:template>
</xsl:stylesheet>
标签
'
编辑:如果不希望两个“页面”模板都应用于第一个页面元素,则添加以下模板以忽略它
<xsl:template match="page[not(preceding-sibling::node())]" />
请注意,只有当文档中存在
时,这才起作用,以去除仅空白的文本节点。或者,你可以写这个
<xsl:template match="page[not(preceding-sibling::node()[not(self::text()) or normalize-space()])]" />
编辑2:您需要额外模板的原因是因为这一行
<xsl:apply-templates />
这将查找与当前para元素下的所有子元素匹配的模板。因此,对于页面元素,将匹配以下模板
<xsl:template match="page">
但是在这种情况下,您表示不希望第一个页面元素匹配。因此,您是一个更“特定”的模板来匹配它。比如说
<xsl:template match="page[not(preceding-sibling::node())]" />
此模板匹配前面没有同级的页面元素;i、 e.段落下的第一个元素
XSLT具有模板优先级的概念。如果模板匹配具有指定条件的元素,则该模板将始终具有优先级。在这种情况下,特定模板只是忽略页面元素,以确保它不会得到输出
对于其他页面元素,其他模板将正常使用。我认为您的意思是,当页面是段落下的第一个子节点时,您希望执行额外的处理。您的应用模板需要如下所示
<xsl:apply-templates select="node()[1][self::page]" />
<xsl:apply-templates select="node()[1][self::page]" mode="first"/>
<xsl:template match="page" mode="first">
但是,听起来您也希望在页面元素上执行其他处理。您可能需要两个模板匹配此处的页面,但其中一个模板带有“模式”,以将其与正常处理区分开来
这样说吧
<xsl:apply-templates select="node()[1][self::page]" />
<xsl:apply-templates select="node()[1][self::page]" mode="first"/>
<xsl:template match="page" mode="first">
像这样搭配
<xsl:apply-templates select="node()[1][self::page]" />
<xsl:apply-templates select="node()[1][self::page]" mode="first"/>
<xsl:template match="page" mode="first">
这将包含输出处理指令的代码
对于页面元素的“正常”处理,只需使用另一个模板匹配页面,而不使用模式
<xsl:template match="page">
<a name="{concat('pg_',./@num)}"/>
<xsl:apply-templates/>
</xsl:template>
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="para">
<xsl:apply-templates select="node()[1][self::page]" mode="first"/>
<li class="item">
<div class="para">
<span class="item-num">
<xsl:value-of select="../@num"></xsl:value-of>
</span>
<xsl:apply-templates />
</div>
</li>
</xsl:template>
<xsl:template match="page">
<a name="{concat('pg_',./@num)}"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="page" mode="first">
<xsl:processing-instruction name="pb">
<xsl:text>label='</xsl:text>
<xsl:value-of select="./@num"/>
<xsl:text>'</xsl:text>
</xsl:processing-instruction>
</xsl:template>
</xsl:stylesheet>
标签
'
编辑:如果不希望两个“页面”模板都应用于第一个页面元素,则添加以下模板以忽略它
<xsl:template match="page[not(preceding-sibling::node())]" />
请注意,只有当文档中存在
时,这才起作用,以去除仅空白的文本节点。或者,你可以写这个
<xsl:template match="page[not(preceding-sibling::node()[not(self::text()) or normalize-space()])]" />
编辑2:您需要额外模板的原因是因为这一行
<xsl:apply-templates />
这将查找与当前para元素下的所有子元素匹配的模板。因此,对于页面元素,将匹配以下模板
<xsl:template match="page">
但是在这种情况下,您表示不希望第一个页面元素匹配。因此,您是一个更“特定”的模板来匹配它。比如说
<xsl:template match="page[not(preceding-sibling::node())]" />
此模板匹配前面没有同级的页面元素;i、 e.段落下的第一个元素
XSLT具有模板优先级的概念。如果模板匹配具有指定条件的元素,则该模板将始终具有优先级。在这种情况下,特定模板只是忽略页面元素,以确保它不会得到输出
对于其他页面元素,另一个模板将正常使用。在这两种情况下页面
元素都是段落
的直接子元素。嗨@IanRoberts,我的意思是,在案例1中是文本,然后是页面,在案例2中是直接的