Jump to content

CDATA in XSLT ?


prichardson

Recommended Posts

Hello,I am pulling data from an external company xml = when I pull the data in - everything works - except there is one tag that has CDATA enclosing the data in this tag. I am having problems trying to display that data correctly. Here below is the xml tag

- <custom_element>- <element description="Testimonial">- <![CDATA[ A two bedroom terraced house which has recently undergone a full refurbishment and benefits from having a 25'8 double reception. The accommodation comprises; 25'8 double reception, separate kitchen, two bedrooms, family bathroom and private rear garden. Albany Road is located with a wide selection of amenities close to hand which includes; local shopping, The Watermans art centre and the river Thames. Transport links include Brentford BR (direct to Waterloo) a selection of bus routes and the A4/M4 into town and out to Heathrow and the West Country.   ]]>   </element>  </custom_element>

This is my xslt tag without any CDATA tags around it:

	<xsl:choose>	 <xsl:when test="custom_element/element description = '{Testimonial}'"></xsl:when><xsl:otherwise><xsl:value-of select="custom_element/element description"/></xsl:otherwise>	</xsl:choose>

and this is my attempt with the CDATA tags around the code above:

	<xsl:choose>	 <xsl:when test="custom_element/element description = <![CDATA['{Testimonial}']]>"></xsl:when><xsl:otherwise><xsl:value-of select="custom_element/element description"/></xsl:otherwise>	</xsl:choose>

** How do you wrap the CDATA tags around the xml tag of which the data i want to display? ** Is this line <xsl:when test="custom_element/element description = '{Testimonial}'"></xsl:when> or should it be <xsl:when test="custom_element/element description = "Testimonial"= '{Testimonial}'"></xsl:when> or this <xsl:when test="custom_element= '{element description = "Testimonial"}'"></xsl:when> from the 1st code i displayed- which has extra information in the tag.... maybe all 3 are wrong??

Link to comment
Share on other sites

What exactly is it that you're trying to do? Show the CDATA text and/or react upon it? Well, if so, ignore CDATA nodes. As far as the XSLT processor is concern, those are text nodes.The "element description " part in all of your code is wrong. If you want to select an attribute, regardless of whether you want to test against it or just output it, you need to use "element/@description" instead. That could be the only thing that's causing you error(s).Try it like that:

<xsl:choose>	 <xsl:when test="custom_element/element[@description = 'Testimonial']">DO</xsl:when><xsl:otherwise><xsl:value-of select="custom_element/element[@description]"/></xsl:otherwise></xsl:choose>

Again, I'm not sure what you want to do, but what this does is that if there's any "element" element with a "description" attribute that has a value of 'Testimonial', it says "DO" (you can of course edit the action within the xsl:when yourself). If there isn't, it just outputs the string value of all "element" elements that have a "description" attribute, regardless of the "description" attribute's value.

Link to comment
Share on other sites

Hi boen_robot,Thanks for replying to my post. You helped me a couple months ago - and was a great help.Sorry i have not been able to explain clearly. I'll try again...A new tag within the xml data has been inserted into the xml which is from an external company. This tag is:- <custom_element>- <element description="Testimonial">Some of the properties that have content in this tag... like this one which has...- <custom_element>- <element description="Testimonial">- <![CDATA[ A two bedroom terraced house which has recently undergone a full refurbishment and benefits from having a 25'8 double reception. The accommodation comprises; 25'8 double reception, separate kitchen, two bedrooms, family bathroom and private rear garden. Albany Road is located with a wide selection of amenities close to hand which includes; local shopping, The Watermans art centre and the river Thames. Transport links include Brentford BR (direct to Waterloo) a selection of bus routes and the A4/M4 into town and out to Heathrow and the West Country. ]]> </element> </custom_element>**I am trying to be able to display the content on my page - if there is any content in that tag.The code you gave me - it make the page appear and have no errors, but the content of the property does not appear as it does in the xml tag . Additionally I do not understand why this property nows does not appear at all on the page? What would you suggest to have the content with that tag appear on the page?Many thanks, again

Link to comment
Share on other sites

I think I'll need a full XML and XSLT samples for that. The XPath doesn't lead to an existing node for some reason, but without knowing the complete picture, I can't know the reason for that.

Link to comment
Share on other sites

Here is a property ...

- <property id="2805" prevref="" last_updated="09/11/2007 09:26">  <id>2805</id>   <department>RL</department>   <branch>HO</branch> - <address>  <address1>12 Albany Road</address1>   <town>Brentford</town>   <county>Middlesex</county>   <postcode>TW8 0NF</postcode>   <property_location>Brentford</property_location>   <display_address>Albany Road, TW8</display_address>   </address>- <custom_element>  <element description="Testimonial">A two bedroom terraced house which has recently undergone a full refurbishment and benefits from having a 25'8 double reception. The accommodation comprises; 25'8 double reception, separate kitchen, two bedrooms, family bathroom and private rear garden. Albany Road is located with a wide selection of amenities close to hand which includes; local shopping, The Watermans art centre and the river Thames. Transport links include Brentford BR (direct to Waterloo) a selection of bus routes and the A4/M4 into town and out to Heathrow and the West Country.</element>   </custom_element>  </property_summary>  </property>

I asked the external company to remove the CDATA - which they have done now. But it still does not display the property?thanks

Link to comment
Share on other sites

Hi boen_robot Thanks - since getting the cdata off - and i released that i had to add a bit more onto the path to the tag for it to work - <xsl:value-of select="property_summary/custom_element/element[@description = 'Testimonial']"/>this works now for one of the properties. But for some reason one of the properties seems to not display on the page (the one which i showed the property in the last post. I just can't work out why that is not displaying now - even though it is in the xml sheet???cheers

Link to comment
Share on other sites

It's not the CDATA's fault. That's what I'm trying to tell you. CDATAs are usually visible as text nodes. Again, show me the complete XSLT file, or at least a new (complete!) sample demonstrating the problem. The XML sample also doesn't seem to show a complete valid copy of a sample you're working with (the "-" sign says it).BTW, when you copy a source code, please right click and select "View source" and copy the code from there, not from the browser. You'll get rid of the "-" that way.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...