【问题标题】:XSLT stylesheet that converts XML to XML (RSS 2.0)将 XML 转换为 XML (RSS 2.0) 的 XSLT 样式表
【发布时间】:2011-06-25 07:11:22
【问题描述】:

这是我目前所拥有的:

< ?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' 分配给每个节点(我是否正确地将每个元素在这里称为节点?)

【问题讨论】:

  • 我不太明白你的问题。请提供减少输入样本,准确的期望输出,并解释关系。
  • @Alejandro: 请看我的其他类似问题,这是更精致的版本

标签: xslt rss2


【解决方案1】:

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

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

  • 不是格式良好的 XML:

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

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

【讨论】:

  • 我认为这只是将几个选择字段从一个文档转移到另一个文档的问题。这些项目都具有相同的结构。我正在寻找材料,但一切都是关于转换为 HTML。如果我只有一个示例样式表来填充类似于上面的列表。
  • @bboyreason:我已经给了你最好的“线索”:阅读和学习。
  • 实际上我找不到用于创建 RSS 2.0 的过程的材料。我已经学会了如何从 xml 中创建 html 网页,但这使用 xsl:template match="/" 的约定,然后从根目录开始工作。我见过使用“选择”,但目标是 /x/y/z 格式,我不明白这如何转换为 xml 的“标签”格式
  • 好的,我做了一些功能正确的东西(见上文),你能帮我改进一下吗? 'rss 2.0' 的输出方法一直出错,我尝试了 'rss_2.0' 'rss' 'rss2.0' 但都是相同的错误,所以我选择了文本,但是当作为渲染结果查看时,我看到了图像,所以...
猜你喜欢
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多