简体   繁体   English

如何将参数值传递给XSL?

[英]How to pass parameter value to XSL?

Suppose I have a XSL as follows: 假设我有一个XSL,如下所示:

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="1.0">
  <xsl:output method="html" encoding="utf-8" omit-xml-declaration="yes" indent="yes"/>
    <xsl:param name="sortKey" select="'firstname'"/>
</xsl:stylesheet>

Then a XML as follows 然后是XML,如下所示

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="XYZ.xsl"?>
<ABC>
     <firstname>GREG</firstname>
</ABC>

I want to pass a value to the XSL parameter firstname from XML. 我想传递一个值从XML的XSL参数名字 Can I do that? 我能这样做吗? If yes, How? 如果是,怎么样?

It looks from the answers that this is not possible. 从答案看来,这是不可能的。

What about reading the value from the same XML and assigning it to the parameter. 如何从相同的XML读取值并将其分配给参数呢? Can this be done? 能做到吗? If yes, how? 如果有,怎么样?

Because the value you want as a parameter is in your base XML, you can simply enter a XPath expression for the paramater that specifies a default value. 因为您要作为参数的值在基本XML中,所以您只需为参数输入一个XPath表达式即可指定默认值。

<xsl:param name="sortKey" select="/ABC/firstname"/> 

Simply using an variable, would also be valid here, if the value was always going to be in the XML 如果该值始终位于XML中,则仅使用变量在此处也将有效

<xsl:variable name="sortKey" select="/ABC/firstname"/> 

And then, to use this parameter/variable, you would simply do something like this 然后,要使用此参数/变量,您只需执行以下操作

<xsl:template match="/">
    <xsl:value-of select="$sortKey" />
</xsl:template>

This would simply output the value GREG 这将简单地输出值GREG

假设您正在使用.NET进行转换,则可以在这里找到该重复的问题的答案: 通过.NET将参数传递给XSLT样式表

The W3C has not proposed a mechanism. W3C尚未提出机制。 It's possible to define a new processing instruction which is capable of doing this. 可以定义一个能够执行此操作的新处理指令。 But if you're planning on using this in a browser or other existing tool, it's not a viable option to pass parameters into stylesheets which are invoked through processing instructions. 但是,如果您打算在浏览器或其他现有工具中使用它,将参数传递到通过处理指令调用的样式表中不是可行的选择。

An alternative option is to place your "parameter" in an alternate document, and load it using something like the document() function . 另一种选择是将“参数”放置在备用文档中,并使用诸如document()函数之类的东西加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM