I need help creating an XPath, to select all subsequent text and font format from a div class

select xpath text fonts children

15 观看

1回复

1 作者的声誉

I would like to select all the text, and all subsequent child text, within a specific div class.

I am trying to grab the description of products from an online store.

If I use the following XPath, I get only "LP"

      //div [@id="tab3"]//div [@class='cms-content']/child::*

The code im trying to gather all the text from:

    <div id='tab3'>
    <h2 class='toggle'>Track Listing</h2>
    <div class='cms-content'> 


    <h4>LP</h4>

        <ol>

        <li> Norman fucking Rockwell </li> 

        <li> Mariners Apartment Complex </li> 

        <li> Venice Bitch </li> 

        <li> Fuck it I love you </li> 

        <li> Doin' Time </li> 

        <li> Love song </li> 

        <li> Cinnamon Girl </li> 

        <li> How to disappear </li> 

        <li> California </li> 

        <li> The Next Best American Record </li> 

        <li> The greatest </li> 

        <li> Bartender </li> 

        <li> Happiness is a butterfly </li> 

        <li> hope is a dangerous thing for a woman like me to have - but I have it </li> 

        </ol> 

I would like the solution to grab all of the text. Ideally, I would like it add a new line for each text item. To look like:

LP
Norman fucking Rockwell
Mariners Apartment Complex
Venice Bitch
Fuck it I love you
Doin' Time
Love song
Cinnamon Girl
How to disappear
California
The Next Best American Record
The greatest Bartender
Happiness is a butterfly
hope is a dangerous thing for a woman like me to have - but I have it

作者: Jackson Harvey 的来源 发布者: 2019 年 8 月 13 日

回应 1


0

1380 作者的声誉

You need to replace "tab3" to 'tab3':

XML:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <div id="tab3">
        <h2 class="toggle">Track Listing</h2>
        <div class="cms-content">
            <h4>LP</h4>
            <ol>
                <li> Norman fucking Rockwell </li>
                <li> Mariners Apartment Complex </li>
                <li> Venice Bitch </li>
                <li> Fuck it I love you </li>
                <li> Doin' Time </li>
                <li> Love song </li>
                <li> Cinnamon Girl </li>
                <li> How to disappear </li>
                <li> California </li>
                <li> The Next Best American Record </li>
                <li> The greatest </li>
                <li> Bartender </li>
                <li> Happiness is a butterfly </li>
                <li> hope is a dangerous thing for a woman like me to have - but I have it </li>
            </ol>
        </div>
        <div class="cms-content2">
            <h4>LP</h4>
            <ol>
                <li> Norman fucking Rockwell </li>
                <li> Mariners Apartment Complex </li>
                <li> Venice Bitch </li>
                <li> Fuck it I love you </li>
                <li> Doin' Time </li>
                <li> Love song </li>
                <li> Cinnamon Girl </li>
                <li> How to disappear </li>
                <li> California </li>
                <li> The Next Best American Record </li>
                <li> The greatest </li>
                <li> Bartender </li>
                <li> Happiness is a butterfly </li>
                <li> hope is a dangerous thing for a woman like me to have - but I have it </li>
            </ol>
        </div>
    </div>
</body>

Tested on XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <xsl:value-of select="//div[@id='tab3']//div[@class='cms-content']/child::*"/>
    </xsl:template>
</xsl:transform>

OUTPUT

LP 
                 Norman fucking Rockwell 
                 Mariners Apartment Complex 
                 Venice Bitch 
                 Fuck it I love you 
                 Doin' Time 
                 Love song 
                 Cinnamon Girl 
                 How to disappear 
                 California 
                 The Next Best American Record 
                 The greatest 
                 Bartender 
                 Happiness is a butterfly 
                 hope is a dangerous thing for a woman like me to have - but I have it

Refer the link: http://xsltransform.net/nbiCsZn

作者: Amrendra Kumar 发布者: 2019 年 8 月 13 日
32x32