Skip to content

Commit

Permalink
Add attr() method
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Feb 18, 2014
1 parent 7bd9d48 commit c2fd16a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ function text($path) {
return trim((string) $r[0]);
}

/*
* Convenience method for getting an attribute of a node
*/
function attr($attribute) {
return trim((string) $this->el->attributes()->{$attribute});
}

function first($path) {
# Convenience method
$x = $this->xpath($path);
Expand Down
16 changes: 16 additions & 0 deletions tests/QuiteSimpleXMLElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ public function testGetName() {
$this->assertEquals('CheckOutItemResponse', $node->getName());
}

public function testAttr() {
$xml = '
<sear:SEGMENTS xmlns:sear="http://www.exlibrisgroup.com/xsd/jaguar/search">
<sear:FACETLIST ACCURATE_COUNTERS="true">
<sear:FACET NAME="creator" COUNT="200">
Test
</sear:FACET>
</sear:FACETLIST>
</sear:SEGMENTS>';
$root = new QuiteSimpleXMLElement($xml);
$node = $root->first('/sear:SEGMENTS/sear:FACETLIST/sear:FACET');

$this->assertEquals('creator', $node->attr('NAME'));
$this->assertEquals('200', $node->attr('COUNT'));
$this->assertEquals('', $node->attr('SOMEETHING_ELSE'));
}
public function testGetNamespaces() {
$xml = '
<ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip">
Expand Down

0 comments on commit c2fd16a

Please sign in to comment.