Is it possible to twist the `xml.etree.ElementTree` parser to accept WebForms .aspx syntax?

3 weeks ago 28
ARTICLE AD BOX

(This is probably for those of you with deeper knowledge of Python's xml.etree.ElementTree implementation.)

I am writing a special purpose tool for processing ASP.NET WebSite source files of our project with extensions .aspx and .ascx. The goal is to manipulate the XML/HTML like fragments. As I am fluent in both Python and using the xml.etree.ElementTree, I have started to parse the .aspx souces to see what show-stoppers should be solved. The problem is the special .aspx syntax adds the things that are not part of pure HTML/XML files.

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" Inherits="Company" Codebehind="Company.aspx.cs" %> <%@ Register Src="~/UserControls/WhateverControl.ascx" TagPrefix="uc" TagName="WhateverControl" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> <style> .panelClub, .panelXxxx { border-style: solid; border-width: 1px; border-color: black; background-color: white; padding-top: 5px; padding-bottom: 5px; text-align: center; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="content" runat="Server"> <dx:ASPxRoundPanel ID="panelCompany" runat="server" HeaderText="Company" Width="100%" OnInit="panelCompany_Init"> <HeaderStyle Font-Bold="True" Font-Size="12pt" /> <PanelCollection> <dx:PanelContent runat="server"> ... <dx:ASPxLabel ID="labDetail" runat="server" Text='<%# FormatHtmlDetail(Eval("custcode")) %>' Width="100%"> </dx:ASPxLabel>

Basically, I need produce new version of the same file with added attributes of elements, and extract some information.

The problem is that <%@ and <%# marks are not used in XML/HTML, and the parser complains when they appear. I have worked around the initial directive lines (<%@); however, it still does not help to solve the HTMLish WebForm attributes like Text='<%# FormatHtmlDetail(Eval("custcode")) %>'. (It uses single quoting because of the double quotes used inside, so the parser chokes.)

Is there a way of twisting easily the Python xml.etree.ElementTree parser (rather easily) to adopt it for WebForms ASP.NET syntax?

The motivation: One of the main goals is to generate meta:resourcekey attributes and generate the resources. However, I would like to replace the Visual Studio functionality (Tools -- Generate Local Resource) with more programmatical solution with some extra functionality.

Read Entire Article