“预期的QName” - 尝试从属性值创建元素

时间:2011-06-28 00:13:59

标签: xslt namespaces

尝试使用XSL样式表转换简单的XML文档。我现在正在使用XMLspy,但最终的目标是浏览器。

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>
    <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>

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>

非常感谢,我已经在这几个小时都无济于事。

-Graham

1 个答案:

答案 0 :(得分:1)

您正尝试使用name属性创建输出元素:

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

但是查看输入XML文档时,有一个元素具有name2属性,这会导致转换错误。

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

我不确定您在输出上期望的XML,但是您可以尝试使用此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>

结果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>