0

这是我到目前为止所拥有的:

< ?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 的 google 命名空间属性以及一些在 xml 文件中没有相应值的属性,就像我只想分配相同的 g:condition 'new' 到每个节点(我是否正确地将每个元素在这里称为节点?)

4

1 回答 1

1

您作为“XSLT 样式表”提供的文本中有许多形式错误:

  • 任何开始标签的左尖括号后面都不会紧跟名称。

  • 它不是格式良好的 XML:

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

版本="1.0">

这里有两个错误

  • 不允许使用该名称xmlns:g,因为名称空间前缀不得以保留字“xml”开头。

  • 元素名称后面不能跟非空格字符(在本例中为=)。

这看起来像一个命名空间声明,不应该被编码为看起来像一个元素的东西。

<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 教程,并且只有在您对开始编写代码有一些基本了解时。

于 2011-02-11T03:08:21.523 回答