html - 在两个同样具有特定属性的 <li> 元素之间选择具有特定属性值的 <li> 元素

标签 html xslt xpath

我正在处理一些非常困惑的 HTML 源内容,并试图将单个 ol 构造成一个属性(来自源)确定是否应将某些列表项放入其自己的子列表中。该列表也被分解成几个 ol,但我认为可以通过忽略父节点来解决这个问题。

来源:

<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>. &#160;This is a three-part field:</p>
    </li>
    <li class="kadov-p-CStepBullet">
      <p class="StepBullet">In the first part, select the bank number &#160;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> &#160;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>

我知道,这是一团糟。

所以,本质上: 任何带有@class='kadov-p-CStep' 的li 都应该是一级li。 任何在下一个 'kadov-p-Step' li 之前带有 @class='kadov-p-CStepBullet' 的 following-sibling li 应该放在其新父级下面的从属列表中。

在这里的其他地方,我找到了一个用于选择节点集交集的公式:

$ns1[count(.|$ns2)=count($ns2)] 

我试图以我自己的方式(可能非常困惑)遵循:

 <xsl:for-each select="following::li[contains(./@class,'CStepBullet')][count(.|following-sibling::li[not(contains(./@class,'Bullet'))][1]/preceding-sibling::li[contains(./@class,'CStepBullet')]) = count(following-sibling::li[not(contains(./@class,'Bullet'))][1]/preceding-sibling::li[contains(./@class,'CStepBullet')])] ">

目前这不会在输出 XML 中产生任何结果。我也确信它不必要地过度设计。

感谢您的浏览,并提前提供您可以提供的任何建议!

最佳答案

我。这个 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>. &#160;This is a three-part field:</p>
    </li>
    <li class="kadov-p-CStepBullet">
      <p class="StepBullet">In the first part, select the bank number &#160;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> &#160;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>

解释:

适当使用 key 将任何 li[@class='kadov-p-CStepBullet'] 定义为其第一个前级兄弟 li[@class='kadov-p-CStep ']。另外,正确使用 modes .


二。 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() 功能。另外,适当使用 modes ,构建模式 #current#default 以及模式列表。

关于html - 在两个同样具有特定属性的 <li> 元素之间选择具有特定属性值的 <li> 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10524438/

相关文章:

c# - 有人能告诉我为什么我的标题没有出现在 asp.net c# 中吗?

java - Java 6 中默认支持 xinclude 吗?

xslt - 如何使用 XSLT 获取嵌套 HTML 表的行

javascript - 需要一个 javascript 或 jquery 库来将 xpath 转换为 jquery 中可选择的 CSS3 格式

C# Selenium : passing a variable into XPath

html - 大绿色按钮

html - 从 css 文件中仅提取使用过的 css 类

HTML 输入类型 ="tel"与输入模式 ="tel"

XSLT 查找同级是否存在

dom - 与importXML一起使用的XPath表达式