Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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
Xml 用XSL包装某些节点_Xml_Xslt - Fatal编程技术网

Xml 用XSL包装某些节点

Xml 用XSL包装某些节点,xml,xslt,Xml,Xslt,我想用包装的子节点,这些子节点不是或 注意:和将始终是的第一个子节点 将其转换为: <root> <foo> <bar>bar</bar> <baz>baz</baz> <qux>qux</qux> <grault>grault</grault> </foo> <foo>

我想用
包装
的子节点,这些子节点不是

注意:
将始终是
的第一个子节点

将其转换为:

<root>
    <foo>
       <bar>bar</bar>
       <baz>baz</baz>
       <qux>qux</qux>
       <grault>grault</grault>
    </foo>
     <foo>
       <bar>bar</bar>
       <baz>baz</baz>
       <qux>qux</qux>
       <quux>quux</quux>
    </foo>
</root>

酒吧
巴兹
库克斯
格劳特
酒吧
巴兹
库克斯
库克斯
为此:

<root>
    <foo>
       <bar>bar</bar>
       <baz>baz</baz>
       <corge>
           <qux>qux</qux>
           <grault>grault</grault>
       </corge>
    </foo>
    <foo>
       <bar>bar</bar>
       <baz>baz</baz>
       <corge>
           <qux>qux</qux>
           <quux>quux</quux>
       </corge>
    </foo>
</root>

酒吧
巴兹
库克斯
格劳特
酒吧
巴兹
库克斯
库克斯
使用XSL执行此操作的好方法是什么?

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="foo/*"/>
    <xsl:template match="foo/bar|foo/baz">
        <xsl:call-template name="identity"/>
    </xsl:template>
    <xsl:template match="foo/*[not(self::bar or self::baz)][1]">
        <corge>
            <xsl:apply-templates select="../*[not(self::bar or self::baz)]"
                                 mode="corge"/>
        </corge>
    </xsl:template>
    <xsl:template match="foo/*" mode="corge">
        <xsl:call-template name="identity"/>
    </xsl:template>
</xsl:stylesheet>

我会这样做的

XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <foo>
    <bar>bar</bar>
    <baz>baz</baz>
    <qux>qux</qux>
    <grault>grault</grault>
  </foo>
  <foo>
    <bar>bar</bar>
    <baz>baz</baz>
    <qux>qux</qux>
    <quux>quux</quux>
  </foo>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="foo">
    <foo>
      <xsl:apply-templates select="bar|baz"/>
      <corge>
        <xsl:apply-templates select="*[name() != 'bar' and name() != 'baz']"/>
      </corge>
    </foo>
  </xsl:template>
</xsl:stylesheet>

酒吧
巴兹
库克斯
格劳特
酒吧
巴兹
库克斯
库克斯
XSL

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <foo>
    <bar>bar</bar>
    <baz>baz</baz>
    <qux>qux</qux>
    <grault>grault</grault>
  </foo>
  <foo>
    <bar>bar</bar>
    <baz>baz</baz>
    <qux>qux</qux>
    <quux>quux</quux>
  </foo>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="foo">
    <foo>
      <xsl:apply-templates select="bar|baz"/>
      <corge>
        <xsl:apply-templates select="*[name() != 'bar' and name() != 'baz']"/>
      </corge>
    </foo>
  </xsl:template>
</xsl:stylesheet>

输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <foo>
      <bar>bar</bar>
      <baz>baz</baz>
      <corge>
         <qux>qux</qux>
         <grault>grault</grault>
      </corge>
   </foo>
   <foo>
      <bar>bar</bar>
      <baz>baz</baz>
      <corge>
         <qux>qux</qux>
         <quux>quux</quux>
      </corge>
   </foo>
</root>

酒吧
巴兹
库克斯
格劳特
酒吧
巴兹
库克斯
库克斯