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'
Related
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>
Camel Route :
<camelContext xmlns="http://camel.apache.org/schema/spring">
<dataFormats>
<xmljson id="xmljson" />
</dataFormats>
<route id="route1">
<from uri="file:C:/Users/User1/InputXML"/>
<to uri="activemq:queue:MyThread1"/>
</route>
<route id="route2">
<from uri="activemq:queue:MyThread1"/>
<marshal ref="xmljson"/>
<bean ref="com.test.OutputProcessor"/>
</route>
</camelContext>
Input XML :
<?xml version="1.0" encoding="UTF-8"?>
<Message>
<to> Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</Message>
Actual output :
{"to":" Tove","from":"Jani","heading":"Reminder","body":"Don't forget me this weekend!"}
I want to customize this output. i want to add some mote attributes to the converted json. For Example i want the output json as
{
"inputs":[
{
"inputname":"to",
"inputValue":"Tove"
},
{
"inputname":"from",
"inputValue":"jani"
},
{
"inputname":"heading",
"inputValue":"Reminder"
},
{
"inputname":"body",
"inputValue":"Don't forget me this weekend!"
}
]
}
How this can be achieved ?
I think an AggregationStrategy might help:
1) Fist you add the aggregationStrategy to your route:
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:start"/>
<enrich strategyRef="aggregationStrategy">
<constant>direct:resource</constant>
<to uri="direct:result"/>
</route>
<route>
<from uri="direct:resource"/>
...
</route>
</camelContext>
<bean id="aggregationStrategy" class="com.ExampleAggregationStrategy" />
2) Then create the class that will get the Body of the message and transform it the way you want, and set the body to the Exchange again.
OBS: Here You will need to use a xml API to add the attributes you want to add.
public class ExampleAggregationStrategy implements AggregationStrategy {
public Exchange aggregate(Exchange original, Exchange resource) {
Object originalBody = original.getIn().getBody();
Object resourceResponse = resource.getIn().getBody();
Object mergeResult = ... // combine original body and resource response
if (original.getPattern().isOutCapable()) {
original.getOut().setBody(mergeResult);
} else {
original.getIn().setBody(mergeResult);
}
return original;
}
}
More here.
Is there anything preventing you from using an XSLT component? You can apply that to bring the input XML to a format that directly maps to your desired output JSON format and then push it to xmljson e.g. - (need some clean up to avoid some blank elements)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Message">
<inputs>
<xsl:for-each select="*">
<inputname><xsl:value-of select="name()" /> </inputname>
<inputvalue><xsl:value-of select="." /></inputvalue>
</xsl:for-each>
</inputs>
</xsl:template>
</xsl:stylesheet>
Use the Jackson library. You can programmatically change the output format. Unmarshal is only good for direct mapping and not enrichment. Essentially Unmarshal to xml, add a processor and then create your output Json format.
I used SpeechRecognizerUI to read user's voice input. I was expecting to get dollar amount so $200 or $20.32 or $0.43 is an example. However, the response from backend is always like "20 point 32" or "20 dot 32" or "zero point forty five. Is there a better way to use the API samrtly so that I can get "200, 20.32, 0.45"? Thanks!
You could try looking in the alternates, but aside from that, I don't see any way to control text normalization in dictation grammars.
You can load your own grammar to SpeechRecognizer as described here: Adding, loading, and preloading grammars for Windows Phone 8.
In your case will be useful SRGS grammar.
You should create grammar like this (I get this from Microsoft Speech example):
<grammar version="1.0" xml:lang="en-US" mode="voice" root="amount"
xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0">
<rule id="amount">
<item repeat="1-3">
<ruleref uri="#number"/>
</item>
<item repeat="0-1">
<ruleref uri="#dot"/>
<item>
<item repeat="0-2">
<ruleref uri="#number"/>
</item>
</rule>
<rule id="dot">
<one-of>
<item> point <tag> out = "." ; </tag> </item>
<item> dot <tag> out = "." ; </tag> </item>
</one-of>
</rule>
<rule id="number">
<one-of>
<item> one <tag> out = 1; </tag> </item>
<item> two <tag> out = 2; </tag> </item>
<item> three <tag> out = 3; </tag> </item>
<item> four <tag> out = 4; </tag> </item>
<item> five <tag> out = 5; </tag> </item>
<item> six <tag> out = 6; </tag> </item>
<item> seven <tag> out = 7; </tag> </item>
<item> eight <tag> out = 8; </tag> </item>
<item> nine <tag> out = 9; </tag> </item>
<item> ten <tag> out = 10; </tag> </item>
</one-of>
</rule>
</grammar>
I have read the Castle Windsor Documentation for Type Converters and passing dictionaries, however I am getting an error that doesn't seem to be mentioned in the docs.
My configuration:
<parameters>
<controllerTranslations>
<list>
<item>
<areaName></areaName>
<routeValue>MyRoute</routeValue>
<translationTable>
<dictionary>
<entry key="en">MyRouteEn</entry>
<entry key="fr">MyRouteFr</entry>
</dictionary>
</translationTable>
</item>
....
</list>
</controllerTranslations>
</parameters>
My TypeConverter:
public override object PerformConversion(IConfiguration configuration, Type targetType)
{
var converter = new Converter(configuration.Children, Context);
var areaName = converter.Get<string>("areaName");
var routeValue = converter.Get<string>("routeValue");
var translationTable = converter.Get<Dictionary<string, string>>("translationTable");
...
}
I get the error, "You must provide a key for the dictionary entry" when the converter tries to get the dictionary.
Debugging the DictionaryConverter in Windsor, the actual expected schema is as follows, but this doesn't seem right. My test passes and no exception is thrown from Windsor:
<parameters>
<controllerTranslations>
<list>
<item>
<areaName></areaName>
<routeValue>MyRoute</routeValue>
<translationTable>
<dictionary key="en">
<entry>MyRouteEn</entry>
</dictionary>
<dictionary key="fr">
<entry>MyRouteFr</entry>
</dictionary>
</translationTable>
</item>
....
</list>
</controllerTranslations>
</parameters>
With some further testing I think I have found the correct way to implement this:
<parameters>
<controllerTranslations>
<list>
<item>
<areaName></areaName>
<routeValue>MyRoute</routeValue>
<translationTable type="System.Collections.IDictionary, mscorlib">
<entry key="en">MyRouteEn</entry>
<entry key="fr">MyRouteFr</entry>
</translationTable>
</item>
....
</list>
</controllerTranslations>
</parameters>
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>