lxml and xslt extensions

Discussion in 'Python' started by dasacc22, Apr 4, 2009.

  1. dasacc22

    dasacc22 Guest

    Hi,

    Im not sure where else to ask this. But basically Im having trouble
    figuring out how to successfully apply multiple extensions in a single
    transformation. So for example if i have
    <xsl:stylesheet .../>
    <xsl:template...>
    <my:tag/>
    <my:tag/>
    </xsl:template>
    </xsl:stylesheet>

    in my xsl and my xslt extension looks like

    class TagExtension(etree.XSLTExtension):
    def execute( ..., output_parent):
    print 'executing tag_extension'
    tag = etree.Element('p')
    tag.text = 'Hello'
    output_parent.append(tag)

    well then the transformation works for the first tag and appends it to
    the root of the created doc but all subsequent calls dont append
    (maybe b/c output_parent is now somewhere else for return?). And to
    clarify, I know that its the first call that completes and all
    subsequent calls fail b/c i have a subsequent call that performs a
    different transformation.

    Thanks for any help or hints,
    Daniel
     
    dasacc22, Apr 4, 2009
    #1
    1. Advertising

  2. dasacc22

    dasacc22 Guest

    On Apr 4, 11:31 am, dasacc22 <> wrote:
    > Hi,
    >
    > Im not sure where else to ask this. But basically Im having trouble
    > figuring out how to successfully apply multiple extensions in a single
    > transformation. So for example if i have
    > <xsl:stylesheet .../>
    > <xsl:template...>
    > <my:tag/>
    > <my:tag/>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > in my xsl and my xslt extension looks like
    >
    > class TagExtension(etree.XSLTExtension):
    >   def execute( ..., output_parent):
    >     print 'executing tag_extension'
    >     tag = etree.Element('p')
    >     tag.text = 'Hello'
    >     output_parent.append(tag)
    >
    > well then the transformation works for the first tag and appends it to
    > the root of the created doc but all subsequent calls dont append
    > (maybe b/c output_parent is now somewhere else for return?). And to
    > clarify, I know that its the first call that completes and all
    > subsequent calls fail b/c i have a subsequent call that performs a
    > different transformation.
    >
    > Thanks for any help or hints,
    > Daniel


    Oh well I found the culprit, etree.tostring seems to cut it off after
    the first transformation, simply doing a

    $> print result

    displays the entire document. To make use of the keyword options
    xml_declaration, pretty_print, encoding, I tried using the .write
    method of result to a StringIO but it produces the same clipped
    result. Guess Ill have to edit the .docinfo attributes on the result
    and return the string
     
    dasacc22, Apr 4, 2009
    #2
    1. Advertising

  3. Hi,

    dasacc22 wrote:
    > On Apr 4, 11:31 am, dasacc22 wrote:
    >> Im not sure where else to ask this.


    The best place to ask is the lxml mailing list:

    http://codespeak.net/mailman/listinfo/lxml-dev


    >> But basically Im having trouble
    >> figuring out how to successfully apply multiple extensions in a single
    >> transformation. So for example if i have
    >> <xsl:stylesheet .../>
    >> <xsl:template...>
    >> <my:tag/>
    >> <my:tag/>
    >> </xsl:template>
    >> </xsl:stylesheet>
    >>
    >> in my xsl and my xslt extension looks like
    >>
    >> class TagExtension(etree.XSLTExtension):
    >> def execute( ..., output_parent):
    >> print 'executing tag_extension'
    >> tag = etree.Element('p')
    >> tag.text = 'Hello'
    >> output_parent.append(tag)
    >>
    >> well then the transformation works for the first tag and appends it to
    >> the root of the created doc but all subsequent calls dont append
    >> (maybe b/c output_parent is now somewhere else for return?). And to
    >> clarify, I know that its the first call that completes and all
    >> subsequent calls fail b/c i have a subsequent call that performs a
    >> different transformation.

    >
    > Oh well I found the culprit, etree.tostring seems to cut it off after
    > the first transformation,


    Not sure what you mean here. Could you post your question on the lxml
    mailing list and add a runnable example that shows the behaviour?


    > simply doing a
    >
    > $> print result
    >
    > displays the entire document. To make use of the keyword options
    > xml_declaration, pretty_print, encoding, I tried using the .write
    > method of result to a StringIO but it produces the same clipped
    > result. Guess Ill have to edit the .docinfo attributes on the result
    > and return the string


    ..docinfo is basically read-only. The result of the XSLT is serialised as
    requested by the xsl:eek:utput element in the stylesheet.

    Stefan
     
    Stefan Behnel, Apr 4, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Srijit Kumar Bhadra

    lxml and SimpleXMLWriter

    Srijit Kumar Bhadra, Jul 1, 2006, in forum: Python
    Replies:
    2
    Views:
    477
    Stefan Behnel
    Jul 6, 2006
  2. Chas Emerick

    lxml/ElementTree and .tail

    Chas Emerick, Nov 15, 2006, in forum: Python
    Replies:
    30
    Views:
    1,311
    Damjan
    Nov 19, 2006
  3. Laurent Pointal

    lxml and identification of elements in source

    Laurent Pointal, Sep 19, 2007, in forum: Python
    Replies:
    1
    Views:
    377
    Stefan Behnel
    Sep 20, 2007
  4. Silfheed

    CDATA and lxml

    Silfheed, Apr 11, 2008, in forum: Python
    Replies:
    5
    Views:
    383
    Silfheed
    Apr 11, 2008
  5. Adam Tauno Williams

    etree/lxml/XSLT and dynamic stylesheet variables

    Adam Tauno Williams, Jan 20, 2012, in forum: Python
    Replies:
    0
    Views:
    198
    Adam Tauno Williams
    Jan 20, 2012
Loading...

Share This Page