Example #1
0
class test_borrowed_cases(xslt_test):
    #Bug report by Robert Świętochowski
    #http://lists.fourthought.com/pipermail/4suite/2006-September/008025.html
    source = stringsource(
        "<doc><a>2005-05-04T23:00:00+02:00</a><b>2005-05-05T01:00:00+02:00</b></doc>"
    )
    transform = stringsource("""<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:date="http://exslt.org/dates-and-times"
  >

  <xsl:template match="/">
    <result><xsl:value-of select="date:difference(a,b)"/>|<xsl:value-of select="date:difference(b,a)"/></result>
  </xsl:template>

</xsl:stylesheet>
""")
    expected = stringsource("<result>A|-A</result>")

    def test_transform_output(self):
        from amara.xslt import transform
        io = cStringIO.StringIO()
        result = transform(self.source, self.transform, output=io)
        self.assert_(treecompare.xml_compare(self.expected, io.getvalue()))
        return
Example #2
0
    def test_transform(self):
        SIZE = 10

        import sys
        from amara.xslt import transform

        #Create the matrix to be transposed
        from Ft.Xml.Domlette import implementation
        doc = implementation.createDocument(None, 'table', None)
        counter = 1
        for row in range(SIZE):
            row_elem = doc.createElementNS(None, 'row')
            doc.documentElement.appendChild(row_elem)
            for col in range(SIZE):
                col_elem = doc.createElementNS(None, 'column')
                row_elem.appendChild(col_elem)
                content = doc.createTextNode(str(counter))
                col_elem.appendChild(content)
                counter = counter + 1

        stream = cStringIO.StringIO()
        from Ft.Xml.Domlette import Print
        Print(doc, stream)

        self.source = stringsource(stream.getvalue())
        result = transform(self.source, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
Example #3
0
 def test_transform(self):
     # Subsequent, read file and compare
     from amara.xslt import transform
     self.assert_(os.path.exists(fname))
     file = open(fname, 'r')
     fcontent = file.read()
     file.close()
     self.assert_(treecompare.html_compare(self.expected, fcontent))
     os.unlink(fname)
     
     # Re-populate file, with indented HTML
     io = cStringIO.StringIO()
     self.transform = commontransform%(furi, "yes")
     result = transform(stringsource(self.source), stringsource(self.transform), output=io)
     open(fname, 'w').write(io.getvalue())
     self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
Example #4
0
class test_xslt_text_output_2_fc_20001125(xslt_test):
    source = stringsource(
        """<?xml version="1.0"?>\012<?xml-stylesheet href="book.html.xsl" type="text/xsl"?>\012<!DOCTYPE Book [\012<!ELEMENT Book (Title, Chapter+)>\012<!ATTLIST Book Author CDATA #REQUIRED>\012<!ELEMENT Title (#PCDATA)>\012<!ELEMENT Chapter (#PCDATA)>\012<!ATTLIST Chapter id CDATA #REQUIRED>\012]>\012\012<Book Author="\351\231\263\345\273\272\345\213\263">\012 <Title>\344\270\200\346\234\254\346\233\270</Title>\012  <Chapter id="1">\012    \351\200\231\346\230\257\347\254\254\344\270\200\347\253\240. &#24179;\012  </Chapter>\012  <Chapter id="2">\012    \351\200\231\346\230\257\347\254\254\344\272\214\347\253\240. &#24179;\012  </Chapter>\012</Book>"""
    )
    transform = commontransform
    parameters = {}
    expected = commonexpected
Example #5
0
    def test_transform(self):
        SIZE=10
        
        import sys
        from amara.xslt import transform

        #Create the matrix to be transposed
        from Ft.Xml.Domlette import implementation
        doc = implementation.createDocument(None, 'table', None)
        counter = 1
        for row in range(SIZE):
            row_elem = doc.createElementNS(None, 'row')
            doc.documentElement.appendChild(row_elem)
            for col in range(SIZE):
                col_elem = doc.createElementNS(None, 'column')
                row_elem.appendChild(col_elem)
                content = doc.createTextNode(str(counter))
                col_elem.appendChild(content)
                counter = counter + 1

        stream = cStringIO.StringIO()
        from Ft.Xml.Domlette import Print
        Print(doc,stream)

        self.source = stringsource(stream.getvalue())
        result = transform(self.source, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
Example #6
0
 def test_transform(self):
     # Preliminary, populate file with unindented HTML
     from amara.xslt import transform
     io = cStringIO.StringIO()
     self.transform = stringsource(commontransform%(furi, "no"))
     result = transform(self.source, self.transform, output=io)
     self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
     open(fname, 'w').write(io.getvalue())
Example #7
0
 def test_transform(self):
     # Preliminary, populate file with unindented HTML
     from amara.xslt import transform
     io = cStringIO.StringIO()
     self.transform = stringsource(commontransform % (furi, "no"))
     result = transform(self.source, self.transform, output=io)
     self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
     open(fname, 'w').write(io.getvalue())
Example #8
0
    def test_transform(self):
        # Subsequent, read file and compare
        from amara.xslt import transform
        self.assert_(os.path.exists(fname))
        file = open(fname, 'r')
        fcontent = file.read()
        file.close()
        self.assert_(treecompare.html_compare(self.expected, fcontent))
        os.unlink(fname)

        # Re-populate file, with indented HTML
        io = cStringIO.StringIO()
        self.transform = commontransform % (furi, "yes")
        result = transform(stringsource(self.source),
                           stringsource(self.transform),
                           output=io)
        open(fname, 'w').write(io.getvalue())
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
Example #9
0
class test_xslt_text_output_1_fc_20001125(xslt_test):
    source = stringsource("""<?xml version="1.0"?>
<?xml-stylesheet href="book.html.xsl" type="text/xsl"?>
<!DOCTYPE Book [
<!ELEMENT Book (Title, Chapter+)>
<!ATTLIST Book Author CDATA #REQUIRED>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Chapter (#PCDATA)>
<!ATTLIST Chapter id CDATA #REQUIRED>
]>

<Book Author="³¯«Ø¾±">
 <Title>¤@¥»®Ñ</Title>
  <Chapter id="1">
    ³o¬O²Ä¤@³¹. &#24179;
  </Chapter>
  <Chapter id="2">
    ³o¬O²Ä¤G³¹. &#24179;
  </Chapter>
</Book>""")
    transform = commontransform
    parameters = {}
    expected = commonexpected

    def test_transform(self):

        tester.startTest("Checking for BIG5 codec")
        try:
            import codecs
            big5_decoder = codecs.getdecoder('big5')
        except LookupError:
            try:
                from encodings import big5
            except ImportError:
                tester.warning(
                    "No BIG5 encoding support for case 1.  You can install \n"
                    "BIG5 by downloading and installing ChineseCodes from\n"
                    "ftp://python-codecs.sourceforge.net/pub/python-codecs/")
                tester.testDone()
                return
            else:
                big5_decode = big5.decode
        else:
            big5_decode = lambda s: big5_decoder(s)[0]
        tester.testDone()

        b5 = big5_decode(self.source)
        utf8 = b5.encode("utf-8")

        from amara.xslt import transform
        io = cStringIO.StringIO()
        result = transform(utf8, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
Example #10
0
class test_xslt_docbook_ss_20010301(xslt_test):
    source = stringsource("""\
<book>
  <chapter>
    <title>Chapter</title>
    <sect1>
      <title>Sect1</title>
      <sect2>
        <title>Sect2</title>
      </sect2>
    </sect1>
  </chapter>
</book>
""")
    transform = ""
    parameters = {}
    expected = ""
Example #11
0
class test_xslt_row_num_km_20000207(xslt_test):
    source = ""
    transform = stringsource("""<xsl:transform
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="1.0"
>

<xsl:output method='html'/>

<xsl:template match="/">
  <table>
    <xsl:apply-templates/>
  </table>
</xsl:template>

<xsl:template match="table">
    <xsl:call-template name="one-row">
      <xsl:with-param name="row-num" select="1"/>
    </xsl:call-template>
</xsl:template>

<!-- From Michael Kay -->
<xsl:template name="one-row">
  <xsl:param name="row-num" select="1"/>
  <tr>
  <xsl:for-each select="row">
     <td><xsl:value-of select="*[$row-num]"/></td>
  </xsl:for-each>
  </tr>
  <xsl:if test="row/*[$row-num+1]">
    <xsl:call-template name="one-row">
      <xsl:with-param name="row-num" select="$row-num+1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>
<!-- END From Michael Kay -->

</xsl:transform>""")
    parameters = {}
    expected = """<table>
  <tr>
    <td>1</td>
    <td>11</td>
    <td>21</td>
    <td>31</td>
    <td>41</td>
    <td>51</td>
    <td>61</td>
    <td>71</td>
    <td>81</td>
    <td>91</td>
  </tr>
  <tr>
    <td>2</td>
    <td>12</td>
    <td>22</td>
    <td>32</td>
    <td>42</td>
    <td>52</td>
    <td>62</td>
    <td>72</td>
    <td>82</td>
    <td>92</td>
  </tr>
  <tr>
    <td>3</td>
    <td>13</td>
    <td>23</td>
    <td>33</td>
    <td>43</td>
    <td>53</td>
    <td>63</td>
    <td>73</td>
    <td>83</td>
    <td>93</td>
  </tr>
  <tr>
    <td>4</td>
    <td>14</td>
    <td>24</td>
    <td>34</td>
    <td>44</td>
    <td>54</td>
    <td>64</td>
    <td>74</td>
    <td>84</td>
    <td>94</td>
  </tr>
  <tr>
    <td>5</td>
    <td>15</td>
    <td>25</td>
    <td>35</td>
    <td>45</td>
    <td>55</td>
    <td>65</td>
    <td>75</td>
    <td>85</td>
    <td>95</td>
  </tr>
  <tr>
    <td>6</td>
    <td>16</td>
    <td>26</td>
    <td>36</td>
    <td>46</td>
    <td>56</td>
    <td>66</td>
    <td>76</td>
    <td>86</td>
    <td>96</td>
  </tr>
  <tr>
    <td>7</td>
    <td>17</td>
    <td>27</td>
    <td>37</td>
    <td>47</td>
    <td>57</td>
    <td>67</td>
    <td>77</td>
    <td>87</td>
    <td>97</td>
  </tr>
  <tr>
    <td>8</td>
    <td>18</td>
    <td>28</td>
    <td>38</td>
    <td>48</td>
    <td>58</td>
    <td>68</td>
    <td>78</td>
    <td>88</td>
    <td>98</td>
  </tr>
  <tr>
    <td>9</td>
    <td>19</td>
    <td>29</td>
    <td>39</td>
    <td>49</td>
    <td>59</td>
    <td>69</td>
    <td>79</td>
    <td>89</td>
    <td>99</td>
  </tr>
  <tr>
    <td>10</td>
    <td>20</td>
    <td>30</td>
    <td>40</td>
    <td>50</td>
    <td>60</td>
    <td>70</td>
    <td>80</td>
    <td>90</td>
    <td>100</td>
  </tr>
</table>"""

    def test_transform(self):
        SIZE = 10

        import sys
        from amara.xslt import transform

        #Create the matrix to be transposed
        from Ft.Xml.Domlette import implementation
        doc = implementation.createDocument(None, 'table', None)
        counter = 1
        for row in range(SIZE):
            row_elem = doc.createElementNS(None, 'row')
            doc.documentElement.appendChild(row_elem)
            for col in range(SIZE):
                col_elem = doc.createElementNS(None, 'column')
                row_elem.appendChild(col_elem)
                content = doc.createTextNode(str(counter))
                col_elem.appendChild(content)
                counter = counter + 1

        stream = cStringIO.StringIO()
        from Ft.Xml.Domlette import Print
        Print(doc, stream)

        self.source = stringsource(stream.getvalue())
        result = transform(self.source, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
Example #12
0
class test_xslt_call_template_ed_20010101(xslt_test):
    source = stringsource("""<?xml version = '1.0'?>
<ROWSET>
   <ROW num="1">
      <ENAME>CLARK</ENAME>
      <SAL>2450</SAL>
   </ROW>
   <ROW num="2">
      <ENAME>KING</ENAME>
      <SAL>3900</SAL>
   </ROW>
   <ROW num="3">
      <ENAME>MILLER</ENAME>
      <SAL>1300</SAL>
   </ROW>
</ROWSET>
""")
    transform = stringsource('''<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:Color="http://www.oracle.com/XSL/Transform/java/java.awt.Color"
   xmlns:Integer="http://www.oracle.com/XSL/Transform/java/java.lang.Integer"
   exclude-result-prefixes="Color Integer">

 <xsl:output media-type="image/svg"/>

 <xsl:template match="/">
 <svg xml:space="preserve" width="1000" height="1000">
   <desc>Salary Chart</desc>
   <g style="stroke:#000000;stroke-width:1;font-family:Arial;font-size:16">
     <xsl:for-each select="ROWSET/ROW">
       <xsl:call-template name="drawBar">
         <xsl:with-param name="rowIndex" select="position()"/>
         <xsl:with-param name="ename" select="ENAME"/>
         <xsl:with-param name="sal" select="number(SAL)"/>
       </xsl:call-template>
     </xsl:for-each>
   </g>
 </svg>
 </xsl:template>

 <xsl:template name="drawBar">
   <xsl:param name="rowIndex" select="number(0)"/>
   <xsl:param name="ename"/>
   <xsl:param name="sal" select="number(0)"/>

   <xsl:variable name="xOffset"   select="number(100)"/>
   <xsl:variable name="yOffset"   select="number(20)"/>
   <xsl:variable name="barHeight" select="number(25)"/>
   <xsl:variable name="gap"       select="number(10)"/>

   <xsl:variable name="x" select="$xOffset"/>
   <xsl:variable name="y" select="$yOffset + $rowIndex * ($barHeight + $gap)"/>
   <xsl:variable name="barWidth" select="$sal div number(10)"/>
   <rect x="{$x}" y="{$y}" height="{$barHeight}" width="{$barWidth}">
     <xsl:attribute name="style">
       <xsl:text>fill:#</xsl:text>
       <xsl:call-template name="getCoolColorStr" xml:space="default">
         <xsl:with-param name="colorIndex" select="$rowIndex"/>
         <xsl:with-param name="totalColors" select="number(14)"/>
       </xsl:call-template>
       <xsl:text> </xsl:text>
     </xsl:attribute>
   </rect>
   <xsl:variable name="fontHeight" select="number(18)"/>
   <text x="20" y="{$y + $fontHeight}">
     <xsl:value-of select="$ename"/>
   </text>
   <xsl:variable name="x2" select="$xOffset + $barWidth + 10"/>
   <text x="{$x2}" y="{$y + $fontHeight}">
     <xsl:value-of select="$sal"/>
   </text>
  </xsl:template>

  <xsl:template name="getCoolColorStr">
    <xsl:param name="colorIndex"/>
    <xsl:param name="totalColors"/>

    <xsl:variable name="SATURATION" select="number(0.6)"/>
    <xsl:variable name="BRIGHTNESS" select="number(0.9)"/>

    <xsl:variable name="hue" select="$colorIndex div $totalColors"/>
    <xsl:variable name="c"   select="Color:getHSBColor($hue, $SATURATION, $BRIGHTNESS)"/>
    <xsl:variable name="r"   select="Color:getRed($c)"/>
    <xsl:variable name="g"   select="Color:getGreen($c)"/>
    <xsl:variable name="b"   select="Color:getBlue($c)"/>
    <xsl:variable name="rs"  select="Integer:toHexString($r)"/>
    <xsl:variable name="gs"  select="Integer:toHexString($g)"/>
    <xsl:variable name="bs"  select="Integer:toHexString($b)"/>
    <xsl:if test="$r &lt; 16">0</xsl:if><xsl:value-of select="$rs"/>
    <xsl:if test="$g &lt; 16">0</xsl:if><xsl:value-of select="$gs"/>
    <xsl:if test="$b &lt; 16">0</xsl:if><xsl:value-of select="$bs"/>
  </xsl:template>

</xsl:stylesheet>
''')
    parameters = {}
    expected = """<?xml version='1.0' encoding='UTF-8'?>
Example #13
0
from amara.lib import treecompare
from amara.test import test_main
from amara.test.xslt import xslt_test, filesource, stringsource

commontransform = stringsource("""<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0"
>

  <xsl:output encoding="UTF-8"/>
  
  <xsl:template match="Book">
    <html>
      <head>
        <title><xsl:value-of select="Title" /></title>
      </head>
      <body>
        <ul>
        <li><xsl:value-of select="Chapter" /></li>
        <li><xsl:value-of select="Chapter" /></li>
        </ul>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>""")

commonexpected = """<html>\012  <head>\012    <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>\012    <title>\344\270\200\346\234\254\346\233\270</title>\012  </head>\012  <body>\012    <ul>\012      <li>\012    \351\200\231\346\230\257\347\254\254\344\270\200\347\253\240. \345\271\263\012  </li>\012      <li>\012    \351\200\231\346\230\257\347\254\254\344\270\200\347\253\240. \345\271\263\012  </li>\012    </ul>\012  </body>\012</html>"""

Example #14
0
from amara.test import test_main
from amara.test.xslt import xslt_test, filesource, stringsource

commontransform = stringsource(
    """<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0"
>

  <xsl:output encoding="UTF-8"/>
  
  <xsl:template match="Book">
    <html>
      <head>
        <title><xsl:value-of select="Title" /></title>
      </head>
      <body>
        <ul>
        <li><xsl:value-of select="Chapter" /></li>
        <li><xsl:value-of select="Chapter" /></li>
        </ul>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>"""
)

commonexpected = """<html>\012  <head>\012    <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>\012    <title>\344\270\200\346\234\254\346\233\270</title>\012  </head>\012  <body>\012    <ul>\012      <li>\012    \351\200\231\346\230\257\347\254\254\344\270\200\347\253\240. \345\271\263\012  </li>\012      <li>\012    \351\200\231\346\230\257\347\254\254\344\270\200\347\253\240. \345\271\263\012  </li>\012    </ul>\012  </body>\012</html>"""
Example #15
0
Output
^^^^^^
&#195;&#169;e
"""

import os
import cStringIO
import unittest

from amara.lib import treecompare
from amara.test import test_main
from amara.test.xslt import xslt_test, filesource, stringsource


commonsource = stringsource("""<?xml version="1.0"?>
<toto/>
""")

class test_xslt_default_utf8_encoding_da_20000714(xslt_test):
    source = commonsource
    # This is non-well-formed XML.
    #In Python 1.52 plus XML-SIG, expat seems to deal with it by passing the data to us verbatim.
    #In Python 2.0, expat seems to silently ignore the element content with unrecognizable text
    transform = stringsource("""<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="toto">
ée
</xsl:template>
</xsl:stylesheet>""")
    expected ="""<?xml version="1.0" encoding="UTF-8"?>