XSLT:如果节点不存在,则创建节点?

8

我该如何使用XSLT创建节点,如果它们不存在?我需要在<group>下插入节点<sectionhead>,但是如果<group>节点不存在,则需要创建它。

例如:

输入(group节点存在):

<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>

输入(组节点不存在):

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

输出:

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

有很多不同的变体<xsl:if test="not(group)">xsl:copy<group><sectionhead /></group></xsl:copy><xsl:copy-of select="@*|*"/>... - ilitirit
<xsl:if test="not(group)"> 是正确的方法。我猜你只是在错误的上下文节点上进行测试。 - Tomalak
1
好问题,+1。请看我的答案,其中提供了一个完整、简短、易于实现的解决方案,使用了最基本和强大的XSLT设计模式——覆盖标识规则。 - Dimitre Novatchev
3个回答

11

尝试直接将问题描述中的规则转化为模板规则:

"我需要在<group>下插入节点<sectionhead>"

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

“但是,如果 <group> 节点不存在,则我还需要创建它。”

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

10

以下是一种完整的解决方案,它会覆盖没有group子元素的任何story元素的身份验证规则/模板:

<xsl:stylesheet version="1.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="story[not(group)]">
  <xsl:copy>
   <xsl:apply-templates select="@*"/>
     <group>
       <sectionhead />
     </group>
   <xsl:apply-templates select="node()"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="group[not(sectionhead)]">
  <xsl:copy>
   <xsl:apply-templates select="@*"/>
     <sectionhead />
   <xsl:apply-templates select="node()"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

当应用于提供的没有 group 的 XML 文档时:

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

产生的结果是所需的正确结果:

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

当应用于第一个XML文档(具有没有sectionhead子元素的group)时:

<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>

0
<xsl:for-each select="//story/group">    
  <sectionhead />  
  <xsl:copy-of select="." />  
</xsl:for-each>  
<xsl:for-each select="//story/text">  
  <group><sectionhead /></group>
  <xsl:copy-of select="." />  
</xsl:for-each>

两阶段 - 行吗?


这个有几个层面上的缺陷。 - Tomalak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接