Converting XML elements to attributes

Posted on 2006-06-03
Last Modified: 2013-11-19
Hi there!

I am trying to convert the elements of an XML to attributes.

For example, the original XML looks like

<?xml version="1.0" encoding="ISO8859-1" ?>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tylor</ARTIST>
 I need the final one to look like:

"<CD TITLE="Empire Burlesque" ARTIST="Bob Dylan" COUNTRY="USA" COMPANY="Columbia" PRICE="10.90" YEAR="1985"></CD>
<CD TITLE ="Hide your heart" .. and so on"

I am using the following ASP:
'Load the XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false

'Load the XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false


And XSL:
<?xml version=`1.0`?>
 <xsl:template match="*">
 <xsl:for-each select="CD">
 <xsl:attribute name="{name(.)}"><xsl:value-of select="."/>
 <xsl:apply-templates select="*[* or @*]|text()"/>

But I get the following error:
"msxml3.dll error '80004005'

The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document.

/xsl/convert.asp, line 12"

Does anyone know what's wrong in the above code? Or do you happen to have another method to convert XML elements to attributes using either ASP or PHP and of course XSL?

Thank you,
Question by:mircea_a
    LVL 60

    Accepted Solution

    Hi Mircea,

    It is generally not a good idea to test your XSLT directly in an ASP.
    You should use a development environment for that
    (I use Oxygen, and Stylus Studio,

    The error message you get is because the stylesheet is not loaded in the stylesheet object.
    The reason for that could be
    - the stylesheet is not found at the location given
    - the stylesheet is not valid

    Your stylesheet has quiet some errors, so there is the reason
    to name a few
    - the XML declaration should have quotes around the version, or single quotes, not the fancy ones you use
    - you need a stylesheet element
    - strip-space is an element
    - indent should be in the xsl:output element

    having corrected that, your stylesheet will not work either
    I created a generic solution for your problem, that helps in this case

    <?xml version='1.0'?>
    <xsl:stylesheet xmlns:xsl="" version="1.0">
    <xsl:output  indent="yes" />
    <xsl:strip-space elements="*"/>
        <xsl:template match="node()[*[not(*)]]">
                <xsl:copy-of select="@*"/>
                <xsl:for-each select="*">
                    <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
       <xsl:template match="node()">
               <xsl:copy-of select="@*"/>
               <xsl:apply-templates select="node() | text()"/>

    I have two templates
    - One for elements whoms children don't have children (that is the one above leaf element, you can attributize)
    - one fallback for the other elements

    I just did this this morning. I am convinced you can find more robust solutions out there
    But this one will work for you


    Author Comment

    Hi Gertone!

    Thanks for your answer, this is very helpful.

    But, I really need to make it work from the ASP, or php.

    I have replaced my xsl with the one you provided and now I get the following error:
    "Switch from current encoding to specified encoding not supported. Error processing resource 'http://localhost/xsl/convert.a..."

    I think that the ASP has a different encoding than the XML, and I've been trying to find a solution but no success so far. Do you have any idea on how to make that xsl work through the asp?

    Thanks again,
    LVL 26

    Expert Comment

    Note the change to the ProgID:

    'Load the XML
    set xml = Server.CreateObject("Msxml2.DomDocument")
    xml.async = false

    'Load the XSL
    set xsl = Server.CreateObject("Msxml2.DomDocument")
    xsl.async = false

    ' This transforms directly to the response stream, using the IStream interface.  It will preserve encoding
    xml.transformNodeToObject(xsl, Response)

    You can modify the xsl:output statement in the XSLT to specify the same encoding you're using on your ASP page.

          <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    Mike Sharp
    LVL 60

    Expert Comment

    by:Geert Bormans

    > But, I really need to make it work from the ASP, or php.

    yes, I was not saying you should leave ASP
    I was suggesting that you test the XSLT _before_ you plug it in an ASP.
    After you know it works

    > Switch from current encoding to specified encoding not supported

    well, I have some observations
    - the encoding in your source might not be accepted or well understood by the msxml parser you are using
    the standardised encoding name for latin1 is <?xml version="1.0" encoding="ISO-8859-1" ?>
    (note the extra '-')
    I am not sure it would make a difference... but try it

    - you could add an output encoding in the XSLT
    change this line: <xsl:output  indent="yes" encoding="ISO-8859-1" />

    - in order to have the output encoding working, you should use the IStream interface for your output
    let us first see that the two earlier points don't help

    - you are using the old MicroSoft objects
    you should use the version 2 objects "Msxml2.DomDocument.4.0" or "Msxml2.DomDocument" if you don't want to bind to a specific version

    keep me posted


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now