如何开始使用 XSLT? [英] How to get started with XSLT?

查看:34
本文介绍了如何开始使用 XSLT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未对 XSLT 做过任何事情,我需要编写一个 XSLT 脚本或其他一些脚本来向我们通过 FTP 传输到某个位置的 XML 文件添加标头和尾标.

I have never done anything with XSLT and I need to write an XSLT script or some other script to add a header and a trailer to XML files that we are FTPing to a location.

我该怎么做?

推荐答案

XSLT 快速入门

  1. 创建示例输入 XML 文件:

  1. Create a sample input XML file:

<root>
  <header>This header text</header>
  <body>This is body text.</body>
</root>

  • 运行身份转换,

  • Run the identity transform,

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

  • 并确保生成与输入 XML 相同的 XML:

  • And be sure that you generate the same XML as the input XML:

    <root>
      <header>This header text</header>
      <body>This is body text.</body>
    </root>
    

  • 然后添加另一个模板以区别对待body

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
      <!-- New template for body element -->
      <xsl:template match="body">
        <!-- Copy body as-is like in the default identity template -->
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
        <!-- Do something new here: Add a footer element -->
        <footer>This is new footer text</footer>
      </xsl:template>
    </xsl:stylesheet>
    

  • 并运行新的 XSLT 以生成包含页脚的新 XML 输出这次:

  • And run the new XSLT to generate new XML output containing a footer this time:

    <root>
      <header>This header text</header>
      <body>This is body text.</body>
      <footer>This is new footer text</footer>
    </root>
    

  • 以这种方式继续,直到您的输出完全符合要求.
  • 推荐的 XSLT 资源

    这篇关于如何开始使用 XSLT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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