2011-02-11 64 views
0

這是我到目前爲止有:XSLT樣式表

< ?xml version="1.0"?> 
< xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
< xmlns:g="http://base.google.com/ns/1.0" version="1.0"> 
< xsl:output method="RSS 2.0"/> 
< xsl:template match="Description"> 
< title> 
< /title> 
< /xsl:template> 
< xsl:template match="Caption"> 
< description> 
< /description> 
< /xsl:template> 
< xsl:template match="Url> 
< link> 
< /link> 
< /xsl:template> 
< xsl:template match="Condition"> 
< g:condition> 
< /g:condition> 
< /xsl:template> 
< xsl:template match="Picture"> 
< g:image_link> 
< /g:image_link> 
< /xsl:template> 
< /xsl:stylesheet> 

當我嘗試使用一個較小的文件,我就可以只用幾個項目創建的,我得到這些調試錯誤:

/Users/subnetfile/Desktop/finalxsltemplate.xslt:4: 
parser 
error : 
error parsing attribute name 
< xmlns:g="http://base.google.com/ns/1.0" version="1.0"> 
     ^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:4: 
parser 
error : 
attributes construct error 
< xmlns:g="http://base.google.com/ns/1.0" version="1.0"> 
     ^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:4: 
namespace 
error : 
Namespace prefix xmlns on g is not defined 
< xmlns:g="http://base.google.com/ns/1.0" version="1.0"> 
     ^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:4: 
parser 
error : 
Couldn't find end of Start Tag g line 4 
< xmlns:g="http://base.google.com/ns/1.0" version="1.0"> 
     ^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:20: 
parser 
error : 
Unescaped '<' not allowed in attributes values 
< link> 
^ 
/Users/subnetfile/Desktop/finalxsltemplate.xslt:20: 
parser 
error : 
attributes construct error 
< link> 
^ 
/Users/subnetfile/Desktop/finalxsltemplate.xslt:20: 
parser 
error : 
Couldn't find end of Start Tag template line 19 
< link> 
^ 
/Users/subnetfile/Desktop/finalxsltemplate.xslt:22: 
parser 
error : 
Opening and ending tag mismatch: stylesheet line 2 and template 
< /xsl:template> 
      ^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:24: 
parser 
error : 
Extra content at the end of the document 
< xsl:template match="Condition"> 
^ 
error 
xsltParseStylesheetFile : cannot parse /Users/subnetfile/Desktop/finalxsltemplate.xslt 
(null) 

是我正確理解這一點,我才真正需要使用「匹配」功能爲所有的項目只是有一個不帶屬性值之類的東西屬性的一個子級。
此外,最終產品中的字段需要在原始文件中具有對應的值,或者可以爲每個項目插入它們。例如,最終的格式需要一個名爲'payment_accepted'的字段,並且沒有字段對應,但我只想爲每個項目添加相同的值,例如'Visa'。我會不會'匹配',使用'foreach'之類的東西?

編輯:

< xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:g="http://base.google.com/ns/1.0" 
    version="1.0"> 

    < xsl:output method="RSS 2.0"/> 
    < xsl:template match="*[local-name()='title']"> 
    < xsl:text>title: </xsl:text> 
    < xsl:apply-templates/> 
    < /xsl:template> 

    < xsl:template match="*[local-name()='link']"> 
    < xsl:text>link: </xsl:text> 
    < xsl:apply-templates/> 
    < /xsl:template> 

    < xsl:template match="*[local-name()='description']"> 
    < xsl:text>description: </xsl:text> 
    < xsl:apply-templates/> 
    < /xsl:template> 


    < xsl:template match="language"/> < !-- suppress --> 

< /xsl:stylesheet> 

這是開始做一些事情,但我需要另一條線索請

編輯:
我已經取得了一些進展,並提出一些正確的做一些事情:

< xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:g="http://base.google.com/ns/1.0" 
    version="1.0"> 

    < xsl:output method="text"/> 

    < xsl:template match="Description"> 
    < xsl:text>title: </xsl:text> 
    < xsl:apply-templates/> 
    < /xsl:template> 

    < xsl:template match="Url"> 
    < xsl:text>link: </xsl:text> 
    < xsl:apply-templates/> 
    < /xsl:template> 

    < xsl:template match="Caption"> 
    < xsl:text>description: </xsl:text> 
    < xsl:apply-templates/> 
    < /xsl:template> 


    < xsl:template match="language"/> < !-- suppress --> 

< /xsl:stylesheet> 

這與沒有調試錯誤,它不正是我所需要的,我將如何添加谷歌命名空間屬性使用xlmnsg..etc和xml文件中沒有對應值的一些屬性,就像我只想爲每個節點分配相同的g:condition'new'(我是否正確,每個元素在這裏被稱爲節點? )

+0

我真的不明白你的問題。請提供減少輸入樣本,準確輸出它,並說明關係。 – 2011-02-12 14:31:24

+0

@Alejandro:請參閱我的其他類似問題,它是這個 – fightermagethief 2011-02-13 01:37:57

回答

1

有文本許多正式的錯誤,您提供的「XSLT樣式表」:任何開始標籤的

  • 左尖括號不是由名字後面緊跟。

  • 它不是良好的XML:

<xmlns:g="http://base.google.com/ns/1.0" 

版本= 「1.0」?>

有兩個錯誤這裏

  • 不允許使用名稱xmlns:g,因爲名稱空間前綴不得以保留字「xml」開頭。

  • elemnt名稱後面不能跟一個非空格字符(在本例中爲=)。

這看起來像一個名稱空間聲明,不應編碼爲看起來像元素的東西。

<xsl:template match="Url> 
<link> 
</link> 
</xsl:template> 
<xsl:template match="Condition"> 

第二個引號必須附上match屬性的值丟失,所以一切都在第一行的第一個報價,並在最後一行的第一次報價之間被認爲是屬性值...

即使所有這些錯誤修正和樣式現在看起來是這樣:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:g="http://base.google.com/ns/1.0" 
    version="1.0"> 

    <xsl:output method="RSS 2.0"/> 
    <xsl:template match="Description"> 
     <title> </title> 
    </xsl:template> 
    <xsl:template match="Caption"> 
     <description> </description> 
    </xsl:template> 
    <xsl:template match="Url"> 
     <link> </link> 
    </xsl:template> 
    <xsl:template match="Condition"> 
     <g:condition> </g:condition> 
    </xsl:template> 
    <xsl:template match="Picture"> 
     <g:image_link> </g:image_link> 
    </xsl:template> 
</xsl:stylesheet> 

這段代碼是相當無意義的。它並不真正處理源XML文件,也不會從結果輸出中獲取任何內容。

我向你推薦的是閱讀至少一本輕量級的XSLT教程,只有當你有一些基本的理解才能開始編寫代碼。