如何将参数传递给XSLT? [英] How to pass parameters to XSLT?

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

问题描述

我有问题.

我有一个XML文件,其中包含有关100门课程的信息.

我有一个XSL文件,可以很好地显示100门课程的列表.

但是,如果我只想显示1门课程,该怎么办?我可以将参数传递给XSLT文件以告诉它仅显示"ENGL 100"吗?

XML看起来像这样:

<document>
<menu>
   <item>
      <name>MTH 300</name>
      <brief>Mathematics Skill Development</brief>
      <description>A course in the fundamentals of ...</description>
   </item>
   <item>
      <name>MTH 301</name>
      <brief>Basic Algebra</brief>
      <description>An introduction to algebra, ...</description>
   </item>
 ...

我知道我可以编写一个名为"eng100.xsl"的XSLT文件以遍历XML并仅显示ENG 100,但我不想写很多这样的文件.

XML是动态的,我可以控制它.我希望XSLT文件是静态文件,并且永不更改.

有什么方法可以将参数传递到XSLT中?

解决方案

您可以将参数传递给XSLT,如何完成此操作取决于您的XSLT处理器,但如果它是命令行处理器,则通常作为附加命令参数使用. /p>

您使用声明参数

  <xsl:param name="courseName" select"initialValue"/>

然后可以在XSLT中测试此参数,并根据其值调用另一个模板.例如,如果参数为空,则调用处理所有元素的当前模板,否则调用仅在项目名称等于参数值时处理元素的模板.您可以通过测试来完成

   <xsl:template match="item">
      <xsl:if test="$courseName=name(./name)">
         <xsl:call-template name="yourOriginalTemplate"/>
      </xsl:if>
   </xsl:template>

但是通过过滤和格式化,您将两个问题混合在一个文件中.我将从格式中分离出XML元素的选择-为此有两个xslt文件,并将它们作为管道运行.

I have a problem.

I have an XML file that contains information about 100 courses.

I have an XSL file that nicely displays the list of 100 courses.

But what if I want to only display 1 course. Can I pass a parameter to the XSLT file to tell it to only display "ENGL 100" ?

The XML looks something like this:

<document>
<menu>
   <item>
      <name>MTH 300</name>
      <brief>Mathematics Skill Development</brief>
      <description>A course in the fundamentals of ...</description>
   </item>
   <item>
      <name>MTH 301</name>
      <brief>Basic Algebra</brief>
      <description>An introduction to algebra, ...</description>
   </item>
 ...

I know I could write an XSLT file called "eng100.xsl" to loop through the XML and display only ENG 100 but I don't want to have to write dozens of these files.

The XML is dynamic and I am able to control it. I want the XSLT file to be static and never change.

Is there any way to pass parameters into the XSLT?

解决方案

You can pass parameters to XSLT, how this is done depends on your XSLT processor, but usually as additional command arguments, if it's a command-line processor.

You declare parameters using

  <xsl:param name="courseName" select"initialValue"/>

You can then test this parameter in your XSLT, and invoke a different template depending on it's value. For example, if the parameter is empty, then invoke the current template that processes all elements, otherwise invoke a template that only processes elements when the item name equals the parameter value. You can do this with a test

   <xsl:template match="item">
      <xsl:if test="$courseName=name(./name)">
         <xsl:call-template name="yourOriginalTemplate"/>
      </xsl:if>
   </xsl:template>

But by filtering and formatting, you are mixing two concerns in one file. I would separate out the selection of the XML elements from formatting - have two xslt files for that and run them as a pipeline.

这篇关于如何将参数传递给XSLT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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