开发者

"Expected QName" - Trying to create element from attribute value

Trying to transform a simple XML document with an XSL stylesheet. I'm using XMLspy right now, but with the end goal of a browswer.

The XML:

<doc>


    <str name="organizationName">Me</str>
    <str name="rights">BY-NC</str>
    <str name="date">2011-05-23</str>
    <str name="type">Collection</str>
    <bool name="collectionPubliclyVisible">true</bool>
    <str name="publisher">Pub</str>
    <str name="creator">Me</str>
    <long name="id">2656</long>
    <int name="rank">2</int&g开发者_如何转开发t;
    <str name="contributor">ME</str>
    <str name="description">This Collection archives 900+ feeds from the network of US based NOAA observation stations recording current climatic conditions, in addition to a daily constructed XML Zip file generated by NOAA.</str>
    <str name2="name">NOAA - XML Feeds of Observed Current Conditions</str>
    <date name="updated_dt">2011-06-03T21:04:56Z</date>
    <str name="relation"/>
    <str name="format">zip</str>
    <date name="created_dt">2011-05-31T22:36:07Z</date>
    <date name="timestamp">2011-06-17T21:54:24.116Z</date>
</doc>

The XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:element name="metadata">
<xsl:apply-templates select="doc"/>
</xsl:element>
</xsl:template>

<xsl:template match="doc">
<xsl:apply-templates select="child::node()"/>
</xsl:template>

<xsl:template match="child::node()">
<element name="{@name}" xmlns="http://www.w3.org/1999/XSL/Transform">
<xsl:value-of select="."/>
</element>
</xsl:template>


</xsl:stylesheet>

Many thanks, I've been at this for hours to no avail.

-Graham


You are trying to create output elements using name attribute:

<element name="{@name}" xmlns="http://www.w3.org/1999/XSL/Transform">

but looking into your input XML document there is one element with name2 attribute, which causes error in your transformation.

<str name2="name">NOAA - XML Feeds of Observed Current Conditions</str>

I am not sure what XML you expect on output, however you can try with this XSL:

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

    <xsl:template match="/">
        <metadata>
            <xsl:apply-templates select="doc/*"/>
        </metadata>
    </xsl:template>

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

Result XML:

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
   <organizationName>Me</organizationName>
   <rights>BY-NC</rights>
   <date>2011-05-23</date>
   <type>Collection</type>
   <collectionPubliclyVisible>true</collectionPubliclyVisible>
   <publisher>Pub</publisher>
   <creator>Me</creator>
   <id>2656</id>
   <rank>2</rank>
   <contributor>ME</contributor>
   <description>This Collection archives 900+ feeds from the network of US based NOAA
        observation stations recording current climatic conditions, in addition to a daily
        constructed XML Zip file generated by NOAA.</description>
   <name>NOAA - XML Feeds of Observed Current Conditions</name>
   <updated_dt>2011-06-03T21:04:56Z</updated_dt>
   <relation/>
   <format>zip</format>
   <created_dt>2011-05-31T22:36:07Z</created_dt>
   <timestamp>2011-06-17T21:54:24.116Z</timestamp>
</metadata>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜