diff --git a/lib/SAML2/Assertion.php b/lib/SAML2/Assertion.php
index f5b670cc0a17977002103cb2899c5be33ec43005..6bbe5621f0f7188fe00fbaa361223cdc3f567f73 100644
--- a/lib/SAML2/Assertion.php
+++ b/lib/SAML2/Assertion.php
@@ -338,10 +338,7 @@ class SAML2_Assertion implements SAML2_SignedElement {
 			}
 			switch ($node->localName) {
 			case 'AudienceRestriction':
-				$audiences = SAML2_Utils::xpQuery($node, './saml_assertion:Audience');
-				foreach ($audiences as &$audience) {
-					$audience = trim($audience->textContent);
-				}
+				$audiences = SAML2_Utils::extractStrings($node, './saml_assertion:Audience');
 				if ($this->validAudiences === NULL) {
 					/* The first (and probably last) AudienceRestriction element. */
 					$this->validAudiences = $audiences;
diff --git a/lib/SAML2/Utils.php b/lib/SAML2/Utils.php
index 45bd0bbc4bf428f6fa6d51d27cd9f9a527931674..8cf84426a599df5ed1bbe326d91d08d33f18ff04 100644
--- a/lib/SAML2/Utils.php
+++ b/lib/SAML2/Utils.php
@@ -348,4 +348,23 @@ class SAML2_Utils {
 		return $ret;
 	}
 
+
+	/**
+	 * Extract strings from a set of nodes.
+	 *
+	 * @param DOMElement $parent  The element we should rund the XPath query on.
+	 * @param string $query  The XPath query we should use to retrieve the nodes.
+	 * @return array  The string values of the various nodes.
+	 */
+	public static function extractStrings(DOMElement $parent, $query) {
+		assert('is_string($query)');
+
+		$ret = array();
+		foreach (self::xpQuery($parent, $query) as $node) {
+			$ret[] = trim($node->textContent);
+		}
+
+		return $ret;
+	}
+
 }