0

Below is the sample input for my 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>

Using XPath is it possible to capture the following-sibling of an element delimited by the previous element attribute value? In this case, I would like to store it in a List:

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

1 に答える 1

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では機能しません。

于 2013-02-18T19:57:31.263 に答える