Package lxml :: Package tests :: Module test_xslt
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_xslt

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  Test cases related to XSLT processing 
  5  """ 
  6   
  7  import unittest 
  8   
  9  from common_imports import etree, StringIO, HelperTestCase, fileInTestDir 
 10  from common_imports import doctest 
 11   
12 -class ETreeXSLTTestCase(HelperTestCase):
13 """XPath tests etree""" 14
15 - def test_xslt(self):
16 tree = self.parse('<a><b>B</b><c>C</c></a>') 17 style = self.parse('''\ 18 <xsl:stylesheet version="1.0" 19 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 20 <xsl:template match="*" /> 21 <xsl:template match="/"> 22 <foo><xsl:value-of select="/a/b/text()" /></foo> 23 </xsl:template> 24 </xsl:stylesheet>''') 25 26 st = etree.XSLT(style) 27 res = st.apply(tree) 28 self.assertEquals('''\ 29 <?xml version="1.0"?> 30 <foo>B</foo> 31 ''', 32 st.tostring(res))
33
35 self.assertRaises(ValueError, etree.XSLT, etree.ElementTree())
36
37 - def test_xslt_input_none(self):
38 self.assertRaises(TypeError, etree.XSLT, None)
39
40 - def test_xslt_utf8(self):
41 tree = self.parse(u'<a><b>\uF8D2</b><c>\uF8D2</c></a>') 42 style = self.parse('''\ 43 <xsl:stylesheet version="1.0" 44 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 45 <xsl:output encoding="UTF-8"/> 46 <xsl:template match="/"> 47 <foo><xsl:value-of select="/a/b/text()" /></foo> 48 </xsl:template> 49 </xsl:stylesheet>''') 50 51 st = etree.XSLT(style) 52 res = st.apply(tree) 53 expected = u'''\ 54 <?xml version="1.0" encoding="UTF-8"?> 55 <foo>\uF8D2</foo> 56 ''' 57 self.assertEquals(expected, 58 unicode(str(res), 'UTF-8'))
59
60 - def test_xslt_encoding(self):
61 tree = self.parse(u'<a><b>\uF8D2</b><c>\uF8D2</c></a>') 62 style = self.parse('''\ 63 <xsl:stylesheet version="1.0" 64 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 65 <xsl:output encoding="UTF-16"/> 66 <xsl:template match="/"> 67 <foo><xsl:value-of select="/a/b/text()" /></foo> 68 </xsl:template> 69 </xsl:stylesheet>''') 70 71 st = etree.XSLT(style) 72 res = st.apply(tree) 73 expected = u'''\ 74 <?xml version="1.0" encoding="UTF-16"?> 75 <foo>\uF8D2</foo> 76 ''' 77 self.assertEquals(expected, 78 unicode(str(res), 'UTF-16'))
79
81 tree = self.parse(u'<a><b>\uF8D2</b><c>\uF8D2</c></a>') 82 style = self.parse('''\ 83 <xsl:stylesheet version="1.0" 84 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 85 <xsl:output encoding="UTF-8"/> 86 <xsl:template match="/"> 87 <foo><xsl:value-of select="/a/b/text()" /></foo> 88 </xsl:template> 89 </xsl:stylesheet>''') 90 91 st = etree.XSLT(style) 92 res = st.apply(tree) 93 expected = u"""\ 94 <?xml version='1.0' encoding='UTF-16'?> 95 <foo>\uF8D2</foo>""" 96 97 f = StringIO() 98 res.write(f, 'UTF-16') 99 result = unicode(f.getvalue(), 'UTF-16') 100 self.assertEquals(expected, 101 result)
102
103 - def test_xslt_unicode(self):
104 tree = self.parse(u'<a><b>\uF8D2</b><c>\uF8D2</c></a>') 105 style = self.parse('''\ 106 <xsl:stylesheet version="1.0" 107 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 108 <xsl:output encoding="UTF-16"/> 109 <xsl:template match="/"> 110 <foo><xsl:value-of select="/a/b/text()" /></foo> 111 </xsl:template> 112 </xsl:stylesheet>''') 113 114 st = etree.XSLT(style) 115 res = st.apply(tree) 116 expected = u'''\ 117 <?xml version="1.0"?> 118 <foo>\uF8D2</foo> 119 ''' 120 self.assertEquals(expected, 121 unicode(res))
122
123 - def test_exslt(self):
124 tree = self.parse('<a><b>B</b><c>C</c></a>') 125 style = self.parse('''\ 126 <xsl:stylesheet version="1.0" 127 xmlns:math="http://exslt.org/math" 128 xmlns:str="http://exslt.org/strings" 129 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 130 exclude-result-prefixes="math str xsl"> 131 <xsl:template match="text()"> 132 <xsl:value-of select="str:align(string(.), '---', 'center')" /> 133 </xsl:template> 134 <xsl:template match="*"> 135 <xsl:copy> 136 <xsl:attribute name="pi"> 137 <xsl:value-of select="math:constant('PI', count(*)+2)"/> 138 </xsl:attribute> 139 <xsl:apply-templates/> 140 </xsl:copy> 141 </xsl:template> 142 </xsl:stylesheet>''') 143 144 st = etree.XSLT(style) 145 res = st(tree) 146 self.assertEquals('''\ 147 <?xml version="1.0"?> 148 <a pi="3.14"><b pi="3">-B-</b><c pi="3">-C-</c></a> 149 ''', 150 st.tostring(res))
151
152 - def test_xslt_input(self):
153 tree = self.parse('<a><b>B</b><c>C</c></a>') 154 style = self.parse('''\ 155 <xsl:stylesheet version="1.0" 156 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 157 <xsl:template match="*" /> 158 <xsl:template match="/"> 159 <foo><xsl:value-of select="/a/b/text()" /></foo> 160 </xsl:template> 161 </xsl:stylesheet>''') 162 163 st = etree.XSLT(style) 164 st = etree.XSLT(style.getroot()) 165 self.assertRaises(TypeError, etree.XSLT, None)
166
168 style = self.parse('''\ 169 <otherroot> 170 <xsl:stylesheet version="1.0" 171 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 172 <xsl:template match="*" /> 173 <xsl:template match="/"> 174 <foo><xsl:value-of select="/a/b/text()" /></foo> 175 </xsl:template> 176 </xsl:stylesheet> 177 </otherroot>''') 178 179 self.assertRaises(etree.XSLTParseError, etree.XSLT, style) 180 root_node = style.getroot() 181 self.assertRaises(etree.XSLTParseError, etree.XSLT, root_node) 182 st = etree.XSLT(root_node[0])
183
184 - def test_xslt_broken(self):
185 tree = self.parse('<a/>') 186 style = self.parse('''\ 187 <xsl:stylesheet version="1.0" 188 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 189 <xsl:foo /> 190 </xsl:stylesheet>''') 191 self.assertRaises(etree.XSLTParseError, 192 etree.XSLT, style)
193
194 - def test_xslt_parameters(self):
195 tree = self.parse('<a><b>B</b><c>C</c></a>') 196 style = self.parse('''\ 197 <xsl:stylesheet version="1.0" 198 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 199 <xsl:template match="/"> 200 <foo><xsl:value-of select="$bar" /></foo> 201 </xsl:template> 202 </xsl:stylesheet>''') 203 204 st = etree.XSLT(style) 205 res = st.apply(tree, bar="'Bar'") 206 self.assertEquals('''\ 207 <?xml version="1.0"?> 208 <foo>Bar</foo> 209 ''', 210 st.tostring(res))
211
213 # DISABLED - NOT RELIABLE? 214 # apply() without needed parameter will lead to XSLTApplyError 215 tree = self.parse('<a><b>B</b><c>C</c></a>') 216 style = self.parse('''\ 217 <xsl:stylesheet version="1.0" 218 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 219 <xsl:template match="/"> 220 <foo><xsl:value-of select="$bar" /></foo> 221 </xsl:template> 222 </xsl:stylesheet>''') 223 224 st = etree.XSLT(style) 225 self.assertRaises(etree.XSLTApplyError, 226 st.apply, tree)
227
229 tree = self.parse('<a><b>B</b><c>C</c></a>') 230 style = self.parse('''\ 231 <xsl:stylesheet version="1.0" 232 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 233 <xsl:template match="*" /> 234 <xsl:template match="/"> 235 <foo><xsl:value-of select="$bar" /></foo> 236 <foo><xsl:value-of select="$baz" /></foo> 237 </xsl:template> 238 </xsl:stylesheet>''') 239 240 st = etree.XSLT(style) 241 res = st.apply(tree, bar="'Bar'", baz="'Baz'") 242 self.assertEquals('''\ 243 <?xml version="1.0"?> 244 <foo>Bar</foo><foo>Baz</foo> 245 ''', 246 st.tostring(res))
247
249 tree = self.parse('<a><b>B</b><c>C</c></a>') 250 style = self.parse('''\ 251 <xsl:stylesheet version="1.0" 252 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 253 <xsl:template match="*" /> 254 <xsl:template match="/"> 255 <foo><xsl:value-of select="$bar" /></foo> 256 </xsl:template> 257 </xsl:stylesheet>''') 258 259 st = etree.XSLT(style) 260 res = st.apply(tree, bar="/a/b/text()") 261 self.assertEquals('''\ 262 <?xml version="1.0"?> 263 <foo>B</foo> 264 ''', 265 st.tostring(res))
266 267
269 tree = self.parse('<a><b>B</b><c>C</c></a>') 270 style = self.parse('''\ 271 <xsl:stylesheet version="1.0" 272 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 273 <xsl:param name="bar" select="'Default'" /> 274 <xsl:template match="*" /> 275 <xsl:template match="/"> 276 <foo><xsl:value-of select="$bar" /></foo> 277 </xsl:template> 278 </xsl:stylesheet>''') 279 280 st = etree.XSLT(style) 281 res = st.apply(tree, bar="'Bar'") 282 self.assertEquals('''\ 283 <?xml version="1.0"?> 284 <foo>Bar</foo> 285 ''', 286 st.tostring(res)) 287 res = st.apply(tree) 288 self.assertEquals('''\ 289 <?xml version="1.0"?> 290 <foo>Default</foo> 291 ''', 292 st.tostring(res))
293
294 - def test_xslt_html_output(self):
295 tree = self.parse('<a><b>B</b><c>C</c></a>') 296 style = self.parse('''\ 297 <xsl:stylesheet version="1.0" 298 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 299 <xsl:output method="html"/> 300 <xsl:strip-space elements="*"/> 301 <xsl:template match="/"> 302 <html><body><xsl:value-of select="/a/b/text()" /></body></html> 303 </xsl:template> 304 </xsl:stylesheet>''') 305 306 st = etree.XSLT(style) 307 res = st(tree) 308 self.assertEquals('''<html><body>B</body></html>''', 309 str(res).strip())
310
311 - def test_xslt_include(self):
312 tree = etree.parse(fileInTestDir('test1.xslt')) 313 st = etree.XSLT(tree)
314
316 f = open(fileInTestDir('test1.xslt'), 'r') 317 tree = etree.parse(f) 318 f.close() 319 st = etree.XSLT(tree)
320
322 xml = '<a/>' 323 xslt = '''\ 324 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 325 <xsl:template match="/"> 326 <response>Some text</response> 327 </xsl:template> 328 </xsl:stylesheet> 329 ''' 330 source = self.parse(xml) 331 styledoc = self.parse(xslt) 332 style = etree.XSLT(styledoc) 333 result = style.apply(source) 334 335 etree.tostring(result.getroot()) 336 337 source = self.parse(xml) 338 styledoc = self.parse(xslt) 339 style = etree.XSLT(styledoc) 340 result = style.apply(source) 341 342 etree.tostring(result.getroot())
343
345 xml = '<a/>' 346 xslt = '''\ 347 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 348 <xsl:template match="/"> 349 <response>Some text</response> 350 </xsl:template> 351 </xsl:stylesheet> 352 ''' 353 source = self.parse(xml) 354 styledoc = self.parse(xslt) 355 transform = etree.XSLT(styledoc) 356 result = transform.apply(source) 357 result = transform.apply(source) 358 etree.tostring(result.getroot()) 359 result = transform.apply(source) 360 etree.tostring(result.getroot()) 361 str(result) 362 363 result1 = transform(source) 364 result2 = transform(source) 365 self.assertEquals(str(result1), str(result2)) 366 result = transform(source) 367 str(result)
368
369 - def test_xslt_empty(self):
370 # could segfault if result contains "empty document" 371 xml = '<blah/>' 372 xslt = ''' 373 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 374 <xsl:template match="/" /> 375 </xsl:stylesheet> 376 ''' 377 378 source = self.parse(xml) 379 styledoc = self.parse(xslt) 380 style = etree.XSLT(styledoc) 381 result = style.apply(source) 382 self.assertEqual('', style.tostring(result)) 383 self.assertEqual('', str(result))
384
385 - def test_xslt_message(self):
386 xml = '<blah/>' 387 xslt = ''' 388 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 389 <xsl:template match="/"> 390 <xsl:message>TEST TEST TEST</xsl:message> 391 </xsl:template> 392 </xsl:stylesheet> 393 ''' 394 395 source = self.parse(xml) 396 styledoc = self.parse(xslt) 397 style = etree.XSLT(styledoc) 398 result = style.apply(source) 399 self.assertEqual('', style.tostring(result)) 400 self.assertEqual('', str(result)) 401 self.assert_("TEST TEST TEST" in [entry.message 402 for entry in style.error_log])
403
405 xml = '<blah/>' 406 xslt = ''' 407 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 408 <xsl:template match="/"> 409 <xsl:message terminate="yes">TEST TEST TEST</xsl:message> 410 </xsl:template> 411 </xsl:stylesheet> 412 ''' 413 414 source = self.parse(xml) 415 styledoc = self.parse(xslt) 416 style = etree.XSLT(styledoc) 417 result = style.apply(source) 418 self.assertEqual('', style.tostring(result)) 419 self.assertEqual('', str(result)) 420 self.assert_("TEST TEST TEST" in [entry.message 421 for entry in style.error_log])
422
423 - def test_xslt_shortcut(self):
424 tree = self.parse('<a><b>B</b><c>C</c></a>') 425 style = self.parse('''\ 426 <xsl:stylesheet version="1.0" 427 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 428 <xsl:template match="*" /> 429 <xsl:template match="/"> 430 <doc> 431 <foo><xsl:value-of select="$bar" /></foo> 432 <foo><xsl:value-of select="$baz" /></foo> 433 </doc> 434 </xsl:template> 435 </xsl:stylesheet>''') 436 437 result = tree.xslt(style, bar="'Bar'", baz="'Baz'") 438 self.assertEquals( 439 '<doc><foo>Bar</foo><foo>Baz</foo></doc>', 440 etree.tostring(result.getroot()))
441
443 tree = self.parse('<a><b>B</b><c>C</c></a>') 444 style = self.parse('''\ 445 <xsl:stylesheet version="1.0" 446 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 447 <xsl:template match="a"><A><xsl:apply-templates/></A></xsl:template> 448 <xsl:template match="b"><B><xsl:apply-templates/></B></xsl:template> 449 <xsl:template match="c"><C><xsl:apply-templates/></C></xsl:template> 450 </xsl:stylesheet>''') 451 452 self.assertEquals(self._rootstring(tree), 453 '<a><b>B</b><c>C</c></a>') 454 result = tree.xslt(style) 455 self.assertEquals(self._rootstring(tree), 456 '<a><b>B</b><c>C</c></a>') 457 self.assertEquals(self._rootstring(result), 458 '<A><B>B</B><C>C</C></A>') 459 460 b_tree = etree.ElementTree(tree.getroot()[0]) 461 self.assertEquals(self._rootstring(b_tree), 462 '<b>B</b>') 463 result = b_tree.xslt(style) 464 self.assertEquals(self._rootstring(tree), 465 '<a><b>B</b><c>C</c></a>') 466 self.assertEquals(self._rootstring(result), 467 '<B>B</B>') 468 469 c_tree = etree.ElementTree(tree.getroot()[1]) 470 self.assertEquals(self._rootstring(c_tree), 471 '<c>C</c>') 472 result = c_tree.xslt(style) 473 self.assertEquals(self._rootstring(tree), 474 '<a><b>B</b><c>C</c></a>') 475 self.assertEquals(self._rootstring(result), 476 '<C>C</C>')
477
478 - def test_extensions1(self):
479 tree = self.parse('<a><b>B</b></a>') 480 style = self.parse('''\ 481 <xsl:stylesheet version="1.0" 482 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 483 xmlns:myns="testns" 484 exclude-result-prefixes="myns"> 485 <xsl:template match="a"><A><xsl:value-of select="myns:mytext(b)"/></A></xsl:template> 486 </xsl:stylesheet>''') 487 488 def mytext(ctxt, values): 489 return 'X' * len(values)
490 491 result = tree.xslt(style, {('testns', 'mytext') : mytext}) 492 self.assertEquals(self._rootstring(result), 493 '<A>X</A>')
494
495 - def test_extensions2(self):
496 tree = self.parse('<a><b>B</b></a>') 497 style = self.parse('''\ 498 <xsl:stylesheet version="1.0" 499 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 500 xmlns:myns="testns" 501 exclude-result-prefixes="myns"> 502 <xsl:template match="a"><A><xsl:value-of select="myns:mytext(b)"/></A></xsl:template> 503 </xsl:stylesheet>''') 504 505 def mytext(ctxt, values): 506 return 'X' * len(values)
507 508 namespace = etree.FunctionNamespace('testns') 509 namespace['mytext'] = mytext 510 511 result = tree.xslt(style) 512 self.assertEquals(self._rootstring(result), 513 '<A>X</A>') 514
515 - def test_xslt_document_XML(self):
516 # make sure document('') works from parsed strings 517 xslt = etree.XSLT(etree.XML("""\ 518 <xsl:stylesheet version="1.0" 519 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 520 <xsl:template match="/"> 521 <test>TEXT<xsl:copy-of select="document('')//test"/></test> 522 </xsl:template> 523 </xsl:stylesheet> 524 """)) 525 result = xslt(etree.XML('<a/>')) 526 root = result.getroot() 527 self.assertEquals(root.tag, 528 'test') 529 self.assertEquals(root[0].tag, 530 'test') 531 self.assertEquals(root[0].text, 532 'TEXT') 533 self.assertEquals(root[0][0].tag, 534 '{http://www.w3.org/1999/XSL/Transform}copy-of')
535
536 - def test_xslt_document_parse(self):
537 # make sure document('') works from loaded files 538 xslt = etree.XSLT(etree.parse(fileInTestDir("test-document.xslt"))) 539 result = xslt(etree.XML('<a/>')) 540 root = result.getroot() 541 self.assertEquals(root.tag, 542 'test') 543 self.assertEquals(root[0].tag, 544 '{http://www.w3.org/1999/XSL/Transform}stylesheet')
545
546 - def test_xslt_document_elementtree(self):
547 # make sure document('') works from loaded files 548 xslt = etree.XSLT(etree.ElementTree(file=fileInTestDir("test-document.xslt"))) 549 result = xslt(etree.XML('<a/>')) 550 root = result.getroot() 551 self.assertEquals(root.tag, 552 'test') 553 self.assertEquals(root[0].tag, 554 '{http://www.w3.org/1999/XSL/Transform}stylesheet')
555
556 - def test_xslt_document_error(self):
557 # make sure document('') works from parsed strings 558 xslt = etree.XSLT(etree.XML("""\ 559 <xsl:stylesheet version="1.0" 560 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 561 <xsl:template match="/"> 562 <test>TEXT<xsl:copy-of select="document('uri:__junkfood__is__evil__')//test"/></test> 563 </xsl:template> 564 </xsl:stylesheet> 565 """)) 566 self.assertRaises(etree.XSLTApplyError, xslt, etree.XML('<a/>'))
567
568 - def test_xslt_move_result(self):
569 root = etree.XML('''\ 570 <transform> 571 <widget displayType="fieldset"/> 572 </transform>''') 573 574 xslt = etree.XSLT(etree.XML('''\ 575 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 576 <xsl:output method="html" indent="no"/> 577 <xsl:template match="/"> 578 <html> 579 <xsl:apply-templates/> 580 </html> 581 </xsl:template> 582 583 <xsl:template match="widget"> 584 <xsl:element name="{@displayType}"/> 585 </xsl:template> 586 587 </xsl:stylesheet>''')) 588 589 result = xslt(root[0]) 590 root[:] = result.getroot()[:] 591 del root # segfaulted before
592
593 - def test_xslt_pi(self):
594 tree = self.parse('''\ 595 <?xml version="1.0"?> 596 <?xml-stylesheet type="text/xsl" href="%s"?> 597 <a> 598 <b>B</b> 599 <c>C</c> 600 </a>''' % fileInTestDir("test1.xslt")) 601 602 style_root = tree.getroot().getprevious().parseXSL().getroot() 603 self.assertEquals("{http://www.w3.org/1999/XSL/Transform}stylesheet", 604 style_root.tag)
605
606 - def test_xslt_pi_embedded_xmlid(self):
607 # test xml:id dictionary lookup mechanism 608 tree = self.parse('''\ 609 <?xml version="1.0"?> 610 <?xml-stylesheet type="text/xsl" href="#style"?> 611 <a> 612 <b>B</b> 613 <c>C</c> 614 <xsl:stylesheet version="1.0" xml:id="style" 615 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 616 <xsl:template match="*" /> 617 <xsl:template match="/"> 618 <foo><xsl:value-of select="/a/b/text()" /></foo> 619 </xsl:template> 620 </xsl:stylesheet> 621 </a>''') 622 623 style_root = tree.getroot().getprevious().parseXSL().getroot() 624 self.assertEquals("{http://www.w3.org/1999/XSL/Transform}stylesheet", 625 style_root.tag) 626 627 st = etree.XSLT(style_root) 628 res = st.apply(tree) 629 self.assertEquals('''\ 630 <?xml version="1.0"?> 631 <foo>B</foo> 632 ''', 633 st.tostring(res))
634
635 - def test_xslt_pi_embedded_id(self):
636 # test XPath lookup mechanism 637 tree = self.parse('''\ 638 <?xml version="1.0"?> 639 <?xml-stylesheet type="text/xsl" href="#style"?> 640 <a> 641 <b>B</b> 642 <c>C</c> 643 </a>''') 644 645 style = self.parse('''\ 646 <xsl:stylesheet version="1.0" xml:id="style" 647 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 648 <xsl:template match="*" /> 649 <xsl:template match="/"> 650 <foo><xsl:value-of select="/a/b/text()" /></foo> 651 </xsl:template> 652 </xsl:stylesheet> 653 ''') 654 655 tree.getroot().append(style.getroot()) 656 657 style_root = tree.getroot().getprevious().parseXSL().getroot() 658 self.assertEquals("{http://www.w3.org/1999/XSL/Transform}stylesheet", 659 style_root.tag) 660 661 st = etree.XSLT(style_root) 662 res = st.apply(tree) 663 self.assertEquals('''\ 664 <?xml version="1.0"?> 665 <foo>B</foo> 666 ''', 667 st.tostring(res))
668
669 - def test_xslt_pi_get(self):
670 tree = self.parse('''\ 671 <?xml version="1.0"?> 672 <?xml-stylesheet type="text/xsl" href="TEST"?> 673 <a> 674 <b>B</b> 675 <c>C</c> 676 </a>''') 677 678 pi = tree.getroot().getprevious() 679 self.assertEquals("TEST", pi.get("href"))
680
681 - def test_xslt_pi_get_all(self):
682 tree = self.parse('''\ 683 <?xml version="1.0"?> 684 <?xml-stylesheet type="text/xsl" href="TEST"?> 685 <a> 686 <b>B</b> 687 <c>C</c> 688 </a>''') 689 690 pi = tree.getroot().getprevious() 691 self.assertEquals("TEST", pi.get("href")) 692 self.assertEquals("text/xsl", pi.get("type")) 693 self.assertEquals(None, pi.get("motz"))
694
695 - def test_xslt_pi_get_all_reversed(self):
696 tree = self.parse('''\ 697 <?xml version="1.0"?> 698 <?xml-stylesheet href="TEST" type="text/xsl"?> 699 <a> 700 <b>B</b> 701 <c>C</c> 702 </a>''') 703 704 pi = tree.getroot().getprevious() 705 self.assertEquals("TEST", pi.get("href")) 706 self.assertEquals("text/xsl", pi.get("type")) 707 self.assertEquals(None, pi.get("motz"))
708
709 - def test_xslt_pi_get_unknown(self):
710 tree = self.parse('''\ 711 <?xml version="1.0"?> 712 <?xml-stylesheet type="text/xsl" href="TEST"?> 713 <a> 714 <b>B</b> 715 <c>C</c> 716 </a>''') 717 718 pi = tree.getroot().getprevious() 719 self.assertEquals(None, pi.get("unknownattribute"))
720
721 - def test_xslt_pi_set_replace(self):
722 tree = self.parse('''\ 723 <?xml version="1.0"?> 724 <?xml-stylesheet type="text/xsl" href="TEST"?> 725 <a> 726 <b>B</b> 727 <c>C</c> 728 </a>''') 729 730 pi = tree.getroot().getprevious() 731 self.assertEquals("TEST", pi.get("href")) 732 733 pi.set("href", "TEST123") 734 self.assertEquals("TEST123", pi.get("href"))
735
736 - def test_xslt_pi_set_new(self):
737 tree = self.parse('''\ 738 <?xml version="1.0"?> 739 <?xml-stylesheet type="text/xsl"?> 740 <a> 741 <b>B</b> 742 <c>C</c> 743 </a>''') 744 745 pi = tree.getroot().getprevious() 746 self.assertEquals(None, pi.get("href")) 747 748 pi.set("href", "TEST") 749 self.assertEquals("TEST", pi.get("href"))
750
751 - def test_exslt_regexp_test(self):
752 xslt = etree.XSLT(etree.XML("""\ 753 <xsl:stylesheet version="1.0" 754 xmlns:regexp="http://exslt.org/regular-expressions" 755 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 756 <xsl:template match="*"> 757 <test><xsl:copy-of select="*[regexp:test(string(.), '8.')]"/></test> 758 </xsl:template> 759 </xsl:stylesheet> 760 """)) 761 result = xslt(etree.XML('<a><b>123</b><b>098</b><b>987</b></a>')) 762 root = result.getroot() 763 self.assertEquals(root.tag, 764 'test') 765 self.assertEquals(len(root), 1) 766 self.assertEquals(root[0].tag, 767 'b') 768 self.assertEquals(root[0].text, 769 '987')
770
771 - def test_exslt_regexp_replace(self):
772 xslt = etree.XSLT(etree.XML("""\ 773 <xsl:stylesheet version="1.0" 774 xmlns:regexp="http://exslt.org/regular-expressions" 775 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 776 <xsl:template match="*"> 777 <test> 778 <xsl:copy-of select="regexp:replace(string(.), 'd.', '', 'XX')"/> 779 <xsl:text>-</xsl:text> 780 <xsl:copy-of select="regexp:replace(string(.), 'd.', 'gi', 'XX')"/> 781 </test> 782 </xsl:template> 783 </xsl:stylesheet> 784 """)) 785 result = xslt(etree.XML('<a>abdCdEeDed</a>')) 786 root = result.getroot() 787 self.assertEquals(root.tag, 788 'test') 789 self.assertEquals(len(root), 0) 790 self.assertEquals(root.text, 'abXXdEeDed-abXXXXeXXd')
791
792 - def test_exslt_regexp_match(self):
793 xslt = etree.XSLT(etree.XML("""\ 794 <xsl:stylesheet version="1.0" 795 xmlns:regexp="http://exslt.org/regular-expressions" 796 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 797 <xsl:template match="*"> 798 <test> 799 <test1><xsl:copy-of select="regexp:match(string(.), 'd.')"/></test1> 800 <test2><xsl:copy-of select="regexp:match(string(.), 'd.', 'g')"/></test2> 801 <test2i><xsl:copy-of select="regexp:match(string(.), 'd.', 'gi')"/></test2i> 802 </test> 803 </xsl:template> 804 </xsl:stylesheet> 805 """)) 806 result = xslt(etree.XML('<a>abdCdEeDed</a>')) 807 root = result.getroot() 808 self.assertEquals(root.tag, 'test') 809 self.assertEquals(len(root), 3) 810 811 self.assertEquals(len(root[0]), 1) 812 self.assertEquals(root[0][0].tag, 'match') 813 self.assertEquals(root[0][0].text, 'dC') 814 815 self.assertEquals(len(root[1]), 2) 816 self.assertEquals(root[1][0].tag, 'match') 817 self.assertEquals(root[1][0].text, 'dC') 818 self.assertEquals(root[1][1].tag, 'match') 819 self.assertEquals(root[1][1].text, 'dE') 820 821 self.assertEquals(len(root[2]), 3) 822 self.assertEquals(root[2][0].tag, 'match') 823 self.assertEquals(root[2][0].text, 'dC') 824 self.assertEquals(root[2][1].tag, 'match') 825 self.assertEquals(root[2][1].text, 'dE') 826 self.assertEquals(root[2][2].tag, 'match') 827 self.assertEquals(root[2][2].text, 'De')
828
829 - def test_exslt_regexp_match_groups(self):
830 xslt = etree.XSLT(etree.XML("""\ 831 <xsl:stylesheet version="1.0" 832 xmlns:regexp="http://exslt.org/regular-expressions" 833 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 834 <xsl:template match="/"> 835 <test> 836 <xsl:for-each select="regexp:match( 837 '123abc567', '([0-9]+)([a-z]+)([0-9]+)' )"> 838 <test1><xsl:value-of select="."/></test1> 839 </xsl:for-each> 840 </test> 841 </xsl:template> 842 </xsl:stylesheet> 843 """)) 844 result = xslt(etree.XML('<a/>')) 845 root = result.getroot() 846 self.assertEquals(root.tag, 'test') 847 self.assertEquals(len(root), 4) 848 849 self.assertEquals(root[0].text, "123abc567") 850 self.assertEquals(root[1].text, "123") 851 self.assertEquals(root[2].text, "abc") 852 self.assertEquals(root[3].text, "567")
853
854 - def test_exslt_regexp_match1(self):
855 # taken from http://www.exslt.org/regexp/functions/match/index.html 856 xslt = etree.XSLT(etree.XML("""\ 857 <xsl:stylesheet version="1.0" 858 xmlns:regexp="http://exslt.org/regular-expressions" 859 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 860 <xsl:template match="/"> 861 <test> 862 <xsl:for-each select="regexp:match( 863 'http://www.bayes.co.uk/xml/index.xml?/xml/utils/rechecker.xml', 864 '(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)')"> 865 <test1><xsl:value-of select="."/></test1> 866 </xsl:for-each> 867 </test> 868 </xsl:template> 869 </xsl:stylesheet> 870 """)) 871 result = xslt(etree.XML('<a/>')) 872 root = result.getroot() 873 self.assertEquals(root.tag, 'test') 874 self.assertEquals(len(root), 5) 875 876 self.assertEquals( 877 root[0].text, 878 "http://www.bayes.co.uk/xml/index.xml?/xml/utils/rechecker.xml") 879 self.assertEquals( 880 root[1].text, 881 "http") 882 self.assertEquals( 883 root[2].text, 884 "www.bayes.co.uk") 885 self.assertFalse(root[3].text) 886 self.assertEquals( 887 root[4].text, 888 "/xml/index.xml?/xml/utils/rechecker.xml")
889
890 - def test_exslt_regexp_match2(self):
891 # taken from http://www.exslt.org/regexp/functions/match/index.html 892 xslt = etree.XSLT(etree.XML("""\ 893 <xsl:stylesheet version="1.0" 894 xmlns:regexp="http://exslt.org/regular-expressions" 895 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 896 <xsl:template match="/"> 897 <test> 898 <xsl:for-each select="regexp:match( 899 'This is a test string', '(\w+)', 'g')"> 900 <test1><xsl:value-of select="."/></test1> 901 </xsl:for-each> 902 </test> 903 </xsl:template> 904 </xsl:stylesheet> 905 """)) 906 result = xslt(etree.XML('<a/>')) 907 root = result.getroot() 908 self.assertEquals(root.tag, 'test') 909 self.assertEquals(len(root), 5) 910 911 self.assertEquals(root[0].text, "This") 912 self.assertEquals(root[1].text, "is") 913 self.assertEquals(root[2].text, "a") 914 self.assertEquals(root[3].text, "test") 915 self.assertEquals(root[4].text, "string")
916
917 - def _test_exslt_regexp_match3(self):
918 # taken from http://www.exslt.org/regexp/functions/match/index.html 919 # THIS IS NOT SUPPORTED! 920 xslt = etree.XSLT(etree.XML("""\ 921 <xsl:stylesheet version="1.0" 922 xmlns:regexp="http://exslt.org/regular-expressions" 923 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 924 <xsl:template match="/"> 925 <test> 926 <xsl:for-each select="regexp:match( 927 'This is a test string', '([a-z])+ ', 'g')"> 928 <test1><xsl:value-of select="."/></test1> 929 </xsl:for-each> 930 </test> 931 </xsl:template> 932 </xsl:stylesheet> 933 """)) 934 result = xslt(etree.XML('<a/>')) 935 root = result.getroot() 936 self.assertEquals(root.tag, 'test') 937 self.assertEquals(len(root), 4) 938 939 self.assertEquals(root[0].text, "his") 940 self.assertEquals(root[1].text, "is") 941 self.assertEquals(root[2].text, "a") 942 self.assertEquals(root[3].text, "test")
943
944 - def _test_exslt_regexp_match4(self):
945 # taken from http://www.exslt.org/regexp/functions/match/index.html 946 # THIS IS NOT SUPPORTED! 947 xslt = etree.XSLT(etree.XML("""\ 948 <xsl:stylesheet version="1.0" 949 xmlns:regexp="http://exslt.org/regular-expressions" 950 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 951 <xsl:template match="/"> 952 <test> 953 <xsl:for-each select="regexp:match( 954 'This is a test string', '([a-z])+ ', 'gi')"> 955 <test1><xsl:value-of select="."/></test1> 956 </xsl:for-each> 957 </test> 958 </xsl:template> 959 </xsl:stylesheet> 960 """)) 961 result = xslt(etree.XML('<a/>')) 962 root = result.getroot() 963 self.assertEquals(root.tag, 'test') 964 self.assertEquals(len(root), 4) 965 966 self.assertEquals(root[0].text, "This") 967 self.assertEquals(root[1].text, "is") 968 self.assertEquals(root[2].text, "a") 969 self.assertEquals(root[3].text, "test")
970
971 -def test_suite():
972 suite = unittest.TestSuite() 973 suite.addTests([unittest.makeSuite(ETreeXSLTTestCase)]) 974 suite.addTests( 975 [doctest.DocFileSuite('../../../doc/extensions.txt')]) 976 suite.addTests( 977 [doctest.DocFileSuite('../../../doc/xpathxslt.txt')]) 978 return suite
979 980 if __name__ == '__main__': 981 unittest.main() 982