2011-06-13 57 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> 
相關問題