开发者

Renaming XML elements using value of attributes

I have the below XML file

    <?xml version="1.0" encoding="iso-8859-1"?>
    <DATASET>
    <TYPE>success</TYPE>
    <RECORD>
        <DATA type="email">eeee@eee.net</DATA>
        <DATA type="name">somename</DATA>
        <DATA type="category">Other</DATA>
        <DATA type="uid">459d28cd11</DATA>
        <DATA type="state">bounced</DATA>
        <DATA type="statetype">Blocked</DATA>
        <DATA type="stateaction">Not Trashed</DATA>
        <DATA type="statetime">5-24-11 12:05 am PDT</DATA>
    </RECORD>
    <RECORD>
        <DATA type="email">bbbbb@eee.net</DATA>
        <DATA type="name">somename</DATA>
        <DATA type="category">Other</DATA>
        <DATA type="uid">0dcc42ebe3</DATA>
        <DATA type="state">bounced</DATA>
        <DATA type="statetype">Blocked</DATA>
        <DATA type="stateaction">Not Trashed</DATA>
        <DATA type="statetime">15-11-11 12:05 am PDT</DATA>
    </RECORD>
    </DATASET>

And I want to convert it into below format

    <?xml version="1.0" encoding="iso-8859-1"?>
    <DATASET>
    <TYPE>success</TYPE>
    <RECORD>
        <email>eeee@eee.net<开发者_运维问答;/email>
        <name>somename<name>
        <category>Other</category>
        <uid>459d28cd11</uid>
        <state>bounced</state>
        <statetype>Blocked</statetype>
        <stateaction>Not Trashed</stateaction>
        <statetime>5-24-11 12:05 am PDT</statetime>
    </RECORD>
    <RECORD>
        <email>bbbbb@eee.net</email>
        <name>somename<name>
        <category>Other</category>
        <uid>0dcc42ebe3</uid>
        <state>bounced</state>
        <statetype>Blocked</statetype>
        <stateaction>Not Trashed</stateaction>
        <statetime>15-11-11 12:05 am PDT</statetime>
    </RECORD>
    </DATASET>

Can you please provide me an XSLT transform to achieve this?


This should work for you

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="DATA">
        <xsl:element name="{@type}"><xsl:value-of select="."/></xsl:element>
    </xsl:template>
    </xsl:stylesheet>

For a quick explanation of how this works check out the guides by w3schools and webucator.


this isn't a valid xml file...there are no closing tags for <email>, etc. You'll probably need to do string processing manually. Perl is good at this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜