1

I have my projects.xml file as below:

<projects>
    <project>
        <id>ID_ABCD1</id>
        <name>ptest1</name>
    </project>
    <project>
        <id>IDB_abcd</id>
        <name>ptest1</name>
        <branch>bnm1</branch>
        <branched_from_id>ID_ABCD1</branched_from_id>
        <branched_from_version>111</branched_from_version>
    </project>
    <project>
        <id>ID_ABCD2</id>
        <name>ptest2</name>
    </project>
    <project>
        <id>IDB_abcd2</id>
        <name>ptest2</name>
        <branch>bnm2</branch>
        <branched_from_id>ID_ABCD2</branched_from_id>
        <branched_from_version>111</branched_from_version>
    </project>
</projects>

In my existing code I am getting project names based on project ID obtained from another file categories.xml.

For each of these project names I have to search is there any branch element for it, and if the element exists then I have to get the corresponding branch name and branched_from_version.

branched_from_id value is the ID of some other project.

#!/usr/bin/env perl

use strict;

use XML::LibXML;
use File::Basename;
use File::Find::Rule;
use XML::Twig;

my $cat_xml  = '/home/Ras/categories.xml';
my $twig     = XML::Twig->parse( $cat_xml );
my $doc      = XML::LibXML->new->parse_file( $cat_xml );

my $proj_xml = '/home/Ras/projects.xml';
my $twigp    = XML::LibXML->new->parse_file( $proj_xml );

my @nodes    = $doc->findnodes( '/categories/category' );

my $catname  = "/home/Ras/category.txt";

open FH, "> $catname" or warn "not able to open catname file";

foreach my $catnode ( @nodes ) {
    my @catn = $catnode->findvalue( '@name' );
    print FH "@catn \n";
}

my @catlist = `cat /home/Ras/category.txt`;

foreach my $cat ( @catlist ) {

    chomp( $cat );
    $cat =~ s/^\s+|\s$//g;

    my $path = "./category[\@name=\"$cat\"]/project";

    foreach my $project ( $twig->findnodes( "$path" ) ) {

        my $projout = $project->text;

        foreach my $projname ( $twigp->findvalue( "/projects/project[id = '$projout']/name" ) ) {

        }
    }
}

In above code $projout contain the ID value of my project.xml file. But those ID value with branch node are not there.

I have name value in $projname. So based on the name value in $projname I need to get the corresponding branch value and branched_from_version value.

3
  • 1
    Why are you mixing XML::Twig and XML::LibXML? That looks like a road to pain. Commented Dec 2, 2016 at 9:54
  • Can you clarify what the expected inputs and outputs are? Perhaps a minimal reproducible example that we can run, with what the desired output would be? Commented Dec 2, 2016 at 9:56
  • This is a bit of a mess. You create a list of category names from categories.xml and write it to disk; then you read it back and search the same xml file for those category elements! You will need to show categories.xml as well if you want help with this. Commented Dec 2, 2016 at 11:03

1 Answer 1

1

It's a bit simplified, but I think you're trying to do something like this:

#!/usr/bin/env perl
use warnings;
use strict;

use XML::Twig;

my $twig = XML::Twig -> parsefile ( 'test.xml' );
my $project = 'ptest2'; 

if ( $twig -> get_xpath("//project/name[string()=\"$project\"]" ) ) {
    print $twig -> get_xpath("//project/name[string()=\"$project\"]",0 ) -> text, " exists\n";
    foreach my $branch ( $twig -> get_xpath("//project/name[string()=\"$project\"]/../branched_from_id/..",0 ) ) {
       print "$project : ", $branch -> first_child_text('branched_from_id'), " => ", $branch -> first_child_text('branched_from_version'),"\n";
    }
}

I've had to cut it down, because I don't have your projects list. But this will use XML::Twigs get_xpath to find a project name - although note, it doesn't differentiate whether it's a branch or not for the first part of the check.

And then pulls out an xpath match based on this of all the branches that 'match'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.