create account

如何在命令行测试XSLT by aafeng

View this thread on: hive.blogpeakd.comecency.com
· @aafeng ·
$11.96
如何在命令行测试XSLT
在工作中大量使用XSLT进行各种文档的转换。但其调试过程还是比较麻烦的。这里分享一个很好用的命令行工具,可以进行XSLT的调试。以下环境是针对Ubuntu 18.04 LTS的。


![image.png](https://images.hive.blog/DQmS8hWyQyHnAZwZPvMmm3VqnddLsxUucCcAQREkJw5wqU4/image.png)


安装

    sudo apt-get install xalan

检查xalan的版本号:

    xalan -v

下面来做一个简单的测试。首先创建一个简单的XML文件text.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <students>
      <student>
        <id>91001</id>
        <name>Jim</name>
      </student>
      <student>
        <id>91002</id>
        <name>Julie</name>
      </student>
      <student>
        <id>91003</id>
        <name>Stuart</name>
      </student>  
    </students>

创建测试的XSLT文件:test.xsl:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html> 
    <body>
      <h2>Students</h2>
      <table border="1">
        <tr bgcolor="#9acd32">
          <th style="text-align:left">ID</th>
          <th style="text-align:left">Name</th>
        </tr>
        <xsl:for-each select="students/student">
        <tr>
          <td><xsl:value-of select="id"/></td>
          <td><xsl:value-of select="name"/></td>
        </tr>
        </xsl:for-each>
      </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

在命令行运行:

    xalan -in test.xml -xsl test.xsl -out out.html

可以看到生成的HTML文件:

    <html>
    <body>
    <h2>Students</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th style="text-align:left">ID</th><th style="text-align:left">Name</th>
    </tr>
    <tr>
    <td>91001</td><td>Jim</td>
    </tr>
    <tr>
    <td>91002</td><td>Julie</td>
    </tr>
    <tr>
    <td>91003</td><td>Stuart</td>
    </tr>
    </table>
    </body>
    </html>

其实xalan命令行工具还有很多好用的选项。比如运行如下参数:

    xalan -in test.xml -xsl test.xsl -out out.html -tg

这样就能看到解析过程中每一个具体步骤:

![image.png](https://images.hive.blog/DQmcJCBLjw7As8kEVdSmW9roNhkkP5d1bMvcngHxp7EXPUk/image.png)

如果想要查看在解析过程中被调用的每一个模板,使用参数tt:

    xslt xalan -in test.xml -xsl test.xsl -out out.html -tt

如果想要监测在解析过程中对每一个XML元素的选取:

    xalan -in test.xml -xsl test.xsl -out out.html -ts

其输出类似于:

![image.png](https://images.hive.blog/DQmSwxss6sKPEBtboZQQwogC54U2LhE4BoPJWtPggNCdxRp/image.png)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 172 others
properties (23)
authoraafeng
permlinkxslt
categoryhive-105017
json_metadata{"tags":["cn","cn-curation","cn-reader","cn-programming"],"image":["https://images.hive.blog/DQmS8hWyQyHnAZwZPvMmm3VqnddLsxUucCcAQREkJw5wqU4/image.png","https://images.hive.blog/DQmcJCBLjw7As8kEVdSmW9roNhkkP5d1bMvcngHxp7EXPUk/image.png","https://images.hive.blog/DQmSwxss6sKPEBtboZQQwogC54U2LhE4BoPJWtPggNCdxRp/image.png"],"app":"hiveblog/0.1","format":"markdown"}
created2020-09-16 15:18:21
last_update2020-09-16 15:18:21
depth0
children0
last_payout2020-09-23 15:18:21
cashout_time1969-12-31 23:59:59
total_payout_value6.316 HBD
curator_payout_value5.646 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,224
author_reputation546,202,457,352,023
root_title如何在命令行测试XSLT
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,659,977
net_rshares43,568,548,675,778
author_curate_reward""
vote details (236)