I am using cheeriojs to parse some xml
const $ = cheerio.load(xml, {
normalizeWhitespace: true,
xmlMode: true
})
This is probably something very basic that eludes me, but I can't figure out how to output the selected element verbatim. For example, I have
<foo id="3B3D3CAD1268FFDFD3E36F2DFD30DBD0" a="3392779302" b="Chiapas" c="Mexico">
<emphasis bold="true" pageId="3" pageNumber="120">Type locality.</emphasis>
</foo>
<emphasis bold="true" pageId="3" pageNumber="120">Other localities.</emphasis>
<foo id="3B3D3CAD1268FFDFD3E36F2D68FFDFD" a="3392779378" b="Baja California" c="Mexico">
<emphasis bold="true" pageId="3" pageNumber="120">Type locality.</emphasis>
</foo>
and I have selected the above like so
const elements = $('foo')
for (let i = 0; i < elements.length; i++) {
const e = elements[i]
console.log($(e).html())
}
I get
<emphasis bold="true" pageId="3" pageNumber="120">Type locality.</emphasis>
<emphasis bold="true" pageId="3" pageNumber="120">Type locality.</emphasis>
But I want
<foo id="3B3D3CAD1268FFDFD3E36F2DFD30DBD0" a="3392779302" b="Chiapas" c="Mexico"></foo>
<foo id="3B3D3CAD1268FFDFD3E36F2D68FFDFD7" a="3392779378" b="Baja California" c="Mexico"></foo>
console.log($(e).prop('outerHTML')) does the trick
Related
I have the AirPlayHistory.xml file that shows the last 3 streamed songs and is updated every time the song changes..
AirPlayHistory:
<Event status="happened">
<Song title="Forbidden Voices">
<Artist name="Martin Garrix" ID="344518"> </Artist>
<Info StartTime="11:06:31" JazlerID="7235" PlayListerID=""/>
</Song>
<Song title="Faded">
<Artist name="Alan Walker" ID="343769"> </Artist>
<Info StartTime="11:10:28" JazlerID="7769" PlayListerID=""/>
</Song>
<Song title="Afterlife">
<Artist name="Illenium" ID="344414"> </Artist>
<Info StartTime="11:13:30" JazlerID="10668" PlayListerID=""/>
</Song>
</Event>
I have this code:
var speed = 10000; // 10 seconds
function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};
ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e) {try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};
function process(xml) {
document.getElementById('contentfile').innerHTML=xml;
var title = document.getElementById('contentfile').getElementsByTagName('Song')[0].title;
var name = document.getElementById('contentfile').getElementsByTagName('Artist')[0].name;
document.getElementById('contentfile').innerHTML='NOW ON AIR: '+name+' | '+title;
}
function checkXml() {
ajax.get('http://website.com/jazler/NowOnAir.xml',process)
}
window.onload=function() {
checkXml();
tId=setInterval('checkXml()',speed)
}
<div id="contentfile">
but he shows only one song of 3..
Clear! you have the first only you write name and title!! you simply rewrote the innerHTML BUT code line document.getElementById('contentfile').innerHTML=xml; is not good you have to loop through! now you write all inner.HTMl content with the first element title[0] name[0]
Am trying to retrieve objects from MySQL database and store it in an XML file. How ever the below code is not writing the XML data in the format i needed.
private static Document buildCustomerXML(ResultSet EmployeeRS) throws Exception
{
Document xmlDoc = new DocumentImpl();
/* Creating the root element */
Element rootElement = xmlDoc.createElement("Schedule");
xmlDoc.appendChild(rootElement);
while(EmployeeRS.next())
{
Element employee1 = xmlDoc.createElement("Employees");
Element employee = xmlDoc.createElement("Employee");
/* Build the CustomerId as a Attribute*/
employee.setAttribute("id", EmployeeRS.getString("id"));
/* Creating elements within customer DOM*/
Element contractid = xmlDoc.createElement("ContractID");
Element lastName = xmlDoc.createElement("Name");
Element skills = xmlDoc.createElement("Skills");
Element skill = xmlDoc.createElement("Skill");
/* Populating Customer DOM with Data*/
contractid.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Contractid")));
lastName.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("last")));
skill.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Skill")));
/* Adding the firstname and lastname elements to the Customer Element*/
employee.appendChild(contractid);
employee.appendChild(lastName);
employee.appendChild(skills);
skills.appendChild(skill);
employee1.appendChild(employee);
rootElement.appendChild(employee1);
/* Appending Customer to the Root Class*/
/* Build the CustomerId as a Attribute*/
Element constraints1 = xmlDoc.createElement("Constraints");
Element constraints = xmlDoc.createElement("Constraint");
constraints.setAttribute("id", EmployeeRS.getString("id"));
Element startdat = xmlDoc.createElement("startdate");
startdat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("startdate")));
constraints.appendChild(startdat);
constraints1.appendChild(constraints);
rootElement.appendChild(constraints1);
}
return xmlDoc;
}
Below is the output im getting for the above code
<?xml version="1.0" encoding="UTF-8"?>
<Schedule>
<Employees>
<Employee id="11">
<ContractID>1</ContractID>
<Name>kumbhar</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="11">
<startdate>11/08/2014</startdate>
</Constraint>
</Constraints>
<Employees>
<Employee id="14">
<ContractID>1</ContractID>
<Name>Raje</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="14">
<startdate>2014-11-12</startdate>
</Constraint>
</Constraints>
</Schedule>
However i need output to be in the below form
<?xml version="1.0" encoding="UTF-8"?>
<Schedule>
<Employees>
<Employee id="11">
<ContractID>1</ContractID>
<Name>kumbhar</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
<Employee id="14">
<ContractID>1</ContractID>
<Name>Raje</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="14">
<startdate>2014-11-12</startdate>
</Constraint>
<Constraint id="11">
<startdate>11/08/2014</startdate>
</Constraint>
</Constraints>
</Schedule>
Can any one provide me suggestions on how to achieve output in the above format
I solved it
while(EmployeeRS.next())
{
Element employee = xmlDoc.createElement("Employee");
/* Build the CustomerId as a Attribute*/
employee.setAttribute("id", EmployeeRS.getString("id"));
/* Creating elements within customer DOM*/
Element contractid = xmlDoc.createElement("ContractID");
Element lastName = xmlDoc.createElement("Name");
Element skills = xmlDoc.createElement("Skills");
Element skill = xmlDoc.createElement("Skill");
/* Populating Customer DOM with Data*/
contractid.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Contractid")));
lastName.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("last")));
skill.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Skill")));
/* Adding the firstname and lastname elements to the Customer Element*/
employee.appendChild(contractid);
employee.appendChild(lastName);
employee.appendChild(skills);
skills.appendChild(skill);
employee1.appendChild(employee);
/* Appending Customer to the Root Class*/
/* Build the CustomerId as a Attribute*/
Element constraints = xmlDoc.createElement("Constraint");
constraints.setAttribute("id", EmployeeRS.getString("id"));
Element startdat = xmlDoc.createElement("startdate");
Element enddat = xmlDoc.createElement("enddate");
startdat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("startdate")));
enddat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("enddate")));
constraints.appendChild(startdat);
constraints.appendChild(enddat);
constraints1.appendChild(constraints);
} rootElement.appendChild(employee1);
rootElement.appendChild(constraints1);
return xmlDoc;
}
var xml:XML = new XML(
<root>
<message for="Harry" > adlfjljfa </message>
<message for="Harry" > ajf ja; jafja </message>
<message for="Akil"> difasfjlfjals </message>
var xmlList:XMLList = xml.message.(#for== "Harry" )
var nameXML:XML = XML(xmlList)
trace( nameXML )
Right now the nameXML doesnot form correctly, and throws an error : The markup in the document following the root element must be well formed.
Thanks
You didnt close the root tag in the XML declaration.
I replaced the "for" attribute because its reserved.
var xml:XML =
<root>
<message dest="Harry" > adlfjljfa </message>
<message dest="Harry" > ajf ja; jafja </message>
<message dest="Akil"> difasfjlfjals </message>
</root>
var xmlList:XMLList = xml.message.(#dest== "Harry" )
for each (var item:XML in xmlList) {
trace(item)
}
output
adlfjljfa
ajf ja; jafja
Edit
You can also keep the "for" attribute :
var xmlList:XMLList = xml.message.(attribute('for')== "Harry" )
How can I get the prefix of a xml namespace from URI?
example:
<Name xmlns:tiger="http://www.census.gov">
</Name>
I've got the
"http://www.census.gov"
I want to get the prefix
tiger
How can I do this in Actionscript / Flex?
thx
EDIT
The answer doesn't work with this complex example:
<Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
tiger:poly_landmarks
</Name>
I've got empty Array.
ANSWER MY OWN Q
for EDIT example
var xml:XML = <Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
tiger:poly_landmarks
</Name>
var ns:Namespace = xml.namespace("http://www.census.gov");
if(ns.uri == "http://www.census.gov")
....
var xml:XML = <Name xmlns:tiger="http://www.census.gov"></Name>;
var ns:Array = xml.namespaceDeclarations();
trace(ns[0].prefix); //output: tiger
UPD for complex xml (output tiger as well):
var xml:XML = <Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
tiger:poly_landmarks
</Name>
var nss:Array = xml.namespaceDeclarations();
for each(var ns:Namespace in nss)
{
if(ns.uri == "http://www.census.gov")
{
trace(ns.prefix);
break;
}
}
I have an input xml for a transformation like ;
<?xml version="1.0" encoding="UTF-8" ?>
<AssetcustomerCollection xmlns="http://xmlns.oracle.com/pcbpel/adapter/db/top/somens">
<Assetcustomer xmlns="">
....
</Assetcustomer>
<Assetcustomer xmlns="">
<accountklantid>000000123456789</accountklantid>
<accountrowid>1-W8HQ1J</accountrowid>
<adrestypeaccnt/>
<adrestypecon/>
<assetbankcode>1173</assetbankcode>
<assetnumber>0000001234</assetnumber>
<assetprodcode>1200</assetprodcode>
<assetproduct>Overeenkomst Rekening-courant</assetproduct>
<assetproductlocatie>00</assetproductlocatie>
<assetstatus>Actief</assetstatus>
<assetsubstatus>Lopende rekening</assetsubstatus>
<assettypecode>0010</assettypecode>
<contactklantid/>
<contactrowid/>
<primairaccount>Y</primairaccount>
<primaircontact>N</primaircontact>
<reltypeaccnt>Hoofdcontractant</reltypeaccnt>
<reltypecon/>
<rowidasset>1-X3XBMO</rowidasset>
<rowidassetaccnt>1-X3XBMQ</rowidassetaccnt>
<rowidassetcon/>
<tnsidaccnt/>
<tnsidcon/>
</Assetcustomer>
<Assetcustomer xmlns="">
....
</Assetcustomer>
<Assetcustomer xmlns="">
<accountklantid/>
<accountrowid/>
<adrestypeaccnt/>
<adrestypecon/>
<assetbankcode>1173</assetbankcode>
<assetnumber>0000004321</assetnumber>
<assetprodcode>1201</assetprodcode>
<assetproduct>WereldPas (Zakelijk)</assetproduct>
<assetproductlocatie>00</assetproductlocatie>
<assetstatus>Actief</assetstatus>
<assetsubstatus>Lopende rekening</assetsubstatus>
<assettypecode>0003</assettypecode>
<contactklantid>000000987654321</contactklantid>
<contactrowid>1-X17PLM</contactrowid>
<primairaccount>N</primairaccount>
<primaircontact>Y</primaircontact>
<reltypeaccnt/>
<reltypecon>Pasverantwoordelijke</reltypecon>
<rowidasset>1-X3XBN0</rowidasset>
<rowidassetaccnt/>
<rowidassetcon>1-X3XBNE</rowidassetcon>
<tnsidaccnt/>
<tnsidcon/>
</Assetcustomer>
<Assetcustomer xmlns="">
....
</Assetcustomer>
</AssetcustomerCollection>
When transforming this input xml i got an unexpected output (15 of the 16 input Assetcustomer nodes were transformed) I now have found the cause, but cannot explain why it occurs;
The following transformation returns the same id twice;
<xsl:element name="A">
<xsl:value-of select="generate-id(key('AssetRowIDs',/ns0:AssetcustomerCollection/Assetcustomer[rowidasset = '1-X3XBMO']/*)[1])"/>
</xsl:element>
<xsl:element name="B">
<xsl:value-of select="generate-id(key('AssetRowIDs',/ns0:AssetcustomerCollection/Assetcustomer[rowidasset = '1-X3XBN0']/*)[1])"/>
</xsl:element>
<A>N10211</A>
<B>N10211</B>
While the generated id for any other node with a different rowidasset is different.
Any ideas before i start pulling my hair out ?
Peter
I do not know exactly why , but changing
<xsl:key name="AssetRowIDs" match="Assetcustomer" use="rowidasset"/>
into
<xsl:key name="AssetRowIDs" match="Assetcustomer" use="concat('-',rowidasset,'-')"/>
and
<xsl:for-each select="/ns0:AssetcustomerCollection/Assetcustomer[generate-id() = generate-id(key('AssetRowIDs',rowidasset)[1])]">
into
<xsl:for-each select="/ns0:AssetcustomerCollection/Assetcustomer[generate-id() = generate-id(key('AssetRowIDs',concat('-',rowidasset,'-'))[1])]">
Seems to generate a unique id for each node, still bugging me dat i do not understand the cause of it.
Check the namespace? If the ns0 prefix is bound to a wrong namespace URI, your query will in both cases yield an empty result set. Together with the same first argument for key, that, I imagine, will yield the same call to key() and thus the same ID.
Also I don't think the key() function does what you think it does: http://www.w3schools.com/xsl/func_key.asp
In any case you can apply generate-id() directly on the node set for which you wish to calculate the ID.