Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

xsl variables

Posted on 2001-08-22
3
474 Views
Last Modified: 2008-02-01
I am trying to create an image tag in an xsl stylesheet. It's path will include a variable called sURL which defines whether the server is production or development. This is the line of code as it was in ASP.

"https://<%=sURL%>/images/dotted.gif"

Now I need to do this with xsl.
How do I get it to replace sURL with a value set in an include file on an asp page.

So far my environment is very simple. An xml page with a record, an XSL sheet, and an asp page which loads both, and does the transformnode.

Any ideas?

Thanks in advance

Dan

0
Comment
Question by:danchristie
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
chabaud earned 100 total points
ID: 6414378
You could use the AddParameter method from IXSLProcessor like this (from MSDN):

Dim xslt As New Msxml2.XSLTemplate
Dim xslDoc As New Msxml2.FreeThreadedDOMDocument
Dim xmlDoc As New Msxml2.DOMDocument
Dim xslProc As IXSLProcessor
xslDoc.async = False
xslDoc.Load "sample.xsl"
Set xslt.stylesheet = xslDoc
xmlDoc.async = False
xmlDoc.Load "books.xml"
Set xslProc = xslt.createProcessor()
xslProc.input = xmlDoc
xslProc.addParameter "param1", "Hello"
xslProc.Transform
MsgBox xslProc.output
Sample.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html"/>
   <xsl:param name="param1"/>
  <xsl:template match="/">
      The parameter value was: <xsl:value-of select="$param1"/>
  </xsl:template>
</xsl:stylesheet>
[C/C++]
C/C++ Syntax
HRESULT addParameter (BSTR baseName, VARIANT parameter, BSTR
namespaceURI);
Parameters
baseName [in]
The name that will be used inside the style sheet to identify the parameter context.
parameter [in]
A number, Boolean, string, node list, or node. Passing in a single node will produce a node list that contains one node (shortcut).
namespaceURI [in, optional]
Optional namespace.
C/C++ Return Values
E_FAIL if readyState is READYSTATE_INTERACTIVE.

Example
#import "msxml3.dll"
using namespace MSXML2;

#define CHECK_AND_RELEASE(pInterface)  \
if(pInterface) \
   {\
pInterface->Release();\
pInterface = NULL;\
   }\

#define RELEASE(pInterface)  \
   {\
pInterface->Release();\
pInterface = NULL;\
   }\

BOOL XSLProcessorAddParameterDemo ()
{
   BOOL bResult = FALSE;
   short sResult = FALSE;
   HRESULT hr;
   IXMLDOMDocument2 *pStyleSheet=NULL;
   IXSLTemplate *pIXSLTemplate=NULL;
   IXSLProcessor *pIXSLProcessor=NULL;
   VARIANT varValue;

   try
   {
      hr = CoCreateInstance(CLSID_XSLTemplate, NULL, CLSCTX_SERVER,
         IID_IXSLTemplate, (LPVOID*)(&pIXSLTemplate));
      SUCCEEDED(hr) ? 0 : throw hr;
     
      if(pIXSLTemplate)
      {
         hr=CoCreateInstance(CLSID_FreeThreadedDOMDocument, NULL,
CLSCTX_SERVER, IID_IXMLDOMDocument2, (LPVOID*)(&pStyleSheet));
         SUCCEEDED(hr) ? 0 : throw hr;
         
         if(pStyleSheet)
         {
            hr=pStyleSheet->put_async(VARIANT_FALSE);
            if(SUCCEEDED(hr))
            {
               hr=pStyleSheet->load(_variant_t
                  (_T("d:\\inetpub\\wwwroot\\samplexsl.xml")), &sResult);
               if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
               {
                  hr=pIXSLTemplate->putref_stylesheet(pStyleSheet);
                  if(SUCCEEDED(hr))
                  {
                     hr=pIXSLTemplate->createProcessor(&pIXSLProcessor);
                     SUCCEEDED(hr) ? 0 : throw hr;
                     if(pIXSLProcessor)
                     {
                        hr=CoCreateInstance(CLSID_DOMDocument, NULL,
                           CLSCTX_SERVER, IID_IXMLDOMDocument2,
                           (LPVOID*)(&pIXMLDOMDocument));
                        SUCCEEDED(hr) ? 0 : throw hr;

                        if(pIXMLDOMDocument)
                        {
                           hr=pIXMLDOMDocument->put_async(VARIANT_FALSE);
                           if(SUCCEEDED(hr))
                           {
                              hr=pIXMLDOMDocument->load(_variant_t
( _T("d:\\inetpub\\wwwroot\\sampleXSLWithParam.xml")), &sResult);
                              if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
                              {
                                 hr=pIXSLProcessor->put_input(_variant_t
                                     (pIXMLDOMDocument));
                                 if(SUCCEEDED(hr))
                                 {
                                    hr=pIXSLProcessor->addParameter
( _T("fooText"), _variant_t(_T("Add Parameter Test")), _T(""));
                                    if(SUCCEEDED(hr))
                                       bResult=TRUE;
                                       hr=pIXSLProcessor->transform(
                                          &sResult);
                                    if(SUCCEEDED(hr)&& (sResult ==
                                       VARIANT_TRUE))
                                    {
                                       pIXSLProcessor->get_output(
                                          &varValue);
                                       ::MessageBox(NULL,
                                       _bstr_t(varValue),
                                       _T("Transformed Output"), MB_OK);
                                    }
                                 }
                              }
                           }
                           RELEASE(pIXMLDOMDocument);
                        }
                     }
                  }
               }
            }
            RELEASE(pStyleSheet);
         }
         RELEASE(pIXSLTemplate);
      }
   }
   catch(...)
   {
      CHECK_AND_RELEASE(pIXSLTemplate);
      CHECK_AND_RELEASE(pStyleSheet);
      CHECK_AND_RELEASE(pIXMLDOMDocument);
      DisplayErrorToUser();
   }
   return bResult;
}
The stylesheet ? "d:\\inetpub\\wwwroot\\sampleXSLWithParam.xml"

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:myObj="urn:my-object">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="fooText"/>

<xsl:template match="/">
<xsl:element name="bar">
<xsl:value-of select="$fooText"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
Output (in a message box)

<?xml version=?1.0? encoding=?UTF-16??>
<bar>
Add Parameter Test
</bar>
0
 
LVL 1

Expert Comment

by:vijayneema
ID: 6424773
Thanks chabaud,

can you tell me how to do the same in JavaScript on Client side.

i am having same type of query i am using XML & XSL in one of my HTML page, and i want this variable to insert at the client side Please help me .... with some example.

bye
Vijay
0
 
LVL 4

Expert Comment

by:chabaud
ID: 6427844
Here is an example with javascript and "xml data island":

<html><head><title>XSL test</title></head><body>

<!-- ********************* XML ********************* -->

<XML id="oXml">
<X>
<GROUPA>
<A>1</A>
<A>2</A>
<A>3</A>
<A>4</A>
</GROUPA>
<GROUPB>
<A>1</A>
<A>3</A>
</GROUPB>
</X>
</XML>

<!-- ********************* XSL ********************* -->

<XML id="oXSL">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         version="1.0" >

<xsl:output method="html" indent="yes" encoding="UTF-8" version="1.0"/>

<xsl:template match = "/">
A from GROUPA not in GROUPB:<br/>
<xsl:for-each select="//GROUPA/A[ not( .=(//GROUPB/A)) ]">
     <li><xsl:value-of select="."/></li>
</xsl:for-each>

</xsl:template>

</xsl:stylesheet>
</XML>

<!-- ********************* HTML ********************* -->

<div id="DivLookUp" align="center"></div>

<script language="javascript">
    DivLookUp.innerHTML=oXml.transformNode( oXSL.documentElement);
</script>

</body></html>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question