2013-02-18 4 views
0

다음은 HAP에 대한 샘플 입력입니다.HtmlAgilityPack XML 다음 형제 캡처

<?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 속성 값 이전 요소로 구분 요소의 다음 - 형제를 캡처하는 것이 가능합니다 사용하십니까? 이 경우, 나는 목록에 저장하고 싶습니다 :

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

답변

0

나는 그냥 XPath의 가능합니다 생각하지 않습니다,하지만 XSLT와 확실히 가능하다.

이 XSLT : 샘플 XML에 적용

<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> 

이 생성됩니다

작업이 XPath에 의해 이루어집니다
<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> 

:

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

$nextCategoryCount이 설정되어 현재 카테고리 다음의 카테고리 수. 식을 실행하기 전에 해당 변수를 설정할 방법이 없으므로 순수 XPath에서는 작동하지 않습니다.

+0

노력해 주셔서 감사합니다. –

+0

@FreddieFabregas : 안녕하세요. – MiMo