我浏览了XSLT Grouping Examples和Using for-each-group for high performance XSLT。我对每个组都有问题。
我的 XML
<?xml version="1.0" encoding="UTF-8"?>
<body>
<p name="h-title" other="main">Introduction</p>
<p name="h1-title " other="other-h1">XSLT and XQuery</p>
<p name="h2-title" other=" other-h2">XSLT</p>
<p name="">
<p1 name="bold"> XSLT is used to write stylesheets.</p1>
</p>
<p name="h2-title " name="other-h2">XQuery</p>
<p name="">
<p1 name="bold"> XQuery is used to query XML databases.</p1>
</p>
<p name="h3-title" name="other-h3">XQuery and stylesheets</p>
<p name="">
<p1 name="bold"> XQuery is used to query XML databases.</p1>
</p>
<p name="h1-title " other="other-h1">XSLT and XQuery</p>
<p name="h2-title " other=" other-h2">XSLT</p>
</body>
</p>
我想要的输出
<?xml version="1.0" encoding="UTF-8"?>
<body>
<p name="h-title " other="main">Introduction</p>
<h1>
<p name="h1-title " other="other-h1"> XSLT and XQuery </p>
<h2>
<p name="h2-title " other="other-h2">XSLT</p>
<p name="">
<p1 name="bold">XSLT is used to write stylesheets.
</p1>
</p>
</h2>
<h2>
<p name="h2-title " other="other-h2"> XQuery is used to query XML databases
</p>
<p name="">
<p name="bold"> XQuery is used to query XML databases.</p>
</p>
<h3>
<p name="h3-title " name="other-h3">XQuery and stylesheets</p>
<p name="">
<p1 name="bold"> XQuery is used to query XML databases.</p1>
</p>
</h3>
</h2>
</h1>
<h1>
<p name="h1-title " other="other-h1">XSLT and XQuery</p>
<h2>
<p name="h2"-title other=" other-h2">XSLT</p>
</h2>
</h1>
</body>
我试过这个。(不工作)
<xsl:template match="body">
<body>
<xsl:for-each-group select="*" group-starting-with="@h1-title" >
<h1>
<xsl:for-each select="current-group()[self:: h1-title]">
<xsl:value-of select="."/>
</xsl:for-each>
</h1>
</xsl:for-each-group>
<xsl:for-each-group select="*" group-starting-with="@h2-title" >
<h2>
<xsl:for-each select="current-group()[self::h2-title/@h2-title]">
<xsl:value-of select="."/>
</xsl:for-each>
</h2>
</xsl:for-each-group>
<xsl:for-each-group select="*" group-starting-with="@h3-title" >
<h3>
<xsl:for-each select="current-group()[self::h2-title/@h3-title]">
<xsl:value-of select="."/>
</xsl:for-each>
</h3>
</xsl:for-each-group>
</body>
</xsl:template>
有人会告诉我获得我想要的结果的正确方法吗?