I am using XSLT Transformation and need to put some data in CDATA section and that value is present in a variable.
Query: How to access variable in CDATA ?
Sample Given Below:
<xsl:attribute name ="attributeName">
<![CDATA[
I need to access some variable here like
*<xsl:value-of select ="$AnyVarible"/>*
]]>
</xsl:attribute>
How can I use varibale in CDATA ?
Note: I can not use --> <![CDATA[<xsl:value-of select ="$AnyVarible"/>]]>
Thanks in advance.
I got the solution for this...FYI for everyone...
<xsl:text
disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:value-of select ="$AnyVarible"/>
<xsl:text
disable-output-escaping="yes">]]></xsl:text>
CDATA is just text like any other element contents...
But using the xsl:output element you should be able to specify which elements are to be written as CDATA with the cdata-section-elements attribute.
EDIT:
Now that there is a valid sample, I guess you mean this:
<xsl:attribute name ="attributeName">
<![CDATA[
I need to access some variable here like
*]]><xsl:value-of select ="$AnyVarible"/><![CDATA[*
]]>
</xsl:attribute>
If you want to include CDATA sections in your output, you should use the cdata-section-elements atribute of xsl:output. This is a list of element names. Any such elements will have their text content wrapped in CDATA.
<xsl:output cdata-section-elements ="foo" />
<foo>
<xsl:value-of select="$bar' />
</foo>
Related
In one of my application, I am trying to convert the response of my service with the help of xslt on datapower.
In one of the response scenario, I need to show an xml something like below:
<data contentType="text/xml;charset=utf-8" contentLength="80"><![CDATA[Your request cannot be processed]]></data>
But my XSLT fails on datapower and it shows ">" and "<" in place of ">" and "<".
Below are my some of the attempted templates. Kindly have a look and suggest any correction:
Attempt 1:Tried with ">" and "<"
<xsl:param name="mask" select="'Your request cannot be processed'"/>
<xsl:template match="*" mode="copyFault">
<xsl:text disable-output-escaping="yes"><data contentType="text/xml;charset=utf-8" contentLength="80"><![CDATA[</xsl:text>
<xsl:value-of select="$mask" />
<xsl:text disable-output-escaping="yes">]]></data></xsl:text>
</xsl:template>
Attempt 2:Tried with HEX values
<xsl:param name="mask" select="'Your request cannot be processed'"/>
<xsl:variable name="lessThan" select="'<'"/>
<xsl:variable name="GreaterThan" select="'>'"/>
<xsl:template match="*" mode="copyFault">
<xsl:value-of disable-output-escaping = "yes" select="$lessThan"/>
<xsl:text>data contentType="text/xml;charset=utf-8" contentLength="80"</xsl:text>
<xsl:value-of disable-output-escaping = "yes" select="$GreaterThan"/>
<xsl:value-of disable-output-escaping = "yes" select="$lessThan"/>
<xsl:text>![CDATA[</xsl:text>
<xsl:value-of select="$mask" />
<xsl:text>]]</xsl:text>
<xsl:value-of disable-output-escaping = "yes" select="$GreaterThan"/>
<xsl:value-of disable-output-escaping = "yes" select="$lessThan"/>
<xsl:text>/data</xsl:text>
<xsl:value-of disable-output-escaping = "yes" select="$GreaterThan"/>
</xsl:template>
Please let me know what should I do to get the xml in proper format from datapower.
Thanks.
The usual way in XSLT to output a certain XML element is a literal result element so using
<data contentType="text/xml;charset=utf-8" contentLength="80">Your request cannot be processed</data>
in your XSLT will then output that element in the result. If you want to populate the element with a variable or parameter value then use e.g.
<data contentType="text/xml;charset=utf-8" contentLength="80"><xsl:value-of select="$mask"/></data>
If the XSLT processor is in charge of serializing the result to a file or string and you want some element like the data element to have a CDATA section as the content then declare e.g. <xsl:output cdata-section-elements="data"/> as a child of xsl:stylesheet (or xsl:transform if you have named the root element that way).
disable-output-escaping is a thoroughly nasty feature: it doesn't work on all processors, and if it's supported at all, it only works when the transformation output is fed directly into an XSLT-aware serializer, so it depends on how you are running the transformation.
It's much better to avoid disable-output-escaping when you can, and there's certainly no evidence you need it here. The requirement to output a CDATA section is somewhat unusual (any well-written application reading XML doesn't care whether the text is in a CDATA section or not), but if you really need it, then you can usually achieve it using <xsl:output cdata-section-elements="data"/>. (Though again, this only works if the output is fed into an XSLT-aware serializer.)
Certainly, generating start and end tags using disable-output-escaping is very poor practice.
I am trying to put a specific condition on the basis of preceding sibling check.
I tried various options but nothing worked.
Here is the sample XML
<abc>
<title>something</title>
<element>1</element>
<element>2</element>
<element>3</element>
<element>4</element>
</abc>
I have a template match for <element> tag and I want to check if its immediate element is <title> Do some additioanl processing else do some other processing. Any pointers appriciated.
<xsl:template match="element">
I am boring. <xsl:value-of select="." />
</xsl:template>
<xsl:template match="element[preceding-sibling::*[1][self::title]]">
I am special. <xsl:value-of select="." />
</xsl:template>
If the current context is an element element then you can get its nearest preceding sibling element (regardless of name) using
preceding-sibling::*[1]
and so to check whether that element is a title you can use
preceding-sibling::*[1][self::title]
<xsl:template match="element[preceding-sibling::element()[1][self::title]]"/>
I have in an input file:
<a></a>
<b/>
<c>text</c>
I need to converting this to string. Using transformer I am getting below output:
<a/> <!-- Empty tags should not collapse-->
<b/>
<c>text</c>
If I use xslt and output method is "HTML", I get the below output:
<a></a> <!-- This is as expected-->
<b></b> <!-- This is not expected-->
<c>text</c>
I want the structure same as in input file. It is required in my application since I need to calculate index and it will be very difficult to change the index calution logic.
What would be the correct XSLT to use?
What XSLT processor? XSLT is merely a language to transform xml so "html output" is dependent on the processor.
I'm going to guess this first solution is too simple for you but i've had to use this to avoid processing raw html
<xsl:copy-of select="child::node()" />
as this should clone the raw input.
In my case, I have used the following to extract all nodes that had the raw attribute:
<xsl:for-each select="xmlData//node()[#raw]">
<xsl:copy-of select="child::node()" />
</xsl:for-each>
Other options:
2) Add an attribute to each empty node depending on what you want it to do later ie role="long", role="short-hand".
3)
Loop through each node (xsl:for-each)
<xsl:choose>
<xsl:when test="string-length(.)=0"> <!-- There is no child-->
<xsl:copy-of select="node()" />
</xsl:when>
<xsl:otherwise>
...whatever normal processing you have
</xsl:otherwise>
4) Redefine your problem. Both are valid XHTML/XML, so perhaps your problem can be reframed or fixed elsewhere.
Either way, you may want to add more information in your question so that we can reproduce your problem and test it locally.
P.S. Too much text/code to put in a comment, but that's where this would belong.
A possible alternative is to use disable-output-escaping like this:
<xsl:text disable-output-escaping="yes"><a></a></xsl:text>
But I understand that this is a dirty solution...
Ia using XSLT 1.0 and I do have a XML while looks like this
<item name="note"><value><p>Add the <bean> tag pased below to the <beans> element in this file .... </value></item>
I want to display it like this in HTML
Add the <bean> tag passed below to the <beans> element in this file.
Note here that the <p> will be converted to a paragraph tag as I use disable-output-escaping= yes.
This is what I have in my xslt
<xsl:template match="item[#name='note']">
<xsl:value-of select="value" disable-output-escaping="yes" />
</xsl:template>
With this xslt it ignores the bean and beans xml and it does not get displayed in the page. How do I make sure to display it the way I want it?
The problem is that some of your entities have been "double escaped".
<bean> should be <bean>
I've got some XML elements with a number attached as more are available.
Such as this:
<Images>
<Image1>C:\Path\To\AnImage</Image1>
<Image2>C:\Path\To\AnotherImage</Image2>
</Images>
The amount of Images in each XML doc is variable. How can I make sure that my XSL file will show all elements inside the tag?
I also want to put each of the strings inside each ImageX tag inside a Img src="stringfromxmlelement" with XSL? Is this possible?
Tony
<xsl:template match="Images/*[starts-with(name(),'Image']">
<img src="{.}" />
</xsl:template>
BTW, perhaps you can't change the XML tag names, but it would better to name the inner tags as Image rather than ImageX, which is probably unneccesary.
I'd try something along these lines:
<xsl:template match="Images">
<xsl:for-each select="*">
<img src="{text()}" />
</xsl:for-each>
</xsl:template>