Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 维护xml文件中的所有xml元素_Xslt - Fatal编程技术网

Xslt 维护xml文件中的所有xml元素

Xslt 维护xml文件中的所有xml元素,xslt,Xslt,在我的xml文件中,元素并不像我的文件那样是常量 <alpha> <a>...</a> <b>...</b> <c>...</c> </alpha> <alpha> <a>...</a> <c>...</c> </alpha> <alpha> <a>...</a> &

在我的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存在任何元素,则应该跳过。请。帮我解决这个问题

希望通过在xslt中创建值为0的不存在元素的输出如下所示

**


...
...
...
...
0
...
...
...
0
...
...
...

**XSLT2.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>
<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
0
0
XSLT1.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>
<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
0
0

一个简单、稍加修改的身份转换可以做到这一点:

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

0
0
0

请注意,上面并没有按照这个特定的顺序创建a、b、c元素(因为我认为这不是真的必要)。它只是确保所有三个都在那里,并按原样复制其余的输入。

您需要突出显示XML,然后使用编辑器工具栏上的“代码”按钮(010 101)来获得有用的结果!您想用这个XML和XSLT做什么?将其转换为包含所有节点的所有元素的XML?我不明白您需要什么,您能提供一个输入和输出的小示例(即少于5个标记)吗/want@marc_c,我非常喜欢看到代码在您编辑它时不断演变,我刷新了屏幕。就像动画一样。@pavium:哈哈@naidu:感谢您的编辑…它不能与XSLT1.0一起工作;变量$forced是一个RTF(结果树片段),不能在XPath表达式中使用RTF。并不是说我很喜欢它。我总是使用saxon进行处理,2.0更好。非常感谢,我得到了解决方案。