Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3697630
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:52:59+00:00 2026-05-19T04:52:59+00:00

I am using lxml 2.2.8 and trying to transform some existing html files into

  • 0

I am using lxml 2.2.8 and trying to transform some existing html files into django templates.
the only problem that i am having is that lxml urlencodes the anchor name and href attributes.
for example:

<xsl:template match="a">
<!-- anchor attribute href is urlencoded but the title is escaped -->
<a href="{{{{item.get_absolute_url}}}}" title="{{{{item.title}}}}">
    <!-- name tag is urlencoded -->
    <xsl:attribute name="name">{{item.name}}</xsl:attribute>
    <!-- but other attributes are not -->
    <xsl:attribute name="nid">{{item.nid}}</xsl:attribute>
    <xsl:attribute name="class">{{item.class_one}}</xsl:attribute>
    <xsl:apply-templates/>
</a>

produces html like this:

<a href="%7B%7Bitem.get_absolute_url%7D%7D"  
   title="{{item.title}}" name="%7B%7Bitem.name%7D%7D" 
   nid="{{item.nid}}" class="{{item.class_one}}">more info</a>

what I am trying for is this:

<a href="{{item.get_absolute_url}}">more info</a>

is there a way to disable the (automatic) urlencoding that lxml is doing?

here is (basically) the code I am using to generate and parse the file:

from lxml import etree, html
from StringIO import StringIO

doc = StringIO(
'''<html>
<head>
    <title>An experiment</title>
</head>
<body>
<p class="one">This is an interesting paragraph detailing the inner workings of something</p>
<p class="two">paragraph with <a href="/link/to/more">more info</a></p>
<p>posted by: me</p>
</body>
</html>''')

stylesheet = StringIO(
'''<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xhtml xsl">
<xsl:template match="p[@class='one']">
    <xsl:copy>
        <!-- when adding an attribute with the xsl:attribute tag -->
        <!-- the curly braces are not escaped, ie you dont have  -->
        <!-- to double them up -->
        <xsl:attribute name="class">{{item.class_one}}</xsl:attribute>
        <xsl:attribute name="nid">{{item.nid}}</xsl:attribute>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="p[@class='two']">
    <!-- but double 'em up in this instance -->
    <p class="{{{{item.class_two}}}}">
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="a">
    <!-- anchor attribute href is urlencoded but the title is escaped -->
    <a href="{{{{item.get_absolute_url}}}}" title="{{{{item.title}}}}">
        <!-- name tag is urlencoded -->
        <xsl:attribute name="name">{{item.name}}</xsl:attribute>
        <!-- but oher attributes are not -->
        <xsl:attribute name="nid">{{item.nid}}</xsl:attribute>
        <xsl:attribute name="class">{{item.class_one}}</xsl:attribute>
        <xsl:apply-templates/>
    </a>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>
''')
def parse_doc():
    xsl = etree.parse(stylesheet)
    trans = etree.XSLT(xsl)
    root = html.parse(doc, etree.HTMLParser(encoding="windows-1252"))
    transformed = trans(root)
    print html.tostring(transformed)

if __name__ == '__main__':
    parse_doc()

with the exception that these files are all malformed html 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:53:00+00:00Added an answer on May 19, 2026 at 4:53 am

    Maybe you can use the XML instead of the HTML serializer.

    >>> from lxml import etree, html
    >>> 
    >>> t = etree.XML('<a href="{{x}}" />')
    >>> 
    >>> etree.tostring(t)
    '<a href="{{x}}"/>'
    >>> html.tostring(t)
    '<a href="%7B%7Bx%7D%7D"></a>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some tests with Lettuce , started using lxml but found
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
I am trying to get some content in html documents. Some of the documents
I'm trying to do this (using lxml): //*[@id=32808345 or @id=33771423 or @id=15929470 or @id=33771117
I am trying to print a XML file using lxml and Python. Here is
I'm trying to parse an xml file using lxml. xml.etree allowed me to simply
I'm trying to create an xml entry that looks like this using python and
I am trying to output a XML file using Python and lxml However, I
I'm trying to bundle some egg dependencies when using py2exe and as stated on
I'm trying to create XML Schema using lxml. For the begining something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.