2011-02-11 65 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教程,只有当你有一些基本的理解才能开始编写代码。