xpath

2013-02-07 47 views
10

साथ स्तंभ शीर्ष लेख नाम से तालिका स्तंभ का चयन करने के लिए कैसे मैं परिणामों की एक टेबल है और यह ऐसा दिखाई देता है:xpath

<table> 
    <thead> 
    <tr> 
     <th>Id</th> 
     <th>Type</th> 
     <th>Amount</th> 
     <th>Price</th> 
     <th>Name</th> 
     <th>Expiration</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>123</td> 
     <td>Paper</td> 
     <td>10 pcs.</td> 
     <td>$10</td> 
     <td>Premium Copier paper</td> 
     <td>None</td> 
    </tr> 
    <tr> 
     <td>321</td> 
     <td>Paper</td> 
     <td>20 pcs.</td> 
     <td>$20</td> 
     <td>Extra Copier paper</td> 
     <td>None</td> 
    </tr> 
    </tbody> 

और मैं xpath उदा के साथ अपने नाम से पूरे स्तंभ का चयन करना चाहते मैं कॉलम नाम "मूल्य" द्वारा चुने गए वापसी के परिणाम को {<td>$10</td>, <td>$20</td>} की सरणी बनाना चाहता हूं। मैं xpath के लिए नया हूं और वास्तव में यह सुनिश्चित नहीं करता कि यह कैसे करें, लेकिन मुझे पूरा यकीन है कि यह संभव है।

उत्तर

22

ठीक है, मैं जवाब है कि w मिल गया है पर्याप्त हो सकता है और काफी सुरुचिपूर्ण लग रहा है। यहाँ आवश्यक XPath स्ट्रिंग जाता है:

//table/tbody/tr/td[count(//table/thead/tr/th[.="$columnName"]/preceding-sibling::th)+1] 

$ columnName के बजाय कॉलम का नाम रखो। यह मेरे लिए अच्छा काम करता है। कोई एक्सएसएल या कुछ भी नहीं है, केवल शुद्ध xpath स्ट्रिंग है। इसे कैसे लागू करें - यह एक और सवाल है।

0

आप इस XPath इस्तेमाल कर सकते हैं:

/table/tbody/tr/td[count(preceding-sibling::td)+1 = count(ancestor::table/thead/tr/th[.='Price']/preceding-sibling::th)+1] 

मैं प्रासंगिक th की स्थिति के खिलाफ काम करेगा td की स्थिति (position()) का परीक्षण कर लगता होगा, लेकिन यह जब मैं परीक्षण था नहीं लगता था ।

+0

ऐसा लगता है कि यह काम नहीं कर रहा है। कोई विचार क्यों नहीं, लेकिन मुझे पहले ही समाधान मिल गया है। –

+0

@EduardSukharev - आपको अपना समाधान एक उत्तर के रूप में जोड़ना चाहिए और इसे स्वीकार करना चाहिए। –

+1

हां, लेकिन मैं अंक पर कम हूं इसलिए पहले 6 घंटों के लिए अपना स्वयं का समाधान नहीं जोड़ सका। –

1

आप एक समाधान मिल गया है, तो मैं एक जवाब यहाँ के रूप में यह पोस्टिंग सुझाव है, लेकिन सिर्फ मनोरंजन के लिए है, यह मैं यह कैसे दृष्टिकोण होता है:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:key name="kCol" match="td" use="count(. | preceding-sibling::td)"/> 
    <xsl:param name="column" select="'Price'" /> 

    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/"> 
    <found> 
     <xsl:apply-templates select="table/thead/tr/th" /> 
    </found> 
    </xsl:template> 

    <xsl:template match="th"> 
    <xsl:if test=". = $column"> 
     <xsl:apply-templates select="key('kCol', position())" /> 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

जब पैरामीटर के रूप में "मूल्य" के साथ चलाने मूल्य:

<found> 
    <td>$10</td> 
    <td>$20</td> 
</found> 

जब "नाम" के साथ चलाने के पैरामीटर मान के रूप में:

<found> 
    <td>Premium Copier paper</td> 
    <td>Extra Copier paper</td> 
</found>