XSLT:如果节点不存在,则创建节点? [英] XSLT: Create node if it doesn't exist?

查看:36
本文介绍了XSLT:如果节点不存在,则创建节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果节点不存在,我如何使用 XSLT 创建节点?我需要插入节点<sectionhead>在 <group> 下,但是如果 <group>节点不存在,那么我也需要创建它.

How can I use XSLT to create nodes if they don't exist? I need to insert the node <sectionhead> under <group>, but if the <group> node doesn't exist, then I need to create that as well.

例如.

输入(组节点存在):

<story>
    <group>
        <overhead>
            <l1>overhead</l1>
        </overhead>
        <headline>
            <l1>headline</l1>
        </headline>
    </group>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>

输出:

<story>
    <group>
        <sectionhead />
        <overhead>
            <l1>overhead</l1>
        </overhead>
        <headline>
            <l1>headline</l1>
        </headline>
    </group>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>

输入(组节点不存在):

Input (group node does not exist):

<story>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>

输出:

<story>
    <group>
        <sectionhead />
    </group>    
    <text>
        <lines>
                <l1>line</l1>
            </lines>
    </text>
</story>

推荐答案

尝试将问题描述中的规则直接翻译成模板规则:

Try to translate the rules in your problem description directly into template rules:

"我需要在下插入节点"

"I need to insert the node <sectionhead> under <group>"

<xsl:template match="group">
  <group>
    <sectionhead/>
    <xsl:apply-templates/>
  </group>
</xsl:template>

但是如果 节点不存在,那么我也需要创建它."

"but if the <group> node doesn't exist, then I need to create that as well."

<xsl:template match="story[not(group)]">
  <story>
    <group>
      <sectionhead/>
    </group>
    <xsl:apply-templates/>
  </story>
</xsl:template>

这篇关于XSLT:如果节点不存在,则创建节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆