2009-11-17 50 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在这个特定的顺序元素(因为我不认为这是真的有必要) 。它只是确保他们三个都在那里,并且将其余的输入按照原样复制。