在元素下选择一些文本

时间:2019-02-13 16:43:31

标签: html xml xpath

我正在尝试选择下表中的文本

<section id="bullet_features">
<h2>Additional Features</h2>
<p>
Knife Steel: 8Cr13MoV, satin finish
<br>
Handle: Molded co-polymer
<br>
Blade: 3.6 in.
<br>
Overall: 8 in.
</p>
</section>

我能够抓取整个“ bullet_features”框,但我想一次专门抓取一行。例如,我想要一个xpath表达式指向:

Knife Steel: 8Cr13MoV, satin finish

另一个指向:

Handle: Molded co-polymer

我当前的表情:

//*[@id="bullet_features"]

尽力而为,我已经尝试过following-sibling/text(),但在我的情况下却不起作用。

1 个答案:

答案 0 :(得分:0)

此XPath,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct contact {                // Data structure that holds contact information 
    char FirstName[10];         // Array for first name
    char LastName[10];          // Array for last name
    int PhoneNum;               // Phone number
};

int main() {

    // Setting up variables
    int Function = 0;
    int Choice = 0;
    char FName[200][10];
    char LName[200][10];
    int PNum = 0;
    int n = 1;
    int size = 200;
    struct contact *con = (struct contact *)malloc(size * sizeof(struct contact));
    int b = 0, a = 0;

    do {     // Will loop through the main function until the user decides to exit the program

        // Prints out the main menu of the phone book
        printf("\nPhone Book");
        printf("\n[1] Add a contact");
        printf("\n[2] Delete a contact");
        printf("\n[3] Show contacts");
        printf("\n[4] Exit program");
        printf("\n\nWhat function would you like to use?\n");       // Asks for user input
        scanf("%d", &Choice);

        switch (Choice) {
        case 1:     // Case to add a contact into the phone book
            printf("\nYou chose to add a contact.");
            printf("\nFirst name: ");
            scanf("%s", &FName[b]);
            printf("\nLast name: ");
            scanf("%s", &LName[b]);
            printf("\nPhone number (Numbers only): ");
            scanf("%d", &PNum);
            printf("\nRecord added to the phone book");

            // Records the information given into the structure
            if (n >= size)
            {
                size = size * 2;
                con = (struct contact*)realloc(con, size * sizeof(struct contact));
            }
            a = (n - 1);

            printf("\na is %d", a);

            strcpy(con[a].FirstName, FName[b]);
            strcpy(con[a].LastName, LName[b]);
            con[a].PhoneNum = PNum;

            b = (b + 1);
            n++;

            printf("\nn is %d", n);

            // Prints out the given information
            printf("\nNew contact:");
            printf("\nFirst name: %s", con[a].FirstName);
            printf("\nLast name: %s", con[a].LastName);
            printf("\nPhone number: %d", con[a].PhoneNum);
            printf("\n");
            break;
        case 2:     // Case to delete a contact from the phone book
            printf("\nYou chose to delete a contact.");
            break;
        case 3:     // Case to see all of the entered contacts
            printf("\nYou chose to show the contacts.");
            for (a = 0; a < (n - 1); a++) {
                printf("\nContact #%d", a);
                printf("\nFirst name: %s", con[a].FirstName);
                printf("\nLast name: %s", con[a].LastName);
                printf("\nPhone number: %d", con[a].PhoneNum);
                printf("\n");
            }
            break;
        case 4:
            printf("Goodbye!");
            break;
        }
    } while (Choice != 4);
    return 0;
}

将选择

//*[@id="bullet_features"]/p/text()[1]

和这个XPath,

Knife Steel: 8Cr13MoV, satin finish

将选择

//*[@id="bullet_features"]/p/text()[2]

根据要求。