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从平面结构到嵌套结构的转换_Xslt - Fatal编程技术网

XSLT从平面结构到嵌套结构的转换

XSLT从平面结构到嵌套结构的转换,xslt,Xslt,我无法使用简单的(?)XSLT转换 有一个输入平面结构XML: <root> <attribute_a>abc</attribute_a> <attribute_b>def</attribute_b> <attribute_c>ghi</attribute_c> <attribute_a>123</attribute_a> <attribute

我无法使用简单的(?)XSLT转换

有一个输入平面结构XML:

<root>
    <attribute_a>abc</attribute_a>
    <attribute_b>def</attribute_b>
    <attribute_c>ghi</attribute_c>
    <attribute_a>123</attribute_a>
    <attribute_b>456</attribute_b>
    <attribute_c>789</attribute_c>
    <attribute_a>xxx</attribute_a>
    <attribute_b>xxx</attribute_b>
    <attribute_c>xxx</attribute_c>
</root>

abc
def
ghi
123
456
789
xxx
xxx
xxx
我应该将其转换为如下XML:

<root>
    <attribute>
        <attribute_a>abc</attribute_a>
        <attribute_b>def</attribute_b>
        <attribute_c>ghi</attribute_c>
    </attribute>
    <attribute>
        <attribute_a>123</attribute_a>
        <attribute_b>456</attribute_b>
        <attribute_c>789</attribute_c>
    </attribute>
    <attribute>
        <attribute_a>xxx</attribute_a>
        <attribute_b>xxx</attribute_b>
        <attribute_c>xxx</attribute_c>
    </attribute>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <attribute>
        <attribute_a>abc</attribute_a>
        <attribute_b>def</attribute_b>
        <attribute_c>ghi</attribute_c>
    </attribute>
    <attribute>
        <attribute_a>abc</attribute_a>
        <attribute_b>def</attribute_b>
        <attribute_c>ghi</attribute_c>
    </attribute>
    <attribute>
        <attribute_a>abc</attribute_a>
        <attribute_b>def</attribute_b>
        <attribute_c>ghi</attribute_c>
    </attribute>
</root>

abc
def
ghi
123
456
789
xxx
xxx
xxx
但问题是经过这样的改造后:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <root>
            <xsl:for-each select="root/attribute_a">
                <attribute>
                    <attribute_a>
                        <xsl:value-of select="../attribute_a" />
                    </attribute_a>
                    <attribute_b>
                        <xsl:value-of select="../attribute_b" />
                    </attribute_b>
                    <attribute_c>
                        <xsl:value-of select="../attribute_c" />
                    </attribute_c>
                </attribute>
            </xsl:for-each>
        </root>
        <xsl:apply-templates />
    </xsl:template>
</xsl:stylesheet>

我得到了这样的东西:

<root>
    <attribute>
        <attribute_a>abc</attribute_a>
        <attribute_b>def</attribute_b>
        <attribute_c>ghi</attribute_c>
    </attribute>
    <attribute>
        <attribute_a>123</attribute_a>
        <attribute_b>456</attribute_b>
        <attribute_c>789</attribute_c>
    </attribute>
    <attribute>
        <attribute_a>xxx</attribute_a>
        <attribute_b>xxx</attribute_b>
        <attribute_c>xxx</attribute_c>
    </attribute>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <attribute>
        <attribute_a>abc</attribute_a>
        <attribute_b>def</attribute_b>
        <attribute_c>ghi</attribute_c>
    </attribute>
    <attribute>
        <attribute_a>abc</attribute_a>
        <attribute_b>def</attribute_b>
        <attribute_c>ghi</attribute_c>
    </attribute>
    <attribute>
        <attribute_a>abc</attribute_a>
        <attribute_b>def</attribute_b>
        <attribute_c>ghi</attribute_c>
    </attribute>
</root>

abc
def
ghi
abc
def
ghi
abc
def
ghi
我对XSLT不是很有经验-你有什么想法吗(

问候,,
早上好

这应该可以做到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <root>
      <xsl:for-each select="root/attribute_a">
        <xsl:variable name="pos" select="position()"/>
        <attribute>
          <xsl:apply-templates
            select="../attribute_a[$pos] | 
                    ../attribute_b[$pos] |
                    ../attribute_c[$pos]" />
        </attribute>
      </xsl:for-each>
    </root>
  </xsl:template>
</xsl:stylesheet>
在示例输入上运行这些XSLT时,会生成以下输出:

<root>
  <attribute>
    <attribute_a>abc</attribute_a>
    <attribute_b>def</attribute_b>
    <attribute_c>ghi</attribute_c>
  </attribute>
  <attribute>
    <attribute_a>123</attribute_a>
    <attribute_b>456</attribute_b>
    <attribute_c>789</attribute_c>
  </attribute>
  <attribute>
    <attribute_a>xxx</attribute_a>
    <attribute_b>xxx</attribute_b>
    <attribute_c>xxx</attribute_c>
  </attribute>
</root>

abc
def
ghi
123
456
789
xxx
xxx
xxx

欢迎来到SO。感谢您提出结构合理的第一个问题。:)这很有帮助!非常感谢!:)