Make XSLT and XML Output to a XML File

眉间皱痕 提交于 2020-01-16 00:54:13

问题


I have a few problems with XSLT.

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>

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>

So now to my problems:

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

  2. 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>.


回答1:


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

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.

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

Looking at the stylesheet reference in your XML document:

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

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

  • use a command line tool like Saxon
  • use an XSLT library and the programming language of your choice to parse an input file, transform it and output the result to a file

One last comment on XSLT versions:

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

<xsl:stylesheet version="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.




回答2:


I had a similar problem, and I have found that it is possible to use Google Chrome to apply an xslt transformation. I will document the process here since I have already forgotten once how to do so.

Here is an example xml file:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sort.xslt"?>

<employees>

  <employee hireDate="04/23/1999">
    <last>Hill</last>
    <first>Phil</first>
    <salary>100000</salary>
  </employee>

  <employee hireDate="09/01/1998">
    <last>Herbert</last>
    <first>Johnny</first>
    <salary>95000</salary>
  </employee>

  <employee hireDate="08/20/2000">
    <last>Hill</last>
    <first>Graham</first>
    <salary>89000</salary>
  </employee>

  <employee hireDate="08/20/2001">
    <last>Hill</last>
    <first>Albert</first>
    <salary>110000</salary>
  </employee>

</employees>

And here is the corresponding xslt (sort.xlst):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="employees">
    <xsl:copy>
      <xsl:apply-templates select="employee">
        <xsl:sort select="salary" data-type="number"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

First, place both of them in the same folder. Then launch Google chrome with the --allow-file-access-from-files flag. Then open the xml file in chrome. The xml will be rendered as:

Hill Graham 89000 Herbert Johnny 95000 Hill Phil 100000 Hill Albert 110000

As you can see, all the xml tags are missing. However, right-click on the page and select "inspect" to launch the developer console. In the developer console, select the "Elements" tab. Right-click on the <employees> top level tag and from the menu select "Copy -> Copy outerHTML". Open a new document in your text editor of choice and paste the transformed xml. In my example it looks like this:

<employees>
  <employee hireDate="08/20/2000">
    <last>Hill</last>
    <first>Graham</first>
    <salary>89000</salary>
  </employee>
  <employee hireDate="09/01/1998">
    <last>Herbert</last>
    <first>Johnny</first>
    <salary>95000</salary>
  </employee>
  <employee hireDate="04/23/1999">
    <last>Hill</last>
    <first>Phil</first>
    <salary>100000</salary>
  </employee>
  <employee hireDate="08/20/2001">
    <last>Hill</last>
    <first>Albert</first>
    <salary>110000</salary>
  </employee>
</employees>

As you can see, the transformed xml has been recovered and can now be saved.



来源:https://stackoverflow.com/questions/29940465/make-xslt-and-xml-output-to-a-xml-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!