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

时间:2011-06-13 11:30:53

标签: xslt

如果节点不存在,我如何使用XSLT创建节点?我需要插入节点< 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>

3 个答案:

答案 0 :(得分:9)

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

“我需要在<sectionhead>

下插入节点<group>
<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>

答案 1 :(得分:7)

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

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

在应用于提供的XML文档时(没有group):

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

产生了想要的正确结果:

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

应用于第一个XML文档(group没有sectionhead子项):

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

答案 2 :(得分: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>

两阶段 - 好吗?