利用XSLT产生一个唯一的ID并引用它

本文介绍了XSLT样式表将一个XML文档转换为另一个文档时,使用generate - id()函数为元素添加唯一ID的方法。该函数能为节点生成唯一ID,可用于创建链接。即使多次调用该函数处理同一节点,生成的ID也一致,这使得它成为生成链接的有效方式。

Generating Unique IDs and Linking to Them

When an XSLT stylesheet converts one XML document into another, the ability to add unique ID values to elements in the result document can make the result document much more useful to applications that use it. Adding unique IDs can, for example, turn each element into the unique target of a link.

XSLT's generate-id() function generates a unique ID for a node passed to it as an argument. This ID starts with a letter so that you can use it as the value of an XML ID attribute. For example, the following stylesheet copies an XML document and adds a uid ("unique ID") attribute to each chapter, sect1, and sect2 element. The xsl:value-of instruction uses the generate-id() function in the stylesheet's first template rule to create a value for these attributes.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version=
"1.0"><xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="chapter | sect1 | sect2">
    <xsl:copy>
      <xsl:attribute name="uid">
        <xsl:value-of select="generate-id(.)"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

The stylesheet turns this XML document

<chapter>
<para>Then with expanded wings he steers his flight</para>
<figure><title>"Incumbent on the Dusky Air"</title>
<graphic fileref="pic1.jpg"/></figure>
<para>Aloft, incumbent on the dusky Air</para>
<sect1>
<para>That felt unusual weight, till on dry Land</para>
<figure><title>"He Lights"</title>
<graphic fileref="pic2.jpg"/></figure>
<para>He lights, if it were Land that ever burned</para>
<sect2>
<para>With solid, as the Lake with liquid fire</para>
<figure><title>"The Lake with Liquid Fire"</title>
<graphic fileref="pic3.jpg"/></figure>
</sect2>
</sect1>
</chapter>
into this one:

<chapter uid="N134711680">
<para>Then with expanded wings he steers his flight</para>
<figure><title>"Incumbent on the Dusky Air"</title>
<graphic fileref="pic1.jpg"/></figure>
<para>Aloft, incumbent on the dusky Air</para>
<sect1 uid="N134683456">
<para>That felt unusual weight, till on dry Land</para>
<figure><title>"He Lights"</title>
<graphic fileref="pic2.jpg"/></figure>
<para>He lights, if it were Land that ever burned</para>
<sect2 uid="N134684064">
<para>With solid, as the Lake with liquid fire</para>
<figure><title>"The Lake with Liquid Fire"</title>
<graphic fileref="pic3.jpg"/></figure>
</sect2>
</sect1>
</chapter>

Your XSLT processor may generate different values with the generate-id() function. In fact, if you run the same stylesheet with the same input document a second time, the XSLT processor may not generate the same ID values that it generated the first time. However, if you call generate-id() more than once in one run with the same node as an argument, it generates the same ID value each time for that node. Because unique IDs are popular ways to identify link destinations, this consistency of the generate-id() function makes it a great way to generate links.

For example, adding a list of all of its illustrations at the beginning of the result document. If we make the result tree version an HTML file, we can use the generate-id function to turn each entry of this opening illustration list into an HTML link to the img element in the body of the document that has the illustration:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
  <xsl:output method="html"/>
  <xsl:template match="chapter">
    <html><body>
      <!-- Generate a list of picture titles, with each 
           title linking to the picture in the poem below. -->
      <b>Pictures:</b><br/>
      <xsl:for-each select="descendant::figure">
        <a href="#{generate-id(graphic)}">
        <xsl:value-of select="title"/></a><br/>
    </xsl:for-each>
    <xsl:apply-templates/>
    </body></html>
  </xsl:template>
  <xsl:template match="para">
    <p><xsl:apply-templates/></p>
  </xsl:template>
  <xsl:template match="graphic">
    <!-- Image and title as caption, centered. -->
    <center><a name="{generate-id(.)}"><img src="{@fileref}"/></a>
    <b><xsl:value-of select="../title"/></b></center>
  </xsl:template>
  <!-- Suppress figure title because "graphic" template
       rule already added it to result tree. -->
  <xsl:template match="figure/title"/>
</xsl:stylesheet>

With the source document above, this stylesheet creates the following HTML document:

<html>
    <body>
        <b>Pictures:</b>
        <br>
        <a href="#N134691840">"Incumbent on the Dusky Air"</a>
        <br>
        <a href="#N134692416">"He Lights"</a>
        <br>
        <a href="#N134757920">"The Lake with Liquid Fire"</a>
        <br>
<p>Then with expanded wings he steers his flight</p>
<center>
            <a name="N134691840"><img src="pic1.jpg"></a>
            <b>"Incumbent on the Dusky Air"</b>
        </center>
<p>Aloft, incumbent on the dusky Air</p>
<p>That felt unusual weight, till on dry Land</p>
<center>
            <a name="N134692416"><img src="pic2.jpg"></a>
            <b>"He Lights"</b>
        </center>
<p>He lights, if it were Land that ever burned</p>
<p>With solid, as the Lake with liquid fire</p>
<center>
            <a name="N134757920"><img src="pic3.jpg"></a>
            <b>"The Lake with Liquid Fire"</b>
        </center>

</body>
</html>

(To view the HTML document, you'll need to supply your own pic1.jpg, pic2.jpg, and pic3.jpg files.) The stylesheet uses the generate-id() ID twice:

This consistency in the generate-id() function's treatment of a particular node, even if the function generates an ID for that node more than once, is the key to its power. These graphic elements didn't have IDs in the source document; with the help of this function, their equivalent in the result document has them, and other elements in that document use those IDs to link to them.

<think>好的,我现在需要帮助用户解决如何在ANT与JMeter整合时编写build.xml文件的问题。首先,我得回忆一下用户提供的引用内容,看看有没有相关的信息可以利用。 根据引用[1],用户提到需要配置build.xml文件,将JMeter的ant-jmeter-1.1.1.jar拷贝到Ant的lib目录下。同时,build.xml需要放在和JMeter脚本同级的目录,执行ant命令生成报告。引用[3]中提到了使用Apache Ant执行多个JMeter脚本生成单独报告的步骤,可能需要为每个脚本配置不同的target。引用[4]解释了Ant的结构,说明build.xml由target和task组成,每个task执行特定操作。 用户的需求是编写一个build.xml文件的示例,整合Ant和JMeter,可能还需要支持多个脚本生成单独报告。首先,我需要确定build.xml的基本结构,包括属性定义、路径设置、任务定义等。然后,需要配置JMeter任务,指定脚本路径、结果输出、报告生成等步骤。 可能需要定义一些属性,如jmeter.home、jmeter.result.dir等,方便后续引用。然后设置类路径,包含JMeter的Ant依赖包。接着,定义初始化、清理、执行测试和生成报告等target。执行测试的target需要遍历所有jmx文件,为每个文件生成对应的结果和报告。引用[3]中提到可以通过循环或多个target来处理多个脚本,但Ant本身不支持循环,可能需要使用ant-contrib库的foreach任务,或者通过文件集动态处理。 需要确保每个JMeter脚本执行后,jtl结果文件和HTML报告存放在不同的目录中,避免覆盖。例如,使用${jmx.basename}来动态获取脚本文件名,创建对应的目录结构。同时,生成报告时使用XSLT转换,可能需要引用JMeter的xsl文件,如jmeter-results-detail-report.xsl。 另外,用户可能希望生成汇总报告,所以需要配置一个汇总的target,合所有结果或生成总览报告。还要注意清理操作,删除临时文件。最后,确保用户按照步骤操作,包括拷贝jar文件、配置路径、执行命令等。 现在需要把这些思路整理成一个结构清晰的build.xml示例,确保语法正确,注释每个部分的作用。同时,按照用户的要求,生成相关问题,帮助用户进一步了解相关配置和优化。</think>以下是一个整合 Apache Ant 和 JMeter 的 `build.xml` 文件示例,包含详细注释和配置说明: ```xml <?xml version="1.0" encoding="UTF-8"?> <project name="JMeter-Ant-Integration" default="run" basedir="."> <!-- 1. 定义全局属性 --> <property name="jmeter.home" value="D:/工具/apache-jmeter-5.5"/> <property name="jmeter.result.dir" value="./results"/> <property name="jmeter.testplans.dir" value="./testplans"/> <!-- 2. 设置类路径 --> <path id="jmeter.classpath"> <fileset dir="${jmeter.home}/lib"> <include name="**/*.jar"/> </fileset> <pathelement location="${jmeter.home}/extras/ant-jmeter-1.1.1.jar"/> </path> <!-- 3. 定义任务 --> <taskdef name="jmeter" classpathref="jmeter.classpath" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/> <!-- 4. 初始化目录 --> <target name="init"> <mkdir dir="${jmeter.result.dir}"/> <tstamp> <format property="timestamp" pattern="yyyyMMdd_HHmmss"/> </tstamp> </target> <!-- 5. 执行JMeter测试 --> <target name="run" depends="init"> <jmeter jmeterhome="${jmeter.home}" testplan="${jmx.file}" resultlog="${jmeter.result.dir}/results_${timestamp}.jtl"> <property name="jmeter.save.saveservice.output_format" value="xml"/> <property name="jmeter.save.saveservice.response_data" value="true"/> </jmeter> </target> <!-- 6. 生成HTML报告 --> <target name="report" depends="run"> <xslt in="${jmeter.result.dir}/results_${timestamp}.jtl" out="${jmeter.result.dir}/report_${timestamp}.html" style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"/> </target> <!-- 7. 多脚本批量执行(需ant-contrib支持) --> <target name="batch-run" depends="init"> <taskdef resource="net/sf/antcontrib/antcontrib.properties"/> <foreach target="execute-single" param="jmx.file"> <fileset dir="${jmeter.testplans.dir}" includes="*.jmx"/> </foreach> </target> <target name="execute-single"> <basename property="jmx.basename" file="${jmx.file}" suffix=".jmx"/> <jmeter jmeterhome="${jmeter.home}" testplan="${jmx.file}" resultlog="${jmeter.result.dir}/${jmx.basename}_${timestamp}.jtl"> <property name="jmeter.save.saveservice.output_format" value="xml"/> </jmeter> <xslt in="${jmeter.result.dir}/${jmx.basename}_${timestamp}.jtl" out="${jmeter.result.dir}/${jmx.basename}_report_${timestamp}.html" style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"/> </target> <!-- 8. 清理结果 --> <target name="clean"> <delete dir="${jmeter.result.dir}"/> </target> </project> ``` ### 关键配置说明 1. **路径配置**:`jmeter.home` 需指向实际 JMeter 安装路径,`ant-jmeter-1.1.1.jar` 需要复制到 Ant 的 lib 目录[^1] 2. **动态时间戳**:通过 `<tstamp>` 生成唯一时间标识,避免结果文件覆盖 3. **多脚本支持**:使用 `ant-contrib` 的 `<foreach>` 任务遍历 `testplans` 目录下的所有 .jmx 文件[^3] 4. **报告生成**:利用 XSLT 转换将 .jtl 结果文件转换为 HTML 报告,需确保指定的 .xsl 文件存在[^4] ### 执行命令示例 ```bash # 执行单个脚本 ant -Djmx.file=testplans/example.jmx # 批量执行所有脚本(需要先安装ant-contrib) ant batch-run ``` ### 目录结构建议 ``` ├── build.xml ├── testplans/ │ ├── test1.jmx │ └── test2.jmx └── results/ ├── test1_report.html └── test2_report.html ``` 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值