2011-06-13 61 views
6

如何使用XSLT创建节点(如果它们不存在)?我需要插入节点<节头>下<组>,但如果<组>节点不存在,那么我也需要创建。XSLT:创建节点,如果它不存在?

例如。

输入(基节点存在):

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

<节头/> ... – ilitirit 2011-06-13 11:37:35

+0

< xsl:if test =“not(group)”>'是要走的路。我怀疑,你只是在错误的上下文节点上测试它。 – Tomalak 2011-06-13 11:39:15

+1

好问题,+1。使用最基本,功能强大的XSLT设计模式 - 覆盖身份规则,查看我的答案,获得完整,简短和简单的解决方案。 – 2011-06-13 13:48:57

回答

7

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

“我需要插入节点<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> 
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> 

两相 - 好吗?

+0

这在几个层面上存在缺陷。 – Tomalak 2011-06-13 13:46:40

6

这里是一个将覆盖任何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> 
相关问题