def test_xslt_cdata_section_elements_og_20010507(): _run_text( source_xml = """\ <?xml version="1.0" encoding="ISO-8859-1"?> <test> <data> <num>1000</num> <str>test</str> </data> </test>""", transform_xml = """\ <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"> <xsl:output method="text" encoding="ISO-8859-1"/> <xsl:template match="/"> <xsl:variable name="file" select="'%s'"/> <exsl:document href="{$file}" method="xml" indent="yes" encoding="ISO-8859-1" cdata-section-elements="str num"> <datatree> <content><xsl:copy-of select="test/data/*"/></content> <what>test</what> </datatree> </exsl:document> </xsl:template> </xsl:stylesheet>"""%(FILESTEM_URI), expected = "\n" )
def test_xslt_cdata_section_elements_og_20010507(): _run_text(source_xml="""\ <?xml version="1.0" encoding="ISO-8859-1"?> <test> <data> <num>1000</num> <str>test</str> </data> </test>""", transform_xml="""\ <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"> <xsl:output method="text" encoding="ISO-8859-1"/> <xsl:template match="/"> <xsl:variable name="file" select="'%s'"/> <exsl:document href="{$file}" method="xml" indent="yes" encoding="ISO-8859-1" cdata-section-elements="str num"> <datatree> <content><xsl:copy-of select="test/data/*"/></content> <what>test</what> </datatree> </exsl:document> </xsl:template> </xsl:stylesheet>""" % (FILESTEM_URI), expected="\n")
def test_xslt_eratosthenes_ob_20000613(): _run_text( source_xml = "<dummy/>""", transform_xml = xslt_path, expected = """2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, """, parameters = {'bound':300}, )
def test_xslt_recursive_2_dn_20020325(): # total-sales/dvc.xsl transform_xml = """<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="/*/book"/> </xsl:call-template> </xsl:template> <xsl:template name="sumSales"> <xsl:param name="pNodes" select="/.."/> <xsl:param name="result" select="0"/> <xsl:variable name="vcntNodes" select="count($pNodes)"/> <xsl:choose> <xsl:when test="$vcntNodes = 1"> <xsl:value-of select="$result + $pNodes/sales * $pNodes/price"/> </xsl:when> <xsl:otherwise> <xsl:variable name="vcntHalf" select="floor($vcntNodes div 2)"/> <xsl:variable name="vValue1"> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="$pNodes[position() <= $vcntHalf]"/> <xsl:with-param name="result" select="$result"/> </xsl:call-template> </xsl:variable> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="$pNodes[position() > $vcntHalf]"/> <xsl:with-param name="result" select="$vValue1"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 for i in range(EXPLIMIT): elements = (2 * MULTIPLIER) * 2**i #title = "simple recursion with %d element" % elements + "s" * (elements > 0) source = BOOKLIST_XML % ((BOOKS * MULTIPLIER) * 2**i) expected = str((BOOKS_TOTAL * MULTIPLIER) * 2**i) _run_text( source_xml=source, transform_xml=transform_xml, expected=expected, )
def test_xslt_recursive_2_dn_20020325(): # total-sales/dvc.xsl transform_xml = """<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="/*/book"/> </xsl:call-template> </xsl:template> <xsl:template name="sumSales"> <xsl:param name="pNodes" select="/.."/> <xsl:param name="result" select="0"/> <xsl:variable name="vcntNodes" select="count($pNodes)"/> <xsl:choose> <xsl:when test="$vcntNodes = 1"> <xsl:value-of select="$result + $pNodes/sales * $pNodes/price"/> </xsl:when> <xsl:otherwise> <xsl:variable name="vcntHalf" select="floor($vcntNodes div 2)"/> <xsl:variable name="vValue1"> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="$pNodes[position() <= $vcntHalf]"/> <xsl:with-param name="result" select="$result"/> </xsl:call-template> </xsl:variable> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="$pNodes[position() > $vcntHalf]"/> <xsl:with-param name="result" select="$vValue1"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 for i in range(EXPLIMIT): elements = (2 * MULTIPLIER) * 2 ** i #title = "simple recursion with %d element" % elements + "s" * (elements > 0) source = BOOKLIST_XML % ((BOOKS * MULTIPLIER) * 2 ** i) expected = str((BOOKS_TOTAL * MULTIPLIER) * 2 ** i) _run_text( source_xml = source, transform_xml = transform_xml, expected = expected, )
def test_xslt_eratosthenes_ob_20000613(): _run_text( source_xml="<dummy/>" "", transform_xml=xslt_path, expected= """2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, """, parameters={'bound': 300}, )
def test_xslt_recursive_4_dn_20020325(): # reverse/lrReverse.xsl transform_xml = """<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="/*/text()"/> </xsl:call-template> </xsl:template> <xsl:template name="reverse2"> <xsl:param name="theString"/> <xsl:variable name="thisLength" select="string-length($theString)"/> <xsl:choose> <xsl:when test="$thisLength = 1"> <xsl:value-of select="$theString"/> </xsl:when> <xsl:otherwise> <xsl:variable name="length1" select="floor($thisLength div 2)"/> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="substring($theString,$length1+1, $thisLength - $length1)"/> </xsl:call-template> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="substring($theString, 1, $length1)"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 for i in range(EXPLIMIT): chars = 1000 * 2 ** i #title = "divide and conquer reversal of %d-char string" % chars source = DIGITS_XML % ((DIGITS * 100) * 2 ** i) expected = str((REVERSED_DIGITS * 100) * 2 ** i) _run_text( source_xml = source, transform_xml = transform_xml, expected = expected, )
def test_xslt_recursive_4_dn_20020325(): # reverse/lrReverse.xsl transform_xml = """<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="/*/text()"/> </xsl:call-template> </xsl:template> <xsl:template name="reverse2"> <xsl:param name="theString"/> <xsl:variable name="thisLength" select="string-length($theString)"/> <xsl:choose> <xsl:when test="$thisLength = 1"> <xsl:value-of select="$theString"/> </xsl:when> <xsl:otherwise> <xsl:variable name="length1" select="floor($thisLength div 2)"/> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="substring($theString,$length1+1, $thisLength - $length1)"/> </xsl:call-template> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="substring($theString, 1, $length1)"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 for i in range(EXPLIMIT): chars = 1000 * 2**i #title = "divide and conquer reversal of %d-char string" % chars source = DIGITS_XML % ((DIGITS * 100) * 2**i) expected = str((REVERSED_DIGITS * 100) * 2**i) _run_text( source_xml=source, transform_xml=transform_xml, expected=expected, )
def test_xslt_recursive_7_dn_20020325(): transform_xml = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common"> <xsl:output method="text" encoding="iso-8859-1" /> <xsl:template match="/"> <xsl:variable name="Result"> <xsl:call-template name="lrReplace"> <xsl:with-param name="theString" select="/*/text()"/> <xsl:with-param name="target" select="'AAA'"/> <xsl:with-param name="replacement" select="'ZZZ'"/> <xsl:with-param name="threshold" select="2000"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$Result" /> </xsl:template> <xsl:template name="lrReplace"> <xsl:param name="theString"/> <xsl:param name="target"/> <xsl:param name="replacement"/> <xsl:param name="threshold" select="150"/> <xsl:variable name="lStr" select="string-length($theString)"/> <xsl:variable name="resRTF"> <xsl:call-template name="lrReplace2"> <xsl:with-param name="theString" select="$theString"/> <xsl:with-param name="target" select="$target"/> <xsl:with-param name="replacement" select="$replacement"/> <xsl:with-param name="threshold" select="$threshold"/> </xsl:call-template> </xsl:variable> <xsl:variable name="resNode-set" select="exsl:node-set($resRTF)"/> <xsl:value-of select="$resNode-set/text()"/> <xsl:value-of select="substring($theString, $lStr - $resNode-set/u+1)"/> </xsl:template> <!-- DVC template: --> <xsl:template name="lrReplace2"> <xsl:param name="theString"/> <xsl:param name="target"/> <xsl:param name="replacement"/> <xsl:param name="threshold" select="150"/> <xsl:variable name="lStr" select="string-length($theString)"/> <xsl:variable name="lTarget" select="string-length($target)"/> <xsl:choose> <xsl:when test="$lStr <= $threshold"> <xsl:call-template name="lrReplace3"> <xsl:with-param name="theString" select="$theString"/> <xsl:with-param name="target" select="$target"/> <xsl:with-param name="replacement" select="$replacement"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:variable name="halfLength" select="floor($lStr div 2)"/> <xsl:variable name="processedHalf"> <xsl:call-template name="lrReplace2"> <xsl:with-param name="theString" select="substring($theString, 1, $halfLength)" /> <xsl:with-param name="target" select="$target" /> <xsl:with-param name="replacement" select="$replacement"/> <xsl:with-param name="threshold" select="$threshold"/> </xsl:call-template> </xsl:variable> <xsl:variable name="nodePrHalf" select="exsl:node-set($processedHalf)"/> <xsl:value-of select="$nodePrHalf/text()"/> <xsl:call-template name="lrReplace2"> <xsl:with-param name="theString" select="substring($theString, $halfLength - $nodePrHalf/u + 1)"/> <xsl:with-param name="target" select="$target"/> <xsl:with-param name="replacement" select="$replacement"/> <xsl:with-param name="threshold" select="$threshold" /> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- simple recursive template: --> <xsl:template name="lrReplace3"> <xsl:param name="theString" /> <xsl:param name="target" /> <xsl:param name="replacement" /> <xsl:choose> <xsl:when test="contains($theString, $target)"> <xsl:value-of select="substring-before($theString, $target)"/> <xsl:value-of select="$replacement"/> <xsl:call-template name="lrReplace3"> <xsl:with-param name="theString" select="substring-after($theString, $target)"/> <xsl:with-param name="target" select="$target"/> <xsl:with-param name="replacement" select="$replacement"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:variable name="lStr" select="string-length($theString)"/> <xsl:variable name="lTarget" select="string-length($target)"/> <xsl:choose> <xsl:when test="$lStr >= $lTarget"> <xsl:value-of select="substring($theString, 1, $lStr -$lTarget+1)" /> <u> <xsl:value-of select="$lTarget -1"/> </u> </xsl:when> <xsl:otherwise> <u> <xsl:value-of select="$lStr"/> </u> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 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 = GOBBLEDY_XML % ((GOBBLEDY * 20) * 2 ** i) expected = str((GOBBLEDY_OUT * 20) * 2 ** i) _run_text( source_xml = source, transform_xml = transform_xml, expected = expected)
def test_borrowed(source_xml=source_xml, transform_xml=transform_xml, expected=expected_txt): _run_text( source_xml = inputsource(source_xml), transform_xml = inputsource(transform_xml), expected = inputsource(expected).stream.read())
def test_xslt_debug_1_mn_20001128(): transform = """<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method="text"/> <xsl:key name="elements" match="*" use="name()"/> <xsl:key name="attributes" match="@*" use="concat(name(parent::*),':::',name())"/> <xsl:key name="allSameAttributes" match="@*" use="name(parent::*)"/> <xsl:template match = "*" > <xsl:if test="generate-id() = generate-id(key('elements',name()))"> <xsl:text>
</xsl:text> <xsl:value-of select="name()"/> <xsl:apply-templates select="key('allSameAttributes',name())"> <xsl:sort select='name()'/> </xsl:apply-templates> </xsl:if> <xsl:apply-templates/> </xsl:template> <xsl:template match="@*"> <xsl:if test="position()=1"> <xsl:text> [ </xsl:text> </xsl:if> <xsl:if test="generate-id() = generate-id(key('attributes',concat(name(parent::*),':::',name())))"> <xsl:value-of select="name()"/> <xsl:text> </xsl:text> </xsl:if> <xsl:if test="position()=last()"> <xsl:text> ] </xsl:text> </xsl:if> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>""" expected = """ slideshow title metadata speaker jobTitle organization presentationDate presentationLocation occasion slideset slide [ id ] item speakerNote emphasis [ role ] heading bulletlist preformatted graphic [ file height width ] link [ href ] para""" _run_text( source_xml=SLIDES4SVG_XML, transform_xml=transform, expected=expected, )
def test_xslt_debug_2_mn_20001128(): transform = """<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method="text"/> <xsl:key name="elements" match="*" use="name()"/> <xsl:key name="attributes" match="@*" use="concat(name(parent::*),':::',name())"/> <xsl:key name="allSameAttributes" match="@*" use="name(parent::*)"/> <xsl:template match="/"> <xsl:apply-templates select="//*"> <xsl:sort select="name()"/> </xsl:apply-templates> </xsl:template> <xsl:template match = "*" > <xsl:if test="generate-id() = generate-id(key('elements',name()))"> <xsl:text>
</xsl:text> <xsl:value-of select="name()"/> <xsl:apply-templates select="key('allSameAttributes',name())"> <xsl:sort select="name()"/> </xsl:apply-templates> </xsl:if> </xsl:template> <xsl:template match="@*"> <xsl:if test="generate-id() = generate-id(key('attributes',concat(name(parent::*),':::',name())))"> <xsl:text>
 </xsl:text> <xsl:value-of select="name()"/> <xsl:text>: </xsl:text> <xsl:apply-templates select="key('attributes',concat(name(parent::*),':::',name()))" mode="values"> <xsl:sort/> </xsl:apply-templates> </xsl:if> </xsl:template> <xsl:template match="@*" mode="values"> <xsl:variable name="sameValues" select="key('attributes',concat(name(parent::*),':::',name()))[. = current()]" /> <xsl:if test="generate-id() = generate-id($sameValues)"> <xsl:value-of select="."/> <xsl:text>(</xsl:text> <xsl:value-of select="count($sameValues)"/> <xsl:text>) </xsl:text> </xsl:if> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>""" expected = """ bulletlist emphasis role: note(3) graphic file: sample.svg(1) height: 800(1) width: 800(1) heading item jobTitle link href: http://dmoz.org/Computers/Data_Formats/Graphics/Vector/SVG/(1) http://www.sun.com/xml/developers/svg-slidetoolkit/(1) http://www.w3.org/Graphics/SVG(1) metadata occasion organization para preformatted presentationDate presentationLocation slide id: I.1(1) II.1(1) II.2(1) II.3(1) III.1(1) III.2(1) slideset slideshow speaker speakerNote title""" _run_text( source_xml=SLIDES4SVG_XML, transform_xml=transform, expected=expected, )
def test_xslt_recursive_7_dn_20020325(): transform_xml = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common"> <xsl:output method="text" encoding="iso-8859-1" /> <xsl:template match="/"> <xsl:variable name="Result"> <xsl:call-template name="lrReplace"> <xsl:with-param name="theString" select="/*/text()"/> <xsl:with-param name="target" select="'AAA'"/> <xsl:with-param name="replacement" select="'ZZZ'"/> <xsl:with-param name="threshold" select="2000"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$Result" /> </xsl:template> <xsl:template name="lrReplace"> <xsl:param name="theString"/> <xsl:param name="target"/> <xsl:param name="replacement"/> <xsl:param name="threshold" select="150"/> <xsl:variable name="lStr" select="string-length($theString)"/> <xsl:variable name="resRTF"> <xsl:call-template name="lrReplace2"> <xsl:with-param name="theString" select="$theString"/> <xsl:with-param name="target" select="$target"/> <xsl:with-param name="replacement" select="$replacement"/> <xsl:with-param name="threshold" select="$threshold"/> </xsl:call-template> </xsl:variable> <xsl:variable name="resNode-set" select="exsl:node-set($resRTF)"/> <xsl:value-of select="$resNode-set/text()"/> <xsl:value-of select="substring($theString, $lStr - $resNode-set/u+1)"/> </xsl:template> <!-- DVC template: --> <xsl:template name="lrReplace2"> <xsl:param name="theString"/> <xsl:param name="target"/> <xsl:param name="replacement"/> <xsl:param name="threshold" select="150"/> <xsl:variable name="lStr" select="string-length($theString)"/> <xsl:variable name="lTarget" select="string-length($target)"/> <xsl:choose> <xsl:when test="$lStr <= $threshold"> <xsl:call-template name="lrReplace3"> <xsl:with-param name="theString" select="$theString"/> <xsl:with-param name="target" select="$target"/> <xsl:with-param name="replacement" select="$replacement"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:variable name="halfLength" select="floor($lStr div 2)"/> <xsl:variable name="processedHalf"> <xsl:call-template name="lrReplace2"> <xsl:with-param name="theString" select="substring($theString, 1, $halfLength)" /> <xsl:with-param name="target" select="$target" /> <xsl:with-param name="replacement" select="$replacement"/> <xsl:with-param name="threshold" select="$threshold"/> </xsl:call-template> </xsl:variable> <xsl:variable name="nodePrHalf" select="exsl:node-set($processedHalf)"/> <xsl:value-of select="$nodePrHalf/text()"/> <xsl:call-template name="lrReplace2"> <xsl:with-param name="theString" select="substring($theString, $halfLength - $nodePrHalf/u + 1)"/> <xsl:with-param name="target" select="$target"/> <xsl:with-param name="replacement" select="$replacement"/> <xsl:with-param name="threshold" select="$threshold" /> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- simple recursive template: --> <xsl:template name="lrReplace3"> <xsl:param name="theString" /> <xsl:param name="target" /> <xsl:param name="replacement" /> <xsl:choose> <xsl:when test="contains($theString, $target)"> <xsl:value-of select="substring-before($theString, $target)"/> <xsl:value-of select="$replacement"/> <xsl:call-template name="lrReplace3"> <xsl:with-param name="theString" select="substring-after($theString, $target)"/> <xsl:with-param name="target" select="$target"/> <xsl:with-param name="replacement" select="$replacement"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:variable name="lStr" select="string-length($theString)"/> <xsl:variable name="lTarget" select="string-length($target)"/> <xsl:choose> <xsl:when test="$lStr >= $lTarget"> <xsl:value-of select="substring($theString, 1, $lStr -$lTarget+1)" /> <u> <xsl:value-of select="$lTarget -1"/> </u> </xsl:when> <xsl:otherwise> <u> <xsl:value-of select="$lStr"/> </u> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 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 = GOBBLEDY_XML % ((GOBBLEDY * 20) * 2**i) expected = str((GOBBLEDY_OUT * 20) * 2**i) _run_text(source_xml=source, transform_xml=transform_xml, expected=expected)
def test_xslt_recursive_5_dn_20020325(): # 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. # transform_xml = """<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:param name="t" select="75"/> <xsl:template match="/"> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="/*/text()"/> <xsl:with-param name="threshold" select="$t"/> </xsl:call-template> </xsl:template> <!-- DVC template: --> <xsl:template name="reverse2"> <xsl:param name="theString"/> <xsl:param name="threshold" select="30"/> <xsl:variable name="thisLength" select="string-length($theString)"/> <xsl:choose> <xsl:when test="$thisLength <= $threshold"> <xsl:call-template name="reverse"> <xsl:with-param name="theString" select="$theString"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:variable name="length1" select="floor($thisLength div 2)"/> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="substring($theString,$length1+1, $thisLength - $length1)"/> <xsl:with-param name="threshold" select="$threshold"/> </xsl:call-template> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="substring($theString, 1, $length1)"/> <xsl:with-param name="threshold" select="$threshold"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- simple recursive template: --> <xsl:template name="reverse"> <xsl:param name="theString"/> <xsl:variable name="thisLength" select="string-length($theString)"/> <xsl:choose> <xsl:when test="$thisLength = 1"> <xsl:value-of select="$theString"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="substring($theString,$thisLength,1)"/> <xsl:call-template name="reverse"> <xsl:with-param name="theString" select="substring($theString, 1, $thisLength -1)"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 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 = DIGITS_XML % ((DIGITS * 100) * 2**i) expected = str((REVERSED_DIGITS * 100) * 2**i) _run_text(source_xml=source, transform_xml=transform_xml, expected=expected)
def test_xslt_debug_2_mn_20001128(): transform = """<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method="text"/> <xsl:key name="elements" match="*" use="name()"/> <xsl:key name="attributes" match="@*" use="concat(name(parent::*),':::',name())"/> <xsl:key name="allSameAttributes" match="@*" use="name(parent::*)"/> <xsl:template match="/"> <xsl:apply-templates select="//*"> <xsl:sort select="name()"/> </xsl:apply-templates> </xsl:template> <xsl:template match = "*" > <xsl:if test="generate-id() = generate-id(key('elements',name()))"> <xsl:text>
</xsl:text> <xsl:value-of select="name()"/> <xsl:apply-templates select="key('allSameAttributes',name())"> <xsl:sort select="name()"/> </xsl:apply-templates> </xsl:if> </xsl:template> <xsl:template match="@*"> <xsl:if test="generate-id() = generate-id(key('attributes',concat(name(parent::*),':::',name())))"> <xsl:text>
 </xsl:text> <xsl:value-of select="name()"/> <xsl:text>: </xsl:text> <xsl:apply-templates select="key('attributes',concat(name(parent::*),':::',name()))" mode="values"> <xsl:sort/> </xsl:apply-templates> </xsl:if> </xsl:template> <xsl:template match="@*" mode="values"> <xsl:variable name="sameValues" select="key('attributes',concat(name(parent::*),':::',name()))[. = current()]" /> <xsl:if test="generate-id() = generate-id($sameValues)"> <xsl:value-of select="."/> <xsl:text>(</xsl:text> <xsl:value-of select="count($sameValues)"/> <xsl:text>) </xsl:text> </xsl:if> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>""" expected = """ bulletlist emphasis role: note(3) graphic file: sample.svg(1) height: 800(1) width: 800(1) heading item jobTitle link href: http://dmoz.org/Computers/Data_Formats/Graphics/Vector/SVG/(1) http://www.sun.com/xml/developers/svg-slidetoolkit/(1) http://www.w3.org/Graphics/SVG(1) metadata occasion organization para preformatted presentationDate presentationLocation slide id: I.1(1) II.1(1) II.2(1) II.3(1) III.1(1) III.2(1) slideset slideshow speaker speakerNote title""" _run_text( source_xml = SLIDES4SVG_XML, transform_xml = transform, expected = expected, )
def test_borrowed(source_xml=source_xml, transform_xml=transform_xml, expected=expected_txt): _run_text(source_xml=inputsource(source_xml), transform_xml=inputsource(transform_xml), expected=inputsource(expected).stream.read())
def test_xslt_count_children_via_incrementing_counter_dn_20010504(): _run_text( source_xml = """\ <numbers> <num>3</num> <num>2</num> <num>9</num> <num>4</num> <num>6</num> <num>5</num> <num>7</num> <num>8</num> <num>1</num> </numbers>""", transform_uri = iri.os_path_to_uri(__file__), transform_xml = """\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:num="num" xmlns:num2="num2" > <num:node>num</num:node> <num2:node>num2</num2:node> <xsl:output method="text" /> <xsl:variable name="document" select="document('')" /> <xsl:variable name="gtNum-node" select="$document//num:*"/> <xsl:variable name="gtNum2-node" select="$document//num2:*"/> <xsl:template match="/"> <xsl:call-template name="get-max"> <xsl:with-param name="greaterSelector" select="$gtNum-node" /> <xsl:with-param name="nodes" select="/numbers/num" /> </xsl:call-template> <xsl:text>
</xsl:text> <xsl:call-template name="get-max"> <xsl:with-param name="greaterSelector" select="$gtNum2-node" /> <xsl:with-param name="nodes" select="/numbers/num" /> </xsl:call-template> </xsl:template> <xsl:template name="get-max"> <xsl:param name="greaterSelector" select="/*"/> <xsl:param name="nodes" /> <xsl:choose> <xsl:when test="$nodes"> <xsl:variable name="max-of-rest"> <xsl:call-template name="get-max"> <xsl:with-param name="greaterSelector" select="$greaterSelector" /> <xsl:with-param name="nodes" select="$nodes[position()!=1]" /> </xsl:call-template> </xsl:variable> <xsl:variable name="isGreater"> <xsl:apply-templates select="$greaterSelector" > <xsl:with-param name="n1" select="$nodes[1]"/> <xsl:with-param name="n2" select="$max-of-rest"/> </xsl:apply-templates> </xsl:variable> <xsl:choose> <xsl:when test="$isGreater = 'true'"> <xsl:value-of select="$nodes[1]" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="$max-of-rest" /> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:value-of select="-999999999" /> <!-- minus infinity --> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="isGreaterNum" match="node()[namespace-uri()='num']"> <xsl:param name="n1"/> <xsl:param name="n2"/> <xsl:value-of select="$n1 > $n2"/> </xsl:template> <xsl:template name="isGreaterNum2" match="node()[namespace-uri()='num2']"> <xsl:param name="n1"/> <xsl:param name="n2"/> <xsl:value-of select="1 div $n1 > 1 div $n2"/> </xsl:template> </xsl:stylesheet>""", expected = """\ 9 1""")
def test_xslt_recursive_5_dn_20020325(): # 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. # transform_xml = """<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:param name="t" select="75"/> <xsl:template match="/"> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="/*/text()"/> <xsl:with-param name="threshold" select="$t"/> </xsl:call-template> </xsl:template> <!-- DVC template: --> <xsl:template name="reverse2"> <xsl:param name="theString"/> <xsl:param name="threshold" select="30"/> <xsl:variable name="thisLength" select="string-length($theString)"/> <xsl:choose> <xsl:when test="$thisLength <= $threshold"> <xsl:call-template name="reverse"> <xsl:with-param name="theString" select="$theString"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:variable name="length1" select="floor($thisLength div 2)"/> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="substring($theString,$length1+1, $thisLength - $length1)"/> <xsl:with-param name="threshold" select="$threshold"/> </xsl:call-template> <xsl:call-template name="reverse2"> <xsl:with-param name="theString" select="substring($theString, 1, $length1)"/> <xsl:with-param name="threshold" select="$threshold"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- simple recursive template: --> <xsl:template name="reverse"> <xsl:param name="theString"/> <xsl:variable name="thisLength" select="string-length($theString)"/> <xsl:choose> <xsl:when test="$thisLength = 1"> <xsl:value-of select="$theString"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="substring($theString,$thisLength,1)"/> <xsl:call-template name="reverse"> <xsl:with-param name="theString" select="substring($theString, 1, $thisLength -1)"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 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 = DIGITS_XML % ((DIGITS * 100) * 2 ** i) expected = str((REVERSED_DIGITS * 100) * 2 ** i) _run_text( source_xml = source, transform_xml = transform_xml, expected = expected)
def test_xslt_recursive_3_dn_20020325(): # 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. # transform_xml = """<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:param name="t" select="20"/> <xsl:template match="/"> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="/*/book"/> <xsl:with-param name="threshold" select="$t"/> </xsl:call-template> </xsl:template> <!-- DVC template: --> <xsl:template name="sumSales"> <xsl:param name="pNodes" select="/.."/> <xsl:param name="threshold" select="10"/> <xsl:param name="result" select="0"/> <xsl:variable name="vcntNodes" select="count($pNodes)"/> <xsl:choose> <xsl:when test="$vcntNodes <= $threshold"> <xsl:call-template name="sumSales1"> <xsl:with-param name="pNodes" select="$pNodes"/> <xsl:with-param name="result" select="$result"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:variable name="vcntHalf" select="floor($vcntNodes div 2)"/> <xsl:variable name="vValue1"> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="$pNodes[position() <= $vcntHalf]"/> <xsl:with-param name="threshold" select="$threshold"/> <xsl:with-param name="result" select="$result"/> </xsl:call-template> </xsl:variable> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="$pNodes[position() > $vcntHalf]"/> <xsl:with-param name="threshold" select="$threshold"/> <xsl:with-param name="result" select="$vValue1"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- simple recursive template: --> <xsl:template name="sumSales1"> <xsl:param name="pNodes" select="/.."/> <xsl:param name="result" select="0"/> <xsl:choose> <xsl:when test="$pNodes"> <xsl:call-template name="sumSales1"> <xsl:with-param name="pNodes" select="$pNodes[position()!=1]"/> <xsl:with-param name="result" select="$result+$pNodes[1]/sales*$pNodes[1]/price"/> </xsl:call-template> </xsl:when> <xsl:otherwise><xsl:value-of select="$result"/></xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 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 = BOOKLIST_XML % ((BOOKS * MULTIPLIER) * 2 ** i) expected = str((BOOKS_TOTAL * MULTIPLIER) * 2 ** i) _run_text( source_xml = source, transform_xml = transform_xml, expected = expected)
def test_xslt_call_template_ed_20010101(): transform = """<?xml version="1.0" encoding="utf-8"?> <!-- Ackermann's function http://pweb.netcom.com/~hjsmith/Ackerman/AckeWhat.html 1. If x = 0 then A(x, y) = y + 1 2. If y = 0 then A(x, y) = A(x-1, 1) 3. Otherwise, A(x, y) = A(x-1, A(x, y-1)) A(3,4) = 125 A(3,5) = 253 A(3,6) = 509; template called 172233 times, nested up to 511 calls deep A(3,7) will call the template 693964 times (good luck) --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" indent="no"/> <xsl:param name="x" select="3"/> <xsl:param name="y" select="6"/> <xsl:template match="/"> <xsl:value-of select="concat('A(',$x,',',$y,') = ')"/> <xsl:variable name="c" select="0"/> <xsl:call-template name="A"> <xsl:with-param name="x" select="$x"/> <xsl:with-param name="y" select="$y"/> </xsl:call-template> </xsl:template> <xsl:template name="A"> <xsl:param name="x" select="0"/> <xsl:param name="y" select="0"/> <xsl:choose> <xsl:when test="$x = 0"> <xsl:value-of select="$y + 1"/> </xsl:when> <xsl:when test="$y = 0"> <xsl:call-template name="A"> <xsl:with-param name="x" select="$x - 1"/> <xsl:with-param name="y" select="1"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:call-template name="A"> <xsl:with-param name="x" select="$x - 1"/> <xsl:with-param name="y"> <xsl:call-template name="A"> <xsl:with-param name="x" select="$x"/> <xsl:with-param name="y" select="$y - 1"/> </xsl:call-template> </xsl:with-param> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" _run_text( source_xml="<dummy/>", transform_xml=transform, # modifies the defaults parameters={ 'x': 3, 'y': 4 }, expected="A(3,4) = 125", )
def test_xslt_recursive_3_dn_20020325(): # 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. # transform_xml = """<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:param name="t" select="20"/> <xsl:template match="/"> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="/*/book"/> <xsl:with-param name="threshold" select="$t"/> </xsl:call-template> </xsl:template> <!-- DVC template: --> <xsl:template name="sumSales"> <xsl:param name="pNodes" select="/.."/> <xsl:param name="threshold" select="10"/> <xsl:param name="result" select="0"/> <xsl:variable name="vcntNodes" select="count($pNodes)"/> <xsl:choose> <xsl:when test="$vcntNodes <= $threshold"> <xsl:call-template name="sumSales1"> <xsl:with-param name="pNodes" select="$pNodes"/> <xsl:with-param name="result" select="$result"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:variable name="vcntHalf" select="floor($vcntNodes div 2)"/> <xsl:variable name="vValue1"> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="$pNodes[position() <= $vcntHalf]"/> <xsl:with-param name="threshold" select="$threshold"/> <xsl:with-param name="result" select="$result"/> </xsl:call-template> </xsl:variable> <xsl:call-template name="sumSales"> <xsl:with-param name="pNodes" select="$pNodes[position() > $vcntHalf]"/> <xsl:with-param name="threshold" select="$threshold"/> <xsl:with-param name="result" select="$vValue1"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- simple recursive template: --> <xsl:template name="sumSales1"> <xsl:param name="pNodes" select="/.."/> <xsl:param name="result" select="0"/> <xsl:choose> <xsl:when test="$pNodes"> <xsl:call-template name="sumSales1"> <xsl:with-param name="pNodes" select="$pNodes[position()!=1]"/> <xsl:with-param name="result" select="$result+$pNodes[1]/sales*$pNodes[1]/price"/> </xsl:call-template> </xsl:when> <xsl:otherwise><xsl:value-of select="$result"/></xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" # how many repetitions of BOOKS for the shortest source doc MULTIPLIER = 10 # how many binary orders of magnitude to go up to EXPLIMIT = 1 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 = BOOKLIST_XML % ((BOOKS * MULTIPLIER) * 2**i) expected = str((BOOKS_TOTAL * MULTIPLIER) * 2**i) _run_text(source_xml=source, transform_xml=transform_xml, expected=expected)
def test_xslt_call_template_ed_20010101(): transform = """<?xml version="1.0" encoding="utf-8"?> <!-- Ackermann's function http://pweb.netcom.com/~hjsmith/Ackerman/AckeWhat.html 1. If x = 0 then A(x, y) = y + 1 2. If y = 0 then A(x, y) = A(x-1, 1) 3. Otherwise, A(x, y) = A(x-1, A(x, y-1)) A(3,4) = 125 A(3,5) = 253 A(3,6) = 509; template called 172233 times, nested up to 511 calls deep A(3,7) will call the template 693964 times (good luck) --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" indent="no"/> <xsl:param name="x" select="3"/> <xsl:param name="y" select="6"/> <xsl:template match="/"> <xsl:value-of select="concat('A(',$x,',',$y,') = ')"/> <xsl:variable name="c" select="0"/> <xsl:call-template name="A"> <xsl:with-param name="x" select="$x"/> <xsl:with-param name="y" select="$y"/> </xsl:call-template> </xsl:template> <xsl:template name="A"> <xsl:param name="x" select="0"/> <xsl:param name="y" select="0"/> <xsl:choose> <xsl:when test="$x = 0"> <xsl:value-of select="$y + 1"/> </xsl:when> <xsl:when test="$y = 0"> <xsl:call-template name="A"> <xsl:with-param name="x" select="$x - 1"/> <xsl:with-param name="y" select="1"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:call-template name="A"> <xsl:with-param name="x" select="$x - 1"/> <xsl:with-param name="y"> <xsl:call-template name="A"> <xsl:with-param name="x" select="$x"/> <xsl:with-param name="y" select="$y - 1"/> </xsl:call-template> </xsl:with-param> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>""" _run_text( source_xml = "<dummy/>", transform_xml = transform, # modifies the defaults parameters = {'x': 3, 'y': 4}, expected = "A(3,4) = 125", )
def test_xslt_debug_1_mn_20001128(): transform = """<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method="text"/> <xsl:key name="elements" match="*" use="name()"/> <xsl:key name="attributes" match="@*" use="concat(name(parent::*),':::',name())"/> <xsl:key name="allSameAttributes" match="@*" use="name(parent::*)"/> <xsl:template match = "*" > <xsl:if test="generate-id() = generate-id(key('elements',name()))"> <xsl:text>
</xsl:text> <xsl:value-of select="name()"/> <xsl:apply-templates select="key('allSameAttributes',name())"> <xsl:sort select='name()'/> </xsl:apply-templates> </xsl:if> <xsl:apply-templates/> </xsl:template> <xsl:template match="@*"> <xsl:if test="position()=1"> <xsl:text> [ </xsl:text> </xsl:if> <xsl:if test="generate-id() = generate-id(key('attributes',concat(name(parent::*),':::',name())))"> <xsl:value-of select="name()"/> <xsl:text> </xsl:text> </xsl:if> <xsl:if test="position()=last()"> <xsl:text> ] </xsl:text> </xsl:if> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>""" expected = """ slideshow title metadata speaker jobTitle organization presentationDate presentationLocation occasion slideset slide [ id ] item speakerNote emphasis [ role ] heading bulletlist preformatted graphic [ file height width ] link [ href ] para""" _run_text( source_xml = SLIDES4SVG_XML, transform_xml = transform, expected = expected, )