Solved

xsl variables

Posted on 2001-08-22
3
473 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RSS Feed Enclosure URL 1 121
Problem to event 3 96
API Soap Calls 4 113
Powershell script to zip up xml files in a directory 13 12
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

773 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