Re: [xsl] Get value from external file

Subject: Re: [xsl] Get value from external file
From: Mike Brown <mike@xxxxxxxx>
Date: Tue, 25 Feb 2003 16:11:08 -0700 (MST)
Anders Svensson wrote:
> I have an xsl file which formats xml files into html, online help etc, and
> the xml has attributes at certain elements to "profile" the documents into
> different versions (different formats, users, etc). The xsl creates the
> profiled versions by selecting elements by their profile attributes. For
> example an element can look like this:
>
> <ELEMENT Help='Yes'>
> 
> The xsl can then select these particular elements by select statements such
> as <xsl:apply-templates select="ELEMENT[Help='Yes']/>.
>
> BUT, and here's the question: I need to be able to place the profiling
> select value in a separate file so I can keep the xsl intact, rather than
> changing it for each version transformation. That way I only need to change
> the (xml)file containing the value, and fetch it using a variable. (E.g.)
> 
> I would like to be able to do something like this:
> xml document containing the value:
> <varValueElement>ELEMENT[Help='Yes']</varValueElement>

You probably meant ELEMENT[@Help='Yes'] because you are talking about the 
attribute named Help, not the child element named Help.
 
> In the xsl file:
> <xsl:param name="varValue" select="document('varValue.xml')/varValueElement"/>
> <xsl:apply-templates select="ELEMENT[Help='Yes']/>

What are you trying to do here? Setting $varValue has no bearing on the
behavior of that paticular apply-templates instruction. Perhaps you meant
something like

  <xsl:apply-templates select="foo:evaluate($varValue)"/>

where foo is a prefix bound to the namespace for an extension function for 
dynamic evaluation of strings as XPath expressions (e.g., EXSLT's 
dyn:evaluate())?

I would approach the problem differently, though. Have a control.xml that
says what profile to generate:

<info>
  <profile>help</profile>
</info>

Then in your stylesheet go get that data, and make a pass through the
source tree using a mode depending on what it was:

<xsl:template match="/">
  <xsl:variable name="profile" select="document('control.xml')/info/profile"/>
  <xsl:choose>
    <xsl:when test="$profile='help'">
      <xsl:apply-templates mode="help"/>
    </xsl:when>
    <xsl:when test="$profile='apidocs'">
      <xsl:apply-templates mode="apidocs"/>
    </xsl:when>
    <xsl:when test="$profile='mktg'">
      <xsl:apply-templates mode="mktg"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:message>unknown profile '<xsl:value-of select="$profile"/>'</xsl:message>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!--
  note: there are built-in templates matching "*" for
  each mode; they just contain the instruction
    <xsl:apply-templates mode="(the same mode"/>,
  resulting in the selection and processing of that
  element's children.
-->

<xsl:template match="ELEMENT[@Help='Yes']" mode="help">
 ...
</xsl:template>

<xsl:template match="ELEMENT[@APIDocs='Yes']" mode="apidocs">
  ...
</xsl:template>

<xsl:template match="ELEMENT[@MarketingDrivel='Yes']" mode="mktg">
  ...
</xsl:template>

Mike

-- 
  Mike J. Brown   |  http://skew.org/~mike/resume/
  Denver, CO, USA |  http://skew.org/xml/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread