2011-06-28 15 views
2

嘗試使用XSL樣式錶轉換簡單的XML文檔。我現在正在使用XMLspy,但是最終目標是瀏覽器。「預期的QName」 - 嘗試從屬性值創建元素

的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

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

+1這更有意義。 –

+0

你是絕對正確的,非常感謝你。 –

相關問題