/** * Replace constant expressions in given AVT * * @param DOMAttr $attribute * @return void */ protected function replaceAVT(DOMAttr $attribute) { AVTHelper::replace($attribute, function ($token) { if ($token[0] === 'expression') { $token[1] = $this->evaluateExpression($token[1]); } return $token; }); }
/** * @testdox replace() tests * @dataProvider getReplaceTests */ public function testReplace($xml, $callback, $expected) { if ($expected instanceof Exception) { $this->setExpectedException(get_class($expected), $expected->getMessage()); } $dom = new DOMDocument(); $dom->loadXML($xml); AVTHelper::replace($dom->documentElement->getAttributeNode('x'), $callback); $this->assertSame($expected, $dom->saveXML($dom->documentElement)); }
/** * Replace an expression with a literal value in given attribute * * @param DOMAttr $attribute * @param string $expr * @param string $value * @return void */ protected function replaceAttribute(DOMAttr $attribute, $expr, $value) { AVTHelper::replace($attribute, function ($token) use($expr, $value) { // Test whether this expression is the one we're looking for if ($token[0] === 'expression' && $token[1] === $expr) { // Replace the expression with the value (as a literal) $token = ['literal', $value]; } return $token; }); }
/** * Remove extraneous space in XPath expressions used in XSL elements * * @param DOMElement $template <xsl:template/> node * @return void */ public function normalize(DOMElement $template) { $xpath = new DOMXPath($template->ownerDocument); // Get all the "match", "select" and "test" attributes of XSL elements, whose value contains // a space $query = '//xsl:*/@*[contains(., " ")][contains("matchselectest", name())]'; foreach ($xpath->query($query) as $attribute) { $attribute->parentNode->setAttribute($attribute->nodeName, XPathHelper::minify($attribute->nodeValue)); } // Get all the attributes of non-XSL elements, whose value contains a space $query = '//*[namespace-uri() != "' . self::XMLNS_XSL . '"]' . '/@*[contains(., " ")]'; foreach ($xpath->query($query) as $attribute) { AVTHelper::replace($attribute, function ($token) { if ($token[0] === 'expression') { $token[1] = XPathHelper::minify($token[1]); } return $token; }); } }
/** * Replace xsl:value nodes that contain a literal with a Text node * * @param DOMElement $template <xsl:template/> node * @return void */ public function normalize(DOMElement $template) { $xpath = new DOMXPath($template->ownerDocument); foreach ($xpath->query('//xsl:value-of') as $valueOf) { $textContent = $this->getTextContent($valueOf->getAttribute('select')); if ($textContent !== false) { $this->replaceElement($valueOf, $textContent); } } $query = '//*[namespace-uri() != "' . self::XMLNS_XSL . '"]' . '/@*[contains(., "{")]'; foreach ($xpath->query($query) as $attribute) { AVTHelper::replace($attribute, function ($token) { if ($token[0] === 'expression') { $textContent = $this->getTextContent($token[1]); if ($textContent !== false) { // Turn this token into a literal $token = ['literal', $textContent]; } } return $token; }); } }