tags:

views:

39

answers:

1

I am inserting two variables countryVar and langVar into a node in my xml document.

<link><![CDATA[/<% = countryVar %>/<% = langVar %>/products/index.aspx]]></link>

I am utilizing the link node in a xslt like this.

<a href="{link}"><xsl:value-of select="linkName"/></a>

The value of link prints out exactly like it is in the xml document. Is there any way to get the two vb.net variables countryVar and langVar to process and print out there value? The values are stored in cache and are pulled into the page.

Thanks

+1  A: 

XSLT doesn't know anything about the variables in the (VB.NET) program that initiated the transformation.

There are two different ways to pass values of variables to the transformation:

  1. Pass them as global parameters to the transformation. Read more how to do this together with XslCompiledTransform.Transform() :::: here.

  2. Modify the source XML document (after loading it, to contain the values of the variables in certain nodes.

I recommend method 1. above.

UPDATE:

Obviously you are using a variation of method 2. above. In this case it is not a good idea at all to put the values of the variables inside the same string.

Much better than:

<link><![CDATA[/<% = countryVar %>/<% = langVar %>/products/index.aspx]]></link>

(and it would be straightforward to process with XSLT afterwards, is:

<link>
  <country><% = countryVar %></country>
  <slash>/</slash>
  <lang><% = langVar %></lang>
  <tail-link>/products/index.aspx</tail-link>
  <linkName>Whatever link text</linkName>
</link>

Then the XSLT code that processes this will be just:

 <a href="{link}"><xsl:value-of select="linkName"/></a>

(this assumes that you have <xsl:strip-space elements="*"/> at the global level in your stylesheet).

and the values of the variables are:

link/country

and

link/lang
Dimitre Novatchev
@Dimitre: Your answer is rigth. But I think that the variable values are now contained in the input. The OP could parse the `link` element...
Alejandro
@Alejandro: To place the variable values inside a string doesn't make sense! So, I am giving him a good advice. Also, there is no `linkName` element in the provided XML document.
Dimitre Novatchev
@Alejandro: He says that he doesn't get the values but he gets what he's showing to us **literally**. So, this is not an XSLT question at all, but an ASP question!
Dimitre Novatchev
@Alejandro: Seems like I am giving him a good advice now...
Dimitre Novatchev
@Dimitre: +1 Excellent and complete answer!
Alejandro
I wound up passing countryVar and lang Var into the xsl from the calling page rather than from the xml.
BillZ
Thanks for another great answer Dimitri!
BillZ