# Examples from the article "Two-stage recursive algorithms in XSLT" # By Dimitre Novatchev and Slawomir Tyszko # http://www.topxml.com/xsl/articles/recurse/ from Xml.Xslt import test_harness BOOKS = """ Angela's Ashes Frank McCourt HarperCollins 0 00 649840 X 6.99 235 Sword of Honour Evelyn Waugh Penguin Books 0 14 018967 X 12.99 12 """ BOOKLIST_XML = """ %s """ BOOKS_TOTAL = 6.99 * 235 + 12.99 * 12 # total-sales/simple.xsl sheet_1 = """ """ # total-sales/dvc.xsl sheet_2 = """ """ # total-sales/two-stage.xsl # (with $t param added so threshold can be adjusted) # # The threshold is the # of elements above which DVC will be used, # and below which recursion will be used. # sheet_3=""" """ DIGITS = "0123456789" DIGITS_XML = """ %s""" REVERSED_DIGITS = "9876543210" # reverse/lrReverse.xsl sheet_4 = """ """ # reverse/lrReverse2.xsl # (with $t param added so threshold can be adjusted) # # The threshold is the # of chars above which DVC will be used, # and below which recursion will be used. # sheet_5 = """ """ GOBBLEDY = "dfd dh AAAsrter xcbxb AAAA gghmjk gfghjk ghAAAkghk dgsdfgAAA sdsdg AAA sdsdfg\n" GOBBLEDY_XML = """ %s""" GOBBLEDY_OUT = GOBBLEDY.replace('AAA','ZZZ') sheet_6=""" """ sheet_7=""" """ def Test(tester): # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 sheet = test_harness.FileInfo(string=sheet_1) for i in range(EXPLIMIT): elements = (2 * MULTIPLIER) * 2 ** i title = "simple recursion with %d element" % elements + "s" * (elements > 0) source_xml = BOOKLIST_XML % ((BOOKS * MULTIPLIER) * 2 ** i) source_1 = test_harness.FileInfo(string=source_xml) expected_1 = str((BOOKS_TOTAL * MULTIPLIER) * 2 ** i) test_harness.XsltTest(tester, source_1, [sheet], expected_1, title=title) sheet = test_harness.FileInfo(string=sheet_2) for i in range(EXPLIMIT): elements = (2 * MULTIPLIER) * 2 ** i title = "divide and conquer with %d element" % elements + "s" * (elements > 0) source_xml = BOOKLIST_XML % ((BOOKS * MULTIPLIER) * 2 ** i) source_1 = test_harness.FileInfo(string=source_xml) expected_1 = str((BOOKS_TOTAL * MULTIPLIER) * 2 ** i) test_harness.XsltTest(tester, source_1, [sheet], expected_1, title=title) sheet = test_harness.FileInfo(string=sheet_3) for i in range(EXPLIMIT): threshold = 8 # seems to be best as of 2003-03-23 elements = (2 * MULTIPLIER) * 2 ** i title = "2-stage divide and conquer with %d element" % elements + "s" * (elements > 0) title += " (threshold=%d)" % threshold source_xml = BOOKLIST_XML % ((BOOKS * MULTIPLIER) * 2 ** i) source_1 = test_harness.FileInfo(string=source_xml) expected_1 = str((BOOKS_TOTAL * MULTIPLIER) * 2 ** i) test_harness.XsltTest(tester, source_1, [sheet], expected_1, title=title, topLevelParams={'t': threshold}) sheet = test_harness.FileInfo(string=sheet_4) for i in range(EXPLIMIT): chars = 1000 * 2 ** i title = "divide and conquer reversal of %d-char string" % chars source_xml = DIGITS_XML % ((DIGITS * 100) * 2 ** i) source_1 = test_harness.FileInfo(string=source_xml) expected_1 = (REVERSED_DIGITS * 100) * 2 ** i test_harness.XsltTest(tester, source_1, [sheet], expected_1, title=title) sheet = test_harness.FileInfo(string=sheet_5) for i in range(EXPLIMIT): threshold = 75 chars = 1000 * 2 ** i title = "2-stage divide and conquer reversal of %d-char string" % chars title += " (threshold=%d)" % threshold source_xml = DIGITS_XML % ((DIGITS * 100) * 2 ** i) source_1 = test_harness.FileInfo(string=source_xml) expected_1 = (REVERSED_DIGITS * 100) * 2 ** i test_harness.XsltTest(tester, source_1, [sheet], expected_1, title=title, topLevelParams={'t': threshold}) sheet = test_harness.FileInfo(string=sheet_6) for i in range(EXPLIMIT): chars = (len(GOBBLEDY) * 20) * 2 ** i title = "divide and conquer search/replace on %d-char string" % chars source_xml = GOBBLEDY_XML % ((GOBBLEDY * 20) * 2 ** i) source_1 = test_harness.FileInfo(string=source_xml) expected_1 = (GOBBLEDY_OUT * 20) * 2 ** i test_harness.XsltTest(tester, source_1, [sheet], expected_1, title=title) sheet = test_harness.FileInfo(string=sheet_7) for i in range(EXPLIMIT): chars = (len(GOBBLEDY) * 20) * 2 ** i title = "2-stage divide and conquer search/replace on %d-char string" % chars source_xml = GOBBLEDY_XML % ((GOBBLEDY * 20) * 2 ** i) source_1 = test_harness.FileInfo(string=source_xml) expected_1 = (GOBBLEDY_OUT * 20) * 2 ** i test_harness.XsltTest(tester, source_1, [sheet], expected_1, title=title) return