HtmlAgilityPack XML捕获兄弟姐妹

时间:2013-02-18 05:15:25

标签: c# xml xpath html-agility-pack

以下是我的HAP的示例输入。

<?xml version="1.0" encoding="UTF-8"?>

<html>
  <div class="category">Name:</div>
  <div class="category1">Company ABC</div>
  <div class="category">ID:</div>
  <div class="category1">1</div>
  <div class="category">Location:</div>
  <div class="category1">Home ABC</div>
  <div class="category1">Home DEF</div>
</html>

使用XPath是否可以捕获由前一个元素属性值分隔的元素的以下兄弟?在这种情况下,我想将其存储在List中:

"Name" , {"Company ABC"}
"ID", {"1"}
"Location", {"Home ABC", "Home DEF"}

1 个答案:

答案 0 :(得分:0)

我认为只有XPath才有可能,但XSLT肯定是可行的。

这个XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="div[@class='category']">
    <div>
      <div class="category">
        <xsl:value-of select="."/>
      </div>
      <div>
        <xsl:variable name="nextCategoryCount" select="count(following-sibling::div[@class='category'])"/>
        <xsl:for-each select="following-sibling::div[count(following-sibling::div[@class='category']) = $nextCategoryCount]">
          <xsl:copy-of select="."/>          
        </xsl:for-each>
      </div>
    </div>

  </xsl:template>

  <xsl:template match="/html">
    <html>
      <xsl:apply-templates select="div[@class='category']"/>
    </html>
  </xsl:template>
</xsl:stylesheet>

应用于样本XML产生:

<html>
  <div>
    <div class="category">Name:</div>
    <div>
      <div class="category1">Company ABC</div>
    </div>
  </div>
  <div>
    <div class="category">ID:</div>
    <div>
      <div class="category1">1</div>
    </div>
  </div>
  <div>
    <div class="category">Location:</div>
    <div>
      <div class="category1">Home ABC</div>
      <div class="category1">Home DEF</div>
    </div>
  </div>
</html>

这项工作由XPath完成:

following-sibling::div[count(following-sibling::div[@class='category']) = $nextCategoryCount]

其中$nextCategoryCount设置为当前值之后的类别数。这在纯XPath中不起作用,因为在执行表达式之前无法设置该变量。