我有以下XML文件
<?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>
我想将其转换为以下格式
<?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>
你能为我提供一个 XSLT 转换来实现这个目标吗?
答案 0 :(得分:3)
这应该对你有用
<?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>
答案 1 :(得分:0)
这不是有效的xml文件... <email>
等没有结束标记。您可能需要手动进行字符串处理。 Perl很擅长这个。