当前位置: 动力学知识库 > 问答 > 编程问答 >

xml - Parameters passed in through xsltApplyStylesheet

问题描述:

How are they stored in the transformation?

How do I access them besides using "$variable". Can I access them through their names?

<xsl:value-of select = '*/*/xsl:param[@name=myvariable']/@select"/>

网友答案:

No, parameters cannot be dynamically accessed by name, but if you are using an XSLT processor that supports the node-set() function, you can set up a map like this to sort of access them by name:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:param name="myParam1" />
  <xsl:param name="myParam2" />

  <xsl:variable name="paramsFragment">
    <item name="myParam1">
      <xsl:copy-of select="$myParam1"/>
    </item>
    <item name="myParam2">
      <xsl:copy-of select="$myParam2"/>
    </item>
  </xsl:variable>
  <xsl:variable name="params" select="msxsl:node-set($paramsFragment)/*" />

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:value-of select="$params[@name = 'myParam2']"/>

    <xsl:variable name="paramName" select="'myParam1'" />
    <xsl:value-of select="$params[@name = $paramName]"/>
  </xsl:template>
</xsl:stylesheet>
网友答案:

The value of parameters, whose values are produced dynamically, can only be accessed via a parameter reference (using the '$'paramName syntax).

From a comment made by the OP:

Is there a way to see what parameters were passed into the transformation while inside the xslt file?

Yes, there is such way that can be successful to a large extent:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:param name="pmyParam1" select="-999999999999999"/>

 <xsl:template match="/">
  <xsl:if test="not($pmyParam1 = '-999999999999999')">
   $pmyParam1 has a non-default value -- was definitely set from outside !
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

The transformation above compares the parameter value with the default value specified. If the two values are different, this means that an outside value was passed. If not, one cannot draw a firm conclusion.

However, if the default value is carefully chosen to be something that is very unique and very improbable for the initiator of the transformation to know, then the fact that the actual and default values are the same, strongly implies that the outside caller hasn't specified a value for this parameter.

分享给朋友:
您可能感兴趣的文章:
随机阅读: