开发者

XSLT: need to replace document('')

开发者 https://www.devze.com 2022-12-23 13:01 出处:网络
I\'ve the following xslt file: <?xml version=\"1.0\" encoding=\"UTF-8\"?> <xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">

I've the following xslt file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- USDomesticCountryList - USE UPPERCASE LETTERS ONLY -->
    <xsl:variable name="USDomesticCountryList">
 开发者_C百科       <entry name="US"/>
        <entry name="UK"/>
        <entry name="EG"/>
    </xsl:variable>
    <!--// USDomesticCountryList -->

    <xsl:template name="IsUSDomesticCountry">
       <xsl:param name="countryParam"/>
       <xsl:variable name="country" select="normalize-space($countryParam)"/>
       <xsl:value-of select="normalize-space(document('')//xsl:variable[@name='USDomesticCountryList']/entry[@name=$country]/@name)"/>
    </xsl:template>
</xsl:stylesheet>

I need to replace the "document('')" xpath function, what should I use instead? I've tried to remove it completely but the xsl document doesn't work for me!

I need to to so because the problem is :

I am using some XSLT document that uses the above file, say document a. So I have document a that includes the above file (document b).

I am using doc a from java code, I am do Caching for doc a as a javax.xml.transform.Templates object to prevent multiple reads to the xsl file on every transformation request.

I found that, the doc b is re-calling itself from the harddisk, I believe this is because of the document('') function above, so I wanna replace/remove it.

Thanks.


If you want to access the nodes inside a variable you normally use the node-set() extension function. The availability and syntax depends on the processor you use. For MSXML and Saxon you can use exsl:node-set(). To use the extension function you will have to include the namespace that defines the function.

E.g. (tested wiht MSXML, returns US for countryName = 'US'):

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
  extension-element-prefixes="exsl"
>
    <xsl:output method="xml"/>

    <!-- USDomesticCountryList - USE UPPERCASE LETTERS ONLY -->
    <xsl:variable name="USDomesticCountryList">
        <entry name="US"/>
        <entry name="UK"/>
        <entry name="EG"/>
    </xsl:variable>
    <!--// USDomesticCountryList -->

    <xsl:template name="IsUSDomesticCountry">
        <xsl:param name="countryParam"/>
        <xsl:variable name="country" select="normalize-space($countryParam)"/>
        <xsl:value-of select="exsl:node-set($USDomesticCountryList)/entry[@name=$country]/@name"/>
    </xsl:template>
</xsl:stylesheet>


If you're trying to make the IsUSDomesticCountry template work without using document(''), you could rewrite the template to

<xsl:template name="IsUSDomesticCountry">
    <xsl:param name="countryParam"/>
    <xsl:variable name="country" select="normalize-space($countryParam)"/>
    <xsl:choose>
        <xsl:when test="$country='US'">true</xsl:when>
        <xsl:when test="$country='UK'">true</xsl:when>
        <xsl:when test="$country='EG'">true</xsl:when>
        <xsl:otherwise>false</xsl:otherwise>
    </xsl:choose>
</xsl:template>

or

<xsl:template name="IsUSDomesticCountry">
    <xsl:param name="countryParam"/>
    <xsl:variable name="country" select="normalize-space($countryParam)"/>
    <xsl:value-of select="$country='US' or $country='UK' or $country='EG'"/>
</xsl:template>

or even

<xsl:template name="IsUSDomesticCountry">
    <xsl:param name="countryParam"/>
    <xsl:variable name="country" 
                  select="concat('-', normalize-space($countryParam),'-')"/>
    <xsl:variable name="domesticCountries" select="'-US-UK-EG-'"/>
    <xsl:value-of select="contains($domesticCountries, $country)"/>
</xsl:template>

Personally, I find the variant using document('') to be more readable.

0

精彩评论

暂无评论...
验证码 换一张
取 消