
12 Answers, 1 is accepted
Could you please provide a bit more information about your DataSource configuration and scenario?
Regards,Rosen
the Telerik team

However, now I have another problem.
Data in an XML file that is wrapped in CDATA tags is ignored?
The JavaScript code:
// === GET RESOURCE FILE / LOCALIZE PAGE ===========================================================================================================
$(document).ready(function () {
var current_file = "resources" + location.pathname + get_culture_path_modifier() + ".xml";
var datasource_resource = new kendo.data.DataSource(
{
transport:
{
read: current_file
},
schema:
{
type: "xml",
data: "/root/data",
model:
{
fields:
{
name: "@name",
value: "value/text()"
}
}
},
change: function () {
if (datasource_resource.total() != undefined) {
var i = 0;
for (i = 0; i < datasource_resource.total(); i++) {
var item_name = datasource_resource.at(i).name;
var item_value = datasource_resource.at(i).value;
var element_id = document.getElementById(item_name);
if (element_id != null) {
var element_type = element_id.tagName.toLowerCase();
if (element_type.startsWith('meta') == true) {
element_id.setAttribute('content', item_value);
}
else if (element_type.startsWith('link') == true) {
element_id.setAttribute('href', item_value);
}
else {
element_id.textContent = item_value;
}
}
}
}
}
});
datasource_resource.read();
});
A portion of the XML data file:
<
data
name
=
"title"
xml:space
=
"preserve"
>
<
value
>
My Site
</
value
>
</
data
>
<
data
name
=
"meta_description"
xml:space
=
"preserve"
>
<
value
>
<![CDATA[
My Site is a template & is under development.
]]>
</
value
>
</
data
>
Any value not in CDATA tags (see title item above) is returned.
Any value in CDATA tags (see meta_description item above) is returned as an empty string.
The behavior you are experiencing is due to the content in the data attribute. In the sample XML, in the tags with CData tag, there is spaces before and after it, this will create several text nodes, as schema is set to get the last text node from a given tag it will get the last space, thus an empty tag. Therefore, in case with CData you should make sure the CData is the last child text node, for example by removing all of the spaces.
Greetings,Rosen
the Telerik team

=
"preserve"
being declared? I haven't ever encountered this problem in .NET when working with RESX files (the behavior I am trying to mimic).
I'm afraid that there is not difference if there is space preserve set or not. Please take a look the following jsfiddle which demonstrates how the element is represented after it been parsed using the browser API:
Rosen
the Telerik team

Is there a way to:
-
target the #cdata-section element directly or...
-
remove the surrounding white space so only the element containing content is seen
As an urgent development suggestion / feature request: Your XML parser is working correctly by displaying each text item is another node. However, I think that in the future it should honor xml:space
=
"preserve"
and treat everying (including the white space) as a single element.Straight to your questions:
- I'm afraid not
- You should format the data before it is feed to the dataSource. For example if it is received from a local service, you should verify that there are not spaces between the nodes and CData tags.
- Unfortunately, we do not have our own XML parser but use the browser APIs, similar to the sample code from my previous message. Therefore, there is little we can do when the browser parser does not honor the attribute in question.
Rosen
the Telerik team

var xml, tmp,
data = '<
data
name
=
"meta_description"
xml:space
=
"preserve"
>' +
'<
value
>
<![CDATA[My Site is a template & is under development.]]>
</
value
>' +
'</
data
>';
try {
if (window.DOMParser) { // Standard
tmp = new DOMParser();
xml = tmp.parseFromString(data, "text/xml");
} else { // IE
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = "false";
xml.loadXML(data);
}
} catch (e) {
xml = undefined;
}
var element = xml.documentElement.firstChild,
text = "";
for (var node = element.firstChild; node; node = node.nextSibling) {
if (!(node.nodeType == 3 && node.nodeValue.replace(/^\s+|\s+$/g, '') == '')){
text += node.nodeValue + "<
br
/>";
}
}
document.getElementById("log").innerHTML = text;
Unfortunately, this approach will have a small drawback. As it skips all empty nodes, the resulting object structure (which is how it is used in the DataSource) will be missing fields/properties. As can be observed in this slightly modified sample:
Greetings,
Rosen
the Telerik team

You would probably need to do some additional checks to see if it was the last child element.
If no value had been found and it was the last child, return the value of the last child, regardless if it is an empty string.
I appreciate you considering this for a future revision! It definitely is needed by us, as we can not guarantee editors will not add those extra spaces in the file.

I'm afraid that there is no change in the behavior discussed in this thread.
Regards,Rosen
the Telerik team