首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XSLT:如果节点不存在,是否创建节点?

XSLT:如果节点不存在,是否创建节点?
EN

Stack Overflow用户
提问于 2011-06-13 19:30:53
回答 3查看 12.1K关注 0票数 8

如果节点不存在,如何使用XSLT创建节点?我需要在下面插入节点,但是如果节点不存在,那么我也需要创建它。

例如:

输入(组节点存在):

代码语言:javascript
运行
复制
<story>
    <group>
        <overhead>
            <l1>overhead</l1>
        </overhead>
        <headline>
            <l1>headline</l1>
        </headline>
    </group>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>

输出:

代码语言:javascript
运行
复制
<story>
    <group>
        <sectionhead />
        <overhead>
            <l1>overhead</l1>
        </overhead>
        <headline>
            <l1>headline</l1>
        </headline>
    </group>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>

输入(组节点不存在):

代码语言:javascript
运行
复制
<story>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>

输出:

代码语言:javascript
运行
复制
<story>
    <group>
        <sectionhead />
    </group>    
    <text>
        <lines>
                <l1>line</l1>
            </lines>
    </text>
</story>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-13 21:10:33

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

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

代码语言:javascript
运行
复制
<xsl:template match="group">
  <group>
    <sectionhead/>
    <xsl:apply-templates/>
  </group>
</xsl:template>

但是,如果<group>节点不存在,那么我也需要创建该节点。

代码语言:javascript
运行
复制
<xsl:template match="story[not(group)]">
  <story>
    <group>
      <sectionhead/>
    </group>
    <xsl:apply-templates/>
  </story>
</xsl:template>
票数 10
EN

Stack Overflow用户

发布于 2011-06-13 21:47:33

下面是一个完整的解决方案,它覆盖了任何没有story group 子级的元素的标识规则/模板:

代码语言:javascript
运行
复制
<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**):**

代码语言:javascript
运行
复制
<story>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>

生成所需的正确结果:

代码语言:javascript
运行
复制
<story>
   <group>
      <sectionhead/>
   </group>
   <text>
      <lines>
         <l1>line</l1>
      </lines>
   </text>
</story>

应用于第一个XML文档(具有没有group sectionhead 子级的)时的

代码语言:javascript
运行
复制
<story>
    <group>
        <overhead>
            <l1>overhead</l1>
        </overhead>
        <headline>
            <l1>headline</l1>
        </headline>
    </group>
    <text>
        <lines>
            <l1>line</l1>
        </lines>
    </text>
</story>

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

代码语言:javascript
运行
复制
<story>
   <group>
      <sectionhead/>
      <overhead>
         <l1>overhead</l1>
      </overhead>
      <headline>
         <l1>headline</l1>
      </headline>
   </group>
   <text>
      <lines>
         <l1>line</l1>
      </lines>
   </text>
</story>
票数 9
EN

Stack Overflow用户

发布于 2011-06-13 19:58:54

代码语言:javascript
运行
复制
<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>

两个阶段-好吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6329843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档