将 XSLT 和 XML 输出到 XML 文件 [英] Make XSLT and XML Output to a XML File

查看:30
本文介绍了将 XSLT 和 XML 输出到 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 XSLT 时遇到了一些问题.

I have a few problems with XSLT.

我有这个 XML 文件:

I have this XML File:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="XSLTest.xsl"?>
<pages>
  <page>
      <title>New Title</title>
      <id>4782</id>
      <timestamp>2012-09-13 13:15:33</timestamp>
      <contributor>
        <username>kf</username>
        <id>2</id>
      </contributor>
      <text xml:space="preserve"> 
      some text
    </text>
 </page>
 </pages>

和这个 XSL 文件:

and this XSL File:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:for-each select="pages/page">
    <content>
       <id><xsl:value-of select="id"/></id>
       <title><xsl:value-of select="title"/></title>
       <created><xsl:value-of select="timestamp"/></created>
       <created_by><xsl:value-of select="contributor/username"/></created_by>
   </content>
   </xsl:for-each>
   </xsl:template>

现在解决我的问题:

  1. XML 文件转换仅部分起作用.除了标题,我得到了我想要的所有值.

  1. The XML File Transformation does only partially work. I get all the values I want except for the title.

我想将转换后的代码保存到一个新的 XML 文件中.

I would like to save the transformed code to a new XML File.

迭代是必要的,因为我要更改的原始文件要大得多,.

The iteration is necessary because the original file that I want to change is much bigger with much more <pages>.

推荐答案

XML 文件转换仅部分起作用.除了标题,我得到了我想要的所有值.(...) 我不知道为什么它在 w3 的 webeditor 上不起作用

The XML File Transformation does only partially work. I get all the values I want except for the title. (...) I have no idea why it doesn't work on the webeditor of w3

永远不要相信 w3schools 上的任何内容 - 它们与 W3C 没有任何关系,并且不能期望他们的工具符合规范.我强烈建议您不要再次使用该站点来测试 XSLT 转换.在撰写本文时,http://xsltransform.net 是一个值得信赖的绝佳替代方案.

Never trust any content on w3schools - they are in no way related to W3C, and their tools cannot be expected to be compliant with the specifications. I strongly recommend you never use the site again to test XSLT transformations. At the time of writing, http://xsltransform.net is a great alternative that can be relied upon.

我想将转换后的代码保存到一个新的 XML 文件中.

I would like to save the transformed code to a new XML File.

查看 XML 文档中的样式表引用:

Looking at the stylesheet reference in your XML document:

<?xml-stylesheet type="text/xsl" href="XSLTest.xsl"?>

看起来您目前正在浏览器中进行此转换.所有主流浏览器都包含 XSLT 1.0 处理器,但它们不允许您将转换结果保存为文件.在浏览器中,转换旨在促进数据的显示,而不是对文档进行永久性更改.

it looks like you are currently doing this transformation in the browser. All major browsers include an XSLT 1.0 processor, but they will not allow you to save the transformation result as a file. In browsers, transformations are meant to facilitate the display of data, not to make permanent changes to the document.

要将输出保存到文件,您需要

To save the output to a file, you need to

  • 使用像 Saxon 这样的命令行工具
  • 使用 XSLT 库和您选择的编程语言来解析输入文件,对其进行转换并将结果输出到文件中

关于 XSLT 版本的最后一条评论:

One last comment on XSLT versions:

您的样式表指出代码应使用 XSLT 2.0 版:

Your stylesheet states that the code should use XSLT version 2.0:

<xsl:stylesheet version="2.0">

但目前,没有浏览器支持 XSLT 2.0.要使用特定于 XSLT 2.0 的功能(您不使用任何功能),您需要一个 XSLT 2.0 处理器.

but currently, no browser supports XSLT 2.0. To use features that are specific to XSLT 2.0 (you don't use any), you need an XSLT 2.0 processor.

这篇关于将 XSLT 和 XML 输出到 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆