使用XSLT从其余子节点拆分第一个子节点

时间:2013-05-08 03:04:25

标签: xslt split nodes

任何人都可以帮助我使用XSLT拆分子节点。只有child1应与其他孩子分开。

<parent>
    <child1>
    <child2>
    <child3>
<parent>

输出:

<parent>
    <child1>
    <element>
         <child2>
         <child3>
    </element>
<parent>

2 个答案:

答案 0 :(得分:0)

这是一个简单的解决方案。

当这个XSLT:

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

  <xsl:template match="/*">
    <parent>
      <xsl:copy-of select="child1"/>
      <element>
        <xsl:copy-of select="*[not(self::child1)]"/>
      </element>
    </parent>
  </xsl:template>
</xsl:stylesheet>

...适用于提供的XML:

<parent>
  <child1/>
  <child2/>
  <child3/>
</parent>

...生成了想要的结果:

<parent>
  <child1/>
  <element>
    <child2/>
    <child3/>
  </element>
</parent>

答案 1 :(得分:-3)

如果我理解正确,你有xml这样:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="data.xsl"?>
<data>
    <parent>
        <child>123</child>
        <child>345</child>
        <child>678</child>
    </parent>
</data>

想要像这样展示:

<span>123</span>
<ul>
   <li>345</li>
   <li>678</li>
</ul>

如果是,请使用下一个代码:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <head>
                <title></title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="parent">
        <strong><xsl:value-of select="child[1]/text()"/></strong>
        <ul>
        <xsl:for-each select="child">
            <xsl:if test="position() != 1">
                <li><xsl:value-of select="text()"/></li>
            </xsl:if>
        </xsl:for-each>
        </ul>
    </xsl:template>
</xsl:stylesheet>