Using the Document () Function in XSLT 1.0

I am running the conversion with .NET code,

if I don't add the property " EnableDocumentFunction

" to the XSL setup, the program gives an error message .. " Usage of Document() function is prohibited

",

The program is not actually editable and is not read-only .. is it possible to edit the XSL itself so that I can use the document () function?

Sample XSL and XMLs here:
Sample XML:

<?xml version="1.0" encoding="utf-8"?>
<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:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
  <xsl:variable name="State_Code_Trn">
    <State In="California" Out="CA"/>
    <State In="CA" Out="CA"/>
    <State In="Texas" Out="TX"/>
    <State In="TX" Out="TX"/>
  </xsl:variable>

  <xsl:template name="testing" match="test_node">
    <xsl:variable name="test_val">
      <xsl:value-of select="."/>
    </xsl:variable>
    <xsl:element name="{name()}">
      <xsl:choose>
      <xsl:when test="document('')/*/xsl:variable[@name='State_Code_Trn']
              /State[@In=$test_val]">
    <xsl:value-of select="document('')/*/xsl:variable[@name='State_Code_Trn']
              /State[@In=$test_val]/@Out"/>
      </xsl:when>
        <xsl:otherwise>
          <xsl:text>Other</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

      



And the XML sample:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <test_node>California</test_node>
  <test_node>CA</test_node>
  <test_node> CA</test_node>
  <test_node>Texas</test_node>
  <test_node>TX</test_node>
  <test_node>CAA</test_node>
  <test_node></test_node>
</root>

      

+2


a source to share


2 answers


The document () function is used here to access the XSLT document itself and retrieve the contents of the xsl: variable. There is virtually no need to use the document () function at all in this case.

Since you are using Microsoft.Net here, you should have access to the msxml extension functions for XSLT. Indeed, the corresponding namespace for msxml is already defined in the XSLT document

xmlns:msxsl="urn:schemas-microsoft-com:xslt"

      

This means that you can use the node-set function to quickly access the nodes inside the State_Code_Tran variable. To do this, try modifying an existing xsl: function select like this:



   <xsl:choose>
    <xsl:when test="msxsl:node-set($State_Code_Trn)/State[@In=$test_val]">
     <xsl:value-of select="msxsl:node-set($State_Code_Trn)/State[@In=$test_val]/@Out"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:text>Other</xsl:text>
    </xsl:otherwise>
   </xsl:choose>

      

This should lead to the following conclusion

<root>
<test_node>CA</test_node>
<test_node>CA</test_node>
<test_node>Other</test_node>
<test_node>TX</test_node>
<test_node>TX</test_node>
<test_node>Other</test_node>
<test_node>Other</test_node>
</root>

      

(Note that you have a space before one of the CAs in your original XML, so it comes out as “Other.” You might want to add some trimming function to handle this.

+2


a source


In addition to @ Tim-C's answer, you can use the ext: node -set () extension function, where the "ext" prefix is ​​associated with the EXSLT: namespace "http://exslt.org/common"

.



This is supported XslCompiledTransform

and will make your XSLT a little more portable as EXSLT is the standard library of extension functions.

+2


a source







All Articles