XSLT - 有条件地添加新节点
XSLT - Add new node Conditionally
我有 xml 如下,
<doc>
<section id="1">This is <style type="normal">first</style> chapter</section>
<section id="2">This is <style type="normal">second</style> chapter</section>
<section id="3">This is <style type="normal">third</style> chapter</section>
<section id="4">This is <style type="normal">forth</style> chapter</section>
<section id="5">This is <style type="normal">fifth</style> chapter</section>
<section id="6">This is <style type="normal">sixth</style> chapter</section>
<section id="7">This is <style type="normal">seventh</style> chapter</section>
</doc>
我需要的是有条件地添加名为 <newNode>
的新节点。我写的 xsl 如下,
<xsl:variable name="var" as="xs:boolean" select="true()"/>
<xsl:template match="section[position()=last()]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
<xsl:template match="section[position()=3]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
我的要求是,如果 var
值为 true()
,则在第 3 节下添加新节点,如果 var
值为 false()
,则在最后一节节点下添加新节点。我已写信在第 3 节和最后一节下添加 <newNode>
。但想不出有条件地检查 var 值并相应地添加 <newNode>
的方法。
如何在 xslt 中完成此任务?
简单使用
<xsl:template match="section[not($var) and position()=last()]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
<xsl:template match="section[$var and position()=3]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
Martin Honnen 回答风格的变化:如果有理由将 match
限制为节点选择,您还可以将依赖于 $var
的任何内容放在模板内的条件中,
<xsl:template match="...">
<xsl:if test="not($var)">
<section id="{@id}">
...
@MartinHonnen 的回答的简化版本
<xsl:template match="section[position()=(if ($var) then 3 else last())]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
我有 xml 如下,
<doc>
<section id="1">This is <style type="normal">first</style> chapter</section>
<section id="2">This is <style type="normal">second</style> chapter</section>
<section id="3">This is <style type="normal">third</style> chapter</section>
<section id="4">This is <style type="normal">forth</style> chapter</section>
<section id="5">This is <style type="normal">fifth</style> chapter</section>
<section id="6">This is <style type="normal">sixth</style> chapter</section>
<section id="7">This is <style type="normal">seventh</style> chapter</section>
</doc>
我需要的是有条件地添加名为 <newNode>
的新节点。我写的 xsl 如下,
<xsl:variable name="var" as="xs:boolean" select="true()"/>
<xsl:template match="section[position()=last()]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
<xsl:template match="section[position()=3]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
我的要求是,如果 var
值为 true()
,则在第 3 节下添加新节点,如果 var
值为 false()
,则在最后一节节点下添加新节点。我已写信在第 3 节和最后一节下添加 <newNode>
。但想不出有条件地检查 var 值并相应地添加 <newNode>
的方法。
如何在 xslt 中完成此任务?
简单使用
<xsl:template match="section[not($var) and position()=last()]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
<xsl:template match="section[$var and position()=3]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>
Martin Honnen 回答风格的变化:如果有理由将 match
限制为节点选择,您还可以将依赖于 $var
的任何内容放在模板内的条件中,
<xsl:template match="...">
<xsl:if test="not($var)">
<section id="{@id}">
...
@MartinHonnen 的回答的简化版本
<xsl:template match="section[position()=(if ($var) then 3 else last())]">
<section id="{@id}">
<xsl:apply-templates/>
</section>
<newNode>New Node</newNode>
</xsl:template>