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

ili*_*rit 7 xslt

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

例如.

输入(组节点存在):

<story>
    <group>
        <overhead>
            <l1>overhead</l1>
        </overhead>
        <headline>
            <l1>headline</l1>
        </headline>
    </group>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>
Run Code Online (Sandbox Code Playgroud)

输出:

<story>
    <group>
        <sectionhead />
        <overhead>
            <l1>overhead</l1>
        </overhead>
        <headline>
            <l1>headline</l1>
        </headline>
    </group>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>
Run Code Online (Sandbox Code Playgroud)

输入(组节点不存在):

<story>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>
Run Code Online (Sandbox Code Playgroud)

输出:

<story>
    <group>
        <sectionhead />
    </group>    
    <text>
        <lines>
                <l1>line</l1>
            </lines>
    </text>
</story>
Run Code Online (Sandbox Code Playgroud)

Mic*_*Kay 9

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

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

<xsl:template match="group">
  <group>
    <sectionhead/>
    <xsl:apply-templates/>
  </group>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

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

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


Dim*_*hev 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>
Run Code Online (Sandbox Code Playgroud)

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

<story>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

<story>
   <group>
      <sectionhead/>
   </group>
   <text>
      <lines>
         <l1>line</l1>
      </lines>
   </text>
</story>
Run Code Online (Sandbox Code Playgroud)

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

<story>
    <group>
        <overhead>
            <l1>overhead</l1>
        </overhead>
        <headline>
            <l1>headline</l1>
        </headline>
    </group>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>
Run Code Online (Sandbox Code Playgroud)

同样的转换再次产生了想要的,正确的结果:

<story>
   <group>
      <sectionhead/>
      <overhead>
         <l1>overhead</l1>
      </overhead>
      <headline>
         <l1>headline</l1>
      </headline>
   </group>
   <text>
      <lines>
         <l1>line</l1>
      </lines>
   </text>
</story>
Run Code Online (Sandbox Code Playgroud)