如何开始使用XSLT?

-2 xml xslt

我从未对XSLT做过任何事情,我需要编写一个XSLT脚本或其他一些脚本来为我们正在FTP到某个位置的XML文件添加标题和预告片.

我怎样才能做到这一点?

kjh*_*hes 5

XSLT快速入门

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

    <root>
      <header>This header text</header>
      <body>This is body text.</body>
    </root>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 运行身份转换,

    <?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>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 并确保生成与输入XML相同的XML:

    <root>
      <header>This header text</header>
      <body>This is body text.</body>
    </root>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 然后添加另一个模板来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>
    
    Run Code Online (Sandbox Code Playgroud)
  5. 并运行新的XSLT以生成包含页脚的新XML输出:

    <root>
      <header>This header text</header>
      <body>This is body text.</body>
      <footer>This is new footer text</footer>
    </root>
    
    Run Code Online (Sandbox Code Playgroud)
  6. 以这种方式继续,直到您的输出完全符合要求.

推荐的XSLT资源