0

I am trying to use an xsl variable in a javascript block in my xslt file and I am at my wit's end.

Here is the XSLT (edited for public consumption):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="html" indent="yes"/>
    <xsl:template match="a">
         <xsl:variable name="myVar" select="xpath to the node"/>

    <script type='text/javascript'>

            googletag.cmd.push(function() {
        ...
      googletag.pubads().setTargeting('label', '<xsl:value-of select="$myVar"/>');
            googletag.enableServices();
            });

  </script> 

    </xsl:template>
</xsl:stylesheet>

If I transform the XML in Oxygen, this code works fine. But when I run it through my servlet, which uses javax.xml.transform.Transformer.transform(Source xmlSource, Result outputTarget) throws TransformerException, I get this: googletag.pubads().setTargeting('label', ' ');

Can anyone suggest a possible reason for this discrepancy between Oxygen and my servlet?

1
  • Is your issue that when you run through oXygen the output has a value for the $myVar and when run through the servlet there is no value for value-of? It could be a difference between what docs are sent to the transforms. What is the xpath to the node? Are you sure that it is matching? Reduce to a more simple test of just an XSLT that produces the value of the $myVar to verify that you are correctly matching when run through the servlet. Commented Aug 15, 2013 at 1:23

1 Answer 1

1

Try this instead as contents of your variable:

<xsl:variable name="myVar" select="'xpath to the node'"/>

This ensures the contents is seen as a literal string. Possibly Oxygen is forgiving enough to ignore it and your Java is (correctly) not -- it's an error, because inside the quoted string you should put an expression.

The outer double quotes do not take part in forming the expression, only what's inside. That way you can express either the string '1+2' or the sum 1+2 in a variable.

Sign up to request clarification or add additional context in comments.

2 Comments

I still haven't resolved this issue but I believe we may have XSLTs that are clashing. This is something that can't be simulated in Oxygen. I will post as soon as I have some hard evidence. Thanks.
I'm almost too embarrassed to admit how this issue was resolved. The reason that the XSLT was working in Oxygen is that I was feeding it XML with the node I needed. It turned out that in production, the XSLT was using different XML, which did not include the node I needed. The simple fix was to add that node to the XML and viola, mystery solved. Duh! Thanks to Jongware and Mads Hansen for helping me home in on this blunder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.