A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label xpath. Show all posts
Showing posts with label xpath. Show all posts

Sample XMLUnit test class

import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import java.util.HashMap;
import org.custommonkey.xmlunit.*;

public class MyTest extends XMLTestCase {
    private HttpClient client;
    private String pageTitle = "something";
  
    protected void setUp() {
         client = new HttpClient();        
        // declare XML namespaces
        HashMap m = new HashMap();
        m.put("agg", "http://www.bbc.co.uk/aggregation/1.0");
        m.put("atom", "http://www.w3.org/2005/Atom");
        NamespaceContext ctx = new SimpleNamespaceContext(m);
        XMLUnit.setXpathNamespaceContext(ctx);
    }

    public void testVersion() throws Exception {
        String pageXML = getXML("http://example.com/version");
        assertXpathEvaluatesTo("2010-07-29", "/version/release-date", pageXML);
    }
  
    public void testTitle() throws Exception {
        String pageXML = getXML("http://example.com/test");
        assertXpathEvaluatesTo("items about "+pageTitle, "/agg:page//atom:feed/atom:title[1]", pageXML);
    }
  
     private String getXML(String uri) throws IOException {
        GetMethod method = new GetMethod(uri);
        int statusCode = client.executeMethod(method);
        assertEquals("HTTP GET " + uri, HttpStatus.SC_OK, statusCode); // 200 OK
        return method.getResponseBodyAsString();
     }

     public void TearDown() {
     }

}

Testing XML with Java

Testing
  • XMLUnit
  • JUnit - most popular

XML

with Java
  • Selenium
  • Eclipse
  • Maven + Jetty

Validating with XML::XPath

use XML::XPath; # update: use XML::LibXML and XML::LibXML::XPathContext instead
use Test::More qw(no_plan);

my $id = 298;
my $xml = &get_url("/$id")->{xml};
my $xp = XML::XPath->new( xml => $xml );
my $r = '/feed'; # root
ok( $xp->exists($r), "there should be a feed" );
ok( $xp->exists("$r/id"), "there should be a feed ID" );
ok( $xp->find("$r/id") =~ m{/$id}, "feed ID should be correct" );
ok( $xp->exists("$r/link[\@rel=\"self\"]"), "rel=self attr is correct" );
ok( ($xp->find("$r/link")->get_nodelist)[0]->getAttribute('href'), "href link exists" ); # this is ugly
check( $xp->find("$r/updated") => like => qr/^\d{4}\-\d\d\-\d\dT\d\d:\d\d:\d\d\.\d+Z$/ );

my $entries = $xp->find("/feed/entry"); # find all entry sections
ok( $entries, "there are entries" );
if ($entries->size) {
my $e = "/entry";
foreach my $node ($entries->get_nodelist) {
# it's bloody stupid transforming the XML::XPath object to a string and then parsing back to an object
# instead maybe loop through each node in the list using xpath [0] [1], etc?
# But why should I have to hack around this....
my $entry = XML::XPath->new( xml => $node->toString );
ok( $entry->exists("$e/title"), "title exists" );
}
}

# Conclusion: XML::XPath is bad for writing tests.

Parents node's position in XSL

1. Count parents using <xsl:number>
Number can be extracted:
($n)=$c=~/(\d+)\.\d+\.\d+\.\d+$/;
and adjusted:
$n=((($n/2)-5)+1)

<xsl:number count="." level="multiple" from="/page" format="1"/>

Or with format="a", interpret like j=1, l=2, n=3, p=4, etc.

2. Count parents using position()
Simpler -- but apparently this is a weird way to do it. Probably inefficient too.

<xsl:value-of select="count(parent::*/parent::*/parent::*/preceding-sibling::*) - 3"/>

PHP 5 XML

SimpleXML
http://www.php.net/manual/en/ref.simplexml.php

$xml = simplexml_load_file($file);

$body = $xml->xpath("//body");

print $body->asXML();