0

I'm trying to pass a string from XSL to Javascript. I was able to pass integers but it doesn't seem to accept strings. The following is the basic structure of my code. If I delete "Test" from "Test 1" I do get the alertbox with the number 1. But once there's text inside, the alertbox does not come up.

XML:

<TEST>
  <TITLE>Test 1</TITLE>
<TEST>

<TEST>
  <TITLE>Test 2</TITLE>
<TEST>

XSL:

<xsl:for-each select="//TEST">
<xsl:call-template name="TEST_TEMPLATE"/>
</xsl:for-each>

...

<xsl:template name="TEST_TEMPLATE">

<script type="text/javascript">
<![CDATA[var title = ]]><xsl:value-of select="./TITLE" /><![CDATA[;

alert(title);]]>
</script>

Your help is much appreciated.

Thanks!

2 Answers 2

2

You are not "passing a string from XSL to Javascript". Your XSLT code is generating HTML, part of which happens to be a script element. It is easy to see that the script element you are generating contains the text:

var title = Test 1;

which will obviously not do anything useful when it comes to be executed.

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

Comments

1

You need qoutes around the title. The template you have just outputs the value of the string, but it has to be in qoutes to be valid javascript. I think this would work:

<xsl:template name="TEST_TEMPLATE">

<script type="text/javascript">
<![CDATA[var title = "]]><xsl:value-of select="./TITLE" /><![CDATA[";

alert(title);]]>
</script>

Comments

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.