I. 这个 XSLT 1.0 转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kFollowing" match="li[@class='kadov-p-CStepBullet']"
use="generate-id(preceding-sibling::li[@class='kadov-p-CStep'][1])"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="li[@class='kadov-p-CStep']">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:variable name="vFollowing" select="key('kFollowing', generate-id())"/>
<xsl:if test="$vFollowing">
<ul>
<xsl:apply-templates select="$vFollowing" mode="inGroup"/>
</ul>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template match="li[@class='kadov-p-CStepBullet']"/>
<xsl:template match="li[@class='kadov-p-CStepBullet']" mode="inGroup">
<xsl:call-template name="identity"/>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<div class="x-popup-text c3" id="POPUP172050488">
<p>To add multiple balance adjustments:</p>
<ol>
<li class="kadov-p-CStep">
<p class="Step">Check
<span class="hcp1">Add to queue</span> at the bottom of the page.</p>
</li>
<li class="kadov-p-CStep">
<p class="Step">At the top of the page, enter the
<span class="hcp1">Account</span>.  This is a three-part field:</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">In the first part, select the bank number  from the drop-down list.</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">In the second part, select the application code from the drop-down list.</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">In the third part, enter the account number or click the account search button
<img src="../mag_glass_blue_bkgrd.gif" x-maintain-ratio="TRUE" width="16" height="16" border="0" class="hcp2 c1" /> to find it.</p>
</li>
</ol>
<ol start="3">
<li class="kadov-p-CStep">
<p class="Step">Enter the start date for the adjustment in the
<span class="hcp1">From</span> field or click the calendar button
<img src="../calendar.gif" x-maintain-ratio="TRUE" width="16" height="18" border="0" class="hcp2 c2" /> to select the date.</p>
</li>
<li class="kadov-p-CStep">
<p class="Step">Enter the end date for the adjustment in the
<span class="hcp1">Through</span> field or click the calendar button
<img src="../calendar.gif" x-maintain-ratio="TRUE" width="16" height="18" border="0" class="hcp2 c2" /> to select the date.</p>
</li>
</ol>
<p class="StepText">
<span class="hcp1">Tip:</span>  The Through date must be the same as or after the From date.</p>
<ol start="5">
<li class="kadov-p-CStep">
<p class="Step">For each balance you want to adjust, do the following:</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">In the table at the bottom of the page, find the appropriate
<span class="hcp1">Balance Type</span> for the adjustment.</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">Enter the
<span class="hcp1">Amount</span> of the adjustment to the right of the balance type.</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">If you want the adjustment to appear on the customer's statement, check the
<span class="hcp1">Print on Statements</span> checkbox that corresponds to the adjustment amount you entered.</p>
</li>
</ol>
<ol start="6">
<li class="kadov-p-CStep">
<p class="Step">Click
<span class="hcp1">Add</span>.</p>
</li>
<li class="kadov-p-CStep">
<p class="Step">Repeat steps 2 through 7 for each additional adjustment you want to add.</p>
</li>
<li class="kadov-p-CStep">
<p class="Step">Click the
<span class="hcp1">View queue</span> link at the bottom of the page to enter the Work Queue and apply the adjustments.</p>
</li>
</ol>
</div>
产生想要的正确结果:
<div class="x-popup-text c3" id="POPUP172050488">
<p>To add multiple balance adjustments:</p>
<ol>
<li class="kadov-p-CStep">
<p class="Step">Check
<span class="hcp1">Add to queue</span> at the bottom of the page.</p>
</li>
<li class="kadov-p-CStep">
<p class="Step">At the top of the page, enter the
<span class="hcp1">Account</span>. This is a three-part field:</p>
<ul>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">In the first part, select the bank number from the drop-down list.</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">In the second part, select the application code from the drop-down list.</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">In the third part, enter the account number or click the account search button
<img src="../mag_glass_blue_bkgrd.gif" x-maintain-ratio="TRUE" width="16"
height="16"
border="0"
class="hcp2 c1"/> to find it.</p>
</li>
</ul>
</li>
</ol>
<ol start="3">
<li class="kadov-p-CStep">
<p class="Step">Enter the start date for the adjustment in the
<span class="hcp1">From</span> field or click the calendar button
<img src="../calendar.gif" x-maintain-ratio="TRUE" width="16" height="18" border="0"
class="hcp2 c2"/> to select the date.</p>
</li>
<li class="kadov-p-CStep">
<p class="Step">Enter the end date for the adjustment in the
<span class="hcp1">Through</span> field or click the calendar button
<img src="../calendar.gif" x-maintain-ratio="TRUE" width="16" height="18" border="0"
class="hcp2 c2"/> to select the date.</p>
</li>
</ol>
<p class="StepText">
<span class="hcp1">Tip:</span> The Through date must be the same as or after the From date.</p>
<ol start="5">
<li class="kadov-p-CStep">
<p class="Step">For each balance you want to adjust, do the following:</p>
<ul>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">In the table at the bottom of the page, find the appropriate
<span class="hcp1">Balance Type</span> for the adjustment.</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">Enter the
<span class="hcp1">Amount</span> of the adjustment to the right of the balance type.</p>
</li>
<li class="kadov-p-CStepBullet">
<p class="StepBullet">If you want the adjustment to appear on the customer's statement, check the
<span class="hcp1">Print on Statements</span> checkbox that corresponds to the adjustment amount you entered.</p>
</li>
</ul>
</li>
</ol>
<ol start="6">
<li class="kadov-p-CStep">
<p class="Step">Click
<span class="hcp1">Add</span>.</p>
</li>
<li class="kadov-p-CStep">
<p class="Step">Repeat steps 2 through 7 for each additional adjustment you want to add.</p>
</li>
<li class="kadov-p-CStep">
<p class="Step">Click the
<span class="hcp1">View queue</span> link at the bottom of the page to enter the Work Queue and apply the adjustments.</p>
</li>
</ol>
</div>
说明:
适当地使用键将 any 定义li[@class='kadov-p-CStepBullet']
为其第一个前面的兄弟的函数li[@class='kadov-p-CStep']
。此外,正确使用模式。
二、XSLT 2.0 解决方案:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*" mode="#default inGroup">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ol[1]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="li"
group-starting-with="li[@class='kadov-p-CStep']">
<xsl:apply-templates select="." mode="inGroup"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="li[@class='kadov-p-CStep']">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:if test="current-group()[2]">
<ul>
<xsl:apply-templates select="current-group()[position() gt 1]" mode="inGroup"/>
</ul>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template match="li[@class='kadov-p-CStepBullet']"/>
</xsl:stylesheet>
说明:
正确使用xsl:for-each-group
具有属性group-starting-with
和current-group()
功能的指令。此外,适当使用模式、内置模式#current
和#default
模式列表。