Can i embed LINQ results into CDATA? - html

Can i merge the values returned from a LINQ query (in the next example c.day) with CDATA like...
Dim Result = <items>
<%= From c In db.News Select _
<item>
<day><![CDATA[<font size="30" color="#7CBEBD"><%= c.day %></font>]]></day>
</item> %>
</items>

I don't think you can use CDATA section literals and embedded expressions inside them with VB.NET but you can certainly construct a CDATA section node with new XCData() as in the following example:
Dim words As String() = {"foo", "bar", "baz"}
Dim doc As XDocument =
<?xml version="1.0"?>
<root>
<items>
<%= From word In words
Select <item>
<%= New XCData("<font size=""3"">" + word + "</font>") %>
</item>
%>
</items>
</root>
That serializes as
<root>
<items>
<item><![CDATA[<font size="3">foo</font>]]></item>
<item><![CDATA[<font size="3">bar</font>]]></item>
<item><![CDATA[<font size="3">baz</font>]]></item>
</items>
</root>

Related

How can one match a subnode in xslt in xml?

I've inherited a project that wants to use xslt to transform some html. Matching works with '/', but I can't get it to run on a subnode
I've found some code snippet on mozilla, that applies xslt transformation to html on mozilla, the code works https://developer.mozilla.org/en-US/docs/Web/XSLT/XSLT_JS_interface_in_Gecko/Advanced_Example.
The Problem is that I'm not able to template match the node "firmenliste"
What I use is:
var xslRef;
var xslloaded = false;
var xsltProcessor = new XSLTProcessor();
var myDOM;
var xmlRef = document.implementation.createDocument("", "", null);
p = new XMLHttpRequest();
p.open("GET", "xsl/FirmenListe.xsl",false);
p.send(null);
xslRef = p.responseXML;
xsltProcessor.importStylesheet(xslRef);
xmlRef = document.implementation.createDocument("", "", null);
// we want to move a part of the DOM from an HTML document to an XML document.
// importNode is used to clone the nodes we want to process via XSLT - true makes it do a deep clone
var myNode = document.getElementById("example");
var clonedNode = xmlRef.importNode(myNode, true);
// after cloning, we append
xmlRef.appendChild(clonedNode);
var fragment = xsltProcessor.transformToFragment(xmlRef, document);
// clear the contents
document.getElementById("example").innerHTML = "";
myDOM = fragment;
// add the new content from the transformation
document.getElementById("example").appendChild(fragment)
The corresponding html and xslt looks like:
<xml id="Data">
<data id="example" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<firmenliste></firmenliste>
</data>
</xml>
<?xml version ='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="/">
b
<xsl:apply-templates select="firmenliste"/>
</xsl:template>
<xsl:template match="firmenliste">
A
</xsl:template>
</xsl:stylesheet>
The output should be
<xml id="Data">
<data id="example" xmlns:dt="urn:schemas-microsoft-com:datatypes">
bA
</data>
</xml>
But what i get is
<xml id="Data">
<data id="example" xmlns:dt="urn:schemas-microsoft-com:datatypes">
b
</data>
</xml>
Edit: The problem is reproducible in https://next.plnkr.co/edit/Yvc59BPQmI1PHlSy?open=lib%2Fscript.js&preview
I think the main problem is that you start with elements in a HTML DOM document which since HTML5 are by definition in the XHTML namespace http://www.w3.org/1999/xhtml and then clone and copy them to an XML document where they keep their namespace but where in XSLT/XPath a path or match pattern like firmenliste selects or matches elements of that name in no namespace and not in the XHTML namespace.
So using
<xsl:template match="/">
b
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="xhtml:firmenliste" xmlns:xhtml="http://www.w3.org/1999/xhtml">
A
</xsl:template>
instead would fix that problem: https://next.plnkr.co/edit/tsB9qwCafLodg8Rz?open=lib%2Fscript.js&preview
But the whole approach of using non-defined elements like xml or firmenliste in HTML and moving between HTML DOM and XML DOMs is asking for trouble in my experience. Consider to keep the XML data you want to transform outside of the HTML document in a separate XML document, only use XSLT on XML documents and only use its transformation result to be inserted into an HTML DOM if you have used transformToFragment with the owning HTML document as the second argument.
In your xslt, while matching root node with '/', You need to give whole xPath to match <firmenliste> in <xsl:apply-templates>
Try the same by replacing the line <xsl:apply-templates select="firmenliste"/>
with <xsl:apply-templates select="/xml/data/firmenliste"/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
b
<xsl:apply-templates select="/xml/data/firmenliste" />
</xsl:template>
<xsl:template match="firmenliste">
A
</xsl:template>
</xsl:stylesheet>

MYSQL - LOAD XML multiple repeating (deplicate) tags as one string

Problem to load multiple repeating tags "<image></image>" with data to one <images> table cell.
XML
<posts>
<item>
<id>1</id>
<type>post</type>
<url>www.url.com/1</url>
<date>2016-06-15</date>
<image>some url/1xxx.jpg</image>
<image>some url/1yyy.jpg</image>
<image>some url/1zzz.jpg</image>
</item>
<item>
<id>2</id>
<type>post</type>
<url>www.url.com/2</url>
<date>2016-06-12</date>
<image>some url/2xxx.jpg</image>
<image>some url/2yyy.jpg</image>
<image>some url/2zzz.jpg</image>
<image>some url/2www.jpg</image>
</item>
<item>
<id>3</id>
<type>post</type>
<url>www.url.com/3</url>
<date>2016-06-12</date>
<image>some url/3fff.jpg</image>
</item>
</posts>
Code
Now it loads only last <image> tag from <item>
LOAD XML local infile 'D:\\demo.xml'
REPLACE
INTO TABLE posts CHARACTER SET UTF8
ROWS IDENTIFIED BY '<item>'
(#id, #type, #url, #date, #image)
SET id=#id, type=#type, url=#url, date = str_to_date(#date, '%Y-%m'), images=#image;
How to store all duplicate <image> tags as images VARCHAR or TEXT
Consider transforming your XML with XSLT to normalize the item and images into one-to-many tables. Below uses PHP to run XSLT but most general purpose languages can run XSLT 1.0 scripts including PHP, Perl, Python, Java, C#, VB. Specifically, the transformation will break <image> tags out of <item> keeping corresponding <id> and maintain two sets of tags to upload to two MySQL database tables.
XSLT Script (save as .xsl file)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/posts">
<xsl:copy>
<xsl:apply-templates select="item"/>
<xsl:apply-templates select="item/image"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<xsl:copy>
<xsl:copy-of select="*[local-name()!='image']"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item/image">
<images>
<itemid><xsl:value-of select="ancestor::item/id"/></itemid>
<xsl:copy-of select="."/>
</images>
</xsl:template>
</xsl:stylesheet>
PHP Script
<?php
$cd = dirname(__FILE__);
// LOAD XML AND XSL FILES
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->load('Original.xml');
$xslfile = new DOMDocument('1.0', 'UTF-8');
$xslfile->load('XSLT_Script.xsl');
// TRANSFORM XML with XSLT
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslfile);
$newXml = $proc->transformToXML($xml);
// SAVE TO FILE
file_put_contents('Output.xml', $newXml);
?>
Output (images contain item id)
<?xml version="1.0"?>
<posts>
<item>
<id>1</id>
<type>post</type>
<url>www.url.com/1</url>
<date>2016-06-15</date>
</item>
<item>
<id>2</id>
<type>post</type>
<url>www.url.com/2</url>
<date>2016-06-12</date>
</item>
<item>
<id>3</id>
<type>post</type>
<url>www.url.com/3</url>
<date>2016-06-12</date>
</item>
<images>
<itemid>1</itemid>
<image>some url/1xxx.jpg</image>
</images>
<images>
<itemid>1</itemid>
<image>some url/1yyy.jpg</image>
</images>
<images>
<itemid>1</itemid>
<image>some url/1zzz.jpg</image>
</images>
<images>
<itemid>2</itemid>
<image>some url/2xxx.jpg</image>
</images>
<images>
<itemid>2</itemid>
<image>some url/2yyy.jpg</image>
</images>
<images>
<itemid>2</itemid>
<image>some url/2zzz.jpg</image>
</images>
<images>
<itemid>2</itemid>
<image>some url/2www.jpg</image>
</images>
<images>
<itemid>3</itemid>
<image>some url/3fff.jpg</image>
</images>
</posts>
SQL (two tables to upload)
-- POSTS TABLE
LOAD XML local infile 'C:\\Path\\To\\Output.xml'
REPLACE
INTO TABLE posts CHARACTER SET UTF8
ROWS IDENTIFIED BY '<item>'
(#id, #type, #url, #date)
SET id=#id, type=#type, url=#url, date=str_to_date(#date, '%Y-%m');
-- IMAGES TABLE
LOAD XML local infile 'C:\\Path\\To\\Output.xml'
REPLACE
INTO TABLE images CHARACTER SET UTF8
ROWS IDENTIFIED BY '<images>'
(#itemid, #image)
SET itemid=#itemid, image=#image;

&#160 shows up as a '?' question mark on HTML

i have a problem with XSLT...
<xsl:text> </xsl:text>
Then after generation, for some reason the resulting JSP file produces a '?' instead. What's wrong?
My recent system changes:
I changed Java5 -> Java6
Weblogic -> Weblogic12
Eclipse Ganymede -> Oracle Pack Eclipse
EDIT 1: <xsl:output method="xml"/>, encoding=UTF-8
The original XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="common.xsl"/>
<xsl:output method="xml"/>
...
<xsl:template name="makeLink">
<xsl:variable name="fieldtype" select="name()"/>
<xsl:variable name="currentNode"><xsl:value-of select="generate-id()"/></xsl:variable>
<xsl:variable name="appendSpace">
<xsl:for-each select="ancestor::ButtonList[position() = 1]/descendant::Button">
<xsl:if test="generate-id() = $currentNode and position() > 1">true</xsl:if>
</xsl:for-each>
</xsl:variable>
<a href="{$url}">
<xsl:attribute name="id">btn_<xsl:value-of select="Action"/></xsl:attribute>
<xsl:call-template name="populateAttributes">
<xsl:with-param name="fieldtype">
<xsl:value-of select="$fieldtype"/>
</xsl:with-param>
</xsl:call-template>
<xsl:copy-of select="#class"/>
<xsl:copy-of select="#style"/>
<xsl:text><span><span></xsl:text><xsl:value-of select="$buffer"/><xsl:text></span></span></xsl:text>
</a>
<xsl:if test="not(#omitWhiteSpace)">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:if test="ReadOnly and ReadOnly != 'someReadOnlyMethod'
and ReadOnly != 'someReadyOnlyMethod'
and ReadOnly != ''">
<xsl:text></c:if></xsl:text>
</xsl:if>
</xsl:template>
....
Transformed (after XSLT), and resulting JSP page:
<%# page contentType = "text/html;charset=GBK"%>
<%# page isELIgnored = "false"%>
<%# page language="java"
import=" my.controller.*, my.core.config.*, my.core.datastructure.*, my.core.error.*, my.core.util.*,
my.service.Constants, my.service.modulesvr.ModuleBean, myW.sn.*, java.util.Locale, java.util.Map"%>
<%# taglib uri="http://www.mycompany.com/my/tags/htmltag-10" prefix="html"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%MySn mySession = (MySn) session.getValue("MySn"); QuickSearchController mb = (mySession == null) ? null : (QuickSearchController)
mySession.getModuleBean(); String sessionToken = mySession.getSessionToken(); String htmlCharSet = mySession.getEncoding();
MyUsr user = mySession.getMyUsr(); String[] result; Object o;%>
.........
<span><span>NEW PROP</span></span> </c:if>
EDIT 2: it seems like if i use <xsl:text>&#160;</xsl:text> instead of <xsl:text> </xsl:text>...the problem seems to have gone away. In the JSP, it will appear as &#160 and on the browser, it is seen as a no-break space, which is expected.
That often happens if your encoding is wrong. What encoding are you writing your output in? How are you serving up the page? Possibly you are serializing in UTF-8 but trying to display in ISO-8859-1 (or Windows-1252), or vice-versa.
Check to see if the default encoding somewhere has changed.
Just because you say <xsl:output method="xml" encoding="UTF-8"/> doesn't mean that the program will honor it. Is the XSLT embedded in a piece of Java? Does the Java control the streams/readers/writers?
If you can save a portion of the file and dump it in HEX, you should quickly be able to find out. If you see 0xC2 0xA0 then your file is indeed in UTF-8. However, if you just see 0xA0 alone, then you are in ISO-8859-1 or one of its close relations.
It's also possible that the page is being rendered properly, but the page is being served up with the wrong encoding. Can you look at the headers returned, perhaps by using Firebug in Firefox or in Chrome "Web Developer->Information->View Response Headers" or by using the IE debug tools.

Unshift element on ArrayCollection

I have this ArrayCollection filled with a xml data coming from and HttpService request.
The generated XML
<items>
<item>
<id>1</id>
<name>Tom</name>
</item>
<item>
<id>2</id>
<name>Jerry</name>
</item>
<item>
<id>3</id>
<name>TV</name>
</item>
</items>
The script
[Bindable] private var dp:ArrayCollection;
private function onResult(event:ResultEvent):void{
dp = event.result.items.item;
}
Right, this dp is used as a dataProvider in a ComboBox
Is there a way to add another element (at the begining) of this ArrayCollection ?
I tried using unshift, but that only worked for an Array
Thanks.
Use addItemAt method.
dp.addItemAt(item, 0);
You can use the addItemAt method, adding it at index 0.
According to the ASDoc for the ArrayCollection class you can use the method 'addItemAt'

XmlPad problems with XPath for finding duplicate nodes

[EDIT] It seems that there is a bug in the XML Editor I'm using (XmlPad) that prevents the Xpath query from returning the correct results. I've tested the same query using two online tools (http://www.zrinity.com/xml/xpath/xpath.cfm and http://www.futurelab.ch/xmlkurs/xpath.en.html) and it seems to work. biziclop also commented that the query works correctly in Oxygen.
I've got this structure:
<?xml version="1.0" encoding="utf-8"?>
<root>
<itemlist>
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0002.0.00</code>
<category>708</category>
</item>
</itemlist>
<itemlist>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0016.0.00</code>
<category>51</category>
</item>
<item>
<code>0016.0.00</code>
<category>50</category>
</item>
<item>
<code>0869.0.00</code>
<category>52</category>
</item>
<item>
<code>0869.0.00</code>
<category>51</category>
</item>
<item>
<code>0869.0.00</code>
<category>50</category>
</item>
</itemlist>
</root>
I want find all items where the previous item has the same category.
This Xpath query:
//item[category = preceding-sibling::item[1]/category]
returns the following nodes:
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0869.0.00</code>
<category>52</category>
</item>
The last item node in the result set is incorrect, because the value of the previous item's category in the input is not 52 so it should not be returned.
Is there an Xpath query that will return the results I want?
This XPath is absolutely correct. I've tested it with Saxon XSLT processor as follows:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="//item[category
= preceding-sibling::item[1]/category]"/>
</xsl:template>
</xsl:stylesheet>
With results:
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
You might want also try alternatives:
//item[category = ./preceding-sibling::item[1]/category]
/root/itemlist/item[category = ./preceding-sibling::item[1]/category]