1 # Copyright (C) 2013 Stefan Merten
3 # This file is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published
5 # by the Free Software Foundation; either version 2 of the License,
6 # or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 Parse and produce result string by XSLT.
22 __docformat__
= 'reStructuredText' # Formatted to be rendered by epydoc
24 ###############################################################################
25 ###############################################################################
28 import docutils
.parsers
30 from lxml
import etree
32 ###############################################################################
33 ###############################################################################
36 ###############################################################################
37 ###############################################################################
40 ###############################################################################
41 ###############################################################################
44 ###############################################################################
45 ###############################################################################
48 class XsltParser(docutils
.parsers
.Parser
):
50 Parses XML input by XSLT and stores the result in the attribute
51 `xslt_result` of the document. Works together with `XsltWriter`.
54 def __init__(self
, xsltPath
):
56 See instance variables for parameter documentation.
58 self
.xsltPath
= xsltPath
60 Path to the XSLT to use.
64 :type: Return type of `etree.XSLT`()
66 The XSLT to use for parsing.
71 xsltF
= open(self
.xsltPath
)
73 raise Exception("Can't open main XSLT file %r: %s"
74 % ( self
.xsltPath
, e
, ))
76 # Parse and prepare XSLT
78 xsltDoc
= etree
.parse(xsltF
)
80 raise Exception("Error parsing main XSLT file %r: %s"
81 % ( self
.xsltPath
, e
, ))
83 self
.xslt
= etree
.XSLT(xsltDoc
)
85 def parse(self
, inputstring
, document
):
86 self
.setup_parse(inputstring
, document
)
87 inDoc
= etree
.fromstring(inputstring
)
88 document
.xslt_result
= self
.xslt(inDoc
, sourceName
="'%s'"
89 % ( document
.current_source
, ))
90 if self
.xslt
.error_log
:
91 document
.reporter
.error(self
.xslt
.error_log
)