2009-11-17 52 views
2

在我的XML文件中的元素不是恆定喜歡說我的文件是保持在XML文件中的所有XML元素

<alpha> 
    <a>...</a> 
    <b>...</b> 
    <c>...</c> 
</alpha> 
<alpha> 
    <a>...</a> 
    <c>...</c> 
</alpha> 
<alpha> 
    <a>...</a> 
    <b>...</b> 
</alpha> 
<alpha> 
    <a>...</a> 
    <b>...</b> 
    <c>...</c> 
</alpha> 

我的意思是說,我想通過創建不存在的元素,以保持我的XML文件中的所有元素在集合中,如果任何元素存在使用xslt應該跳過。 PLS。幫助我解決這個問題。

想要輸出像下面這樣通過在xslt中創建值爲0的非現有元素。

**

<alpha> 
     <a>...</a> 
     <b>...</b> 
     <c>...</c> 
    </alpha> 
    <alpha> 
     <a>...</a> 
     <b>0</b> 
     <c>...</c> 
    </alpha> 
    <alpha> 
     <a>...</a> 
     <b>...</b> 
     <c>0</c> 
    </alpha> 
    <alpha> 
     <a>...</a> 
     <b>...</b> 
     <c>...</c> 
    </alpha> 

**

+2

您需要突出顯示您的XML,然後使用編輯器工具欄上的「代碼」按鈕(010 101)才能獲得有用的結果! – 2009-11-17 06:45:36

+0

和你想用這個XML和XSLT做什麼?將它轉換爲包含所有節點的所有元素的XML? – 2009-11-17 06:46:48

+0

我不明白你需要什麼,你能提供一個小例子(即少於5個標籤)的輸入和輸出你期望/想要 – RageZ 2009-11-17 06:46:52

回答

1

XSLT 2.0

<xsl:template match="a|b|c"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 
    <xsl:template match="alpha"> 
     <xsl:variable name="alpha" select="."/> 
     <xsl:variable name="forced"> 
      <forced> 
       <a>0</a> 
       <b>0</b> 
       <c>0</c> 
      </forced> 
     </xsl:variable> 
     <xsl:copy> 
      <xsl:for-each select="$forced/forced/*"> 
       <xsl:variable name="current" select="name()"/> 
       <xsl:choose> 
        <xsl:when test="exists($alpha/*[name() = $current])"> 
         <xsl:apply-templates select="$alpha/*[name() = $current]"/> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:apply-templates select="$forced/forced/*[name() = $current]"/> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each> 
     </xsl:copy> 
    </xsl:template> 

XSLT 1.0

<xsl:template match="/root"> 
    <root> 
     <xsl:for-each select="alpha"> 
       <alpha> 
       <xsl:choose> 
        <xsl:when test="a"> 
         <xsl:copy-of select="a"/> 
        </xsl:when> 
        <xsl:otherwise> 
         <a>0</a> 
        </xsl:otherwise> 
       </xsl:choose> 
       <xsl:choose> 
        <xsl:when test="b"> 
         <xsl:copy-of select="b"/> 
        </xsl:when> 
        <xsl:otherwise> 
         <b>0</b> 
        </xsl:otherwise> 
       </xsl:choose> 
       <xsl:choose> 
        <xsl:when test="c"> 
         <xsl:copy-of select="c"/> 
        </xsl:when> 
        <xsl:otherwise> 
         <c>0</c> 
        </xsl:otherwise> 
       </xsl:choose> 
       </alpha> 
      </xsl:for-each> 
    </root> 
</xsl:template> 
+0

它不適用於XSLT 1.0;你的變量$ forced是一個RTF(Result Tree Fragment),你不能在XPath表達式中使用RTF。 – Erlock 2009-11-17 08:20:37

+0

很好,我還添加了一個1.0模板。並不是說我非常喜歡它。我總是使用撒克遜進行處理,而2.0更好。 – 2009-11-17 08:35:43

+0

非常感謝,我得到了解決方案。 – naidu 2009-11-17 11:02:08

1

簡單,稍加修改身份轉換可以這樣做:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> 
    <xsl:template match="node() | @*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
     <xsl:if test="self::alpha"> 
     <xsl:if test="not(a)"><a>0</a></xsl:if> 
     <xsl:if test="not(b)"><b>0</b></xsl:if> 
     <xsl:if test="not(c)"><c>0</c></xsl:if> 
     </xsl:if> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

注意的是,上述不創建A,B,C在這個特定的順序元素(因爲我不認爲這是真的有必要) 。它只是確保他們三個都在那裏,並且將其餘的輸入按照原樣複製。