I have xml data looking like this:
<persons>
<person key="M">John Doe</person>
<person key="N">Jane Doe</person>
</persons>
I want to collect them into a list of maps like
[[key: M, name: John Doe], [key: N, name: Jane Doe]]
and I use, after slurping the data into the variable 'p', using XmlSlurper:
p.collect { [key: it.#key.text(), name it.text()] }
but I get
[[key: MN, name: John DoeJane Doe]]
Obviously I do something very wrong, but I can't figure out what. I have tried a number of methods but get the same answer.
Try looking for children() from root node.
def xml = """
<persons>
<person key="M">John Doe</person>
<person key="N">Jane Doe</person>
</persons>
"""
def slurper = new XmlSlurper().parseText( xml )
assert [
[key:'M', name:'John Doe'],
[key:'N', name:'Jane Doe']
] == slurper.children().collect {
[ key: it.#key.text(), name: it.text() ]
}
Related
I'm trying to match tag and possible attributes of xml tag.
Here you can find example:
https://regex101.com/r/mZYvGU/1/
^([\S]+)(?:\s*([\S]+)="([^"]+)")*
test string:
tag att1="1" att2="2"
The problem is - it is always catching last attribute only.
Thanks for help.
You can follow my example:
let s = `tag att1="1" att2="2" att3="34"`;
let regex = /(.*?)(?<=\s)(\w+)="(\w+)"/ig;
let result = [];
s.replace(regex, function(match,tag,att_name,att_value){
if(tag.trim()!==''){
result.push(tag.trim());
result.push(att_name); //att1
result.push(att_value); //1
}else{
result.push(att_name); //att2 and att3
result.push(att_value); //2 and 34
};
});
console.log(result);
I want to change "A" in Apple, in key which value is 1 to "D", in this code 2 items should be replaced. I tried something like this, but nothing happens.
<C>
<B>
<Key value="1">
<Apple>A</Apple>
</Key>
<Key value="2">
<Apple>A</Apple>
</Key>
<Key value="3">
<Apple>A</Apple>
</Key>
<Key value="1">
<Apple>A</Apple>
</Key>
</B>
</C>
Qt:
QDomNodeList roots = doc.elementsByTagName("Key");
for (int i = 0; i < roots.size(); ++i) {
QDomElement domElement = roots.at(i).toElement();
QDomAttr attribute = domElement.attributeNode("value");
if (attribute.value() == "1") {
domElement.firstChild().setNodeValue("D");
}
In your code the domElement.firstChild() refers to the <Apple> element node, not to its content. The value inside the <Apple> is also a node (text node), that is why it is located one level deeper in the DOM hierarchy:
domElement — <Key>
domElement.firstChild() — <Apple>
domElement.firstChild().firstChild() — A
So you will also have to get the first child of the <Apple> element node:
domElement.firstChild().firstChild().setNodeValue("D");
Maybe:
if (attribute.value() == "1")
Im trying to create a function in javascript in xsl which accepts multiple in-parameters. I can not get it to work and get the following error:
Code: 0x80020009
Microsoft JScript runtime error
Wrong number of arguments or invalid property assignment
line = 37, col = 2 (line is offset from the start of the script block).
Error returned from property or method call.
The function looks as follows:
<msxsl:script language="JavaScript" implements-prefix="jsfuncs">
<![CDATA[
function getLineLength (x1,x2,y1,y2)
{
var xVector = x2 - x1;
var yVector = y2 - y1;
var output = Math.sqrt(raised2(xVector)+raised2(yVector));
return output;
}
]]>
</msxsl:script>
The calling code looks as follows:
<xsl:value-of select="jsfuncs:getLineLength($x1,$x2,$y1,$y2)"/>
x1,x2... are variables set earlier and they are correct. I can get everything to work when I post-process the values to one parameter. Is it at all possible to pass multiple parameters in xslt to Javascript? The engine in use is msxml 3.0.
Here is an example passing in four parameters:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="ms mf"
version="1.0">
<ms:script language="JScript" implements-prefix="mf">
function test(a, b, c, d) {
return a + ":" + b + ":" + c + ":" + d;
}
</ms:script>
<xsl:template match="/">
<xsl:value-of select="mf:test(1, 2, 3, 'foo')"/>
<br/>
<xsl:variable name="item" select="root/item"/>
<xsl:value-of select="mf:test(string($item/#a), string($item/#b), string($item/#c), string($item/#d))"/>
</xsl:template>
</xsl:stylesheet>
As you can see, when wanting to pass in a node to a function expecting a primitive value I would recommend to make sure to convert it on the XSLT side to a primitive value first, as done in mf:test(string($item/#a), string($item/#b), string($item/#c), string($item/#d)), as otherwise the function will get a node-set represented by some selection interface that you would need to iterate first.
The sample works fine for me with MSXML 3 and outputs 1:2:3:foo<br />1:2:3:whatever when run against an input sample with
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item a="1" b="2" c="3" d="whatever"/>
</root>
I need to assign a value to a particular attribute in a stream by checking a condition.
How do I do it?
e.g
if a person's name is 'John' and I need to modify his name into 'Peter' and insert into another stream, how do I do it?
from myStream [name == 'John']
select *
insert into outStream;
define stream myStream (name string, attribute1 int, attribute2 bool);
from myStream [name == 'John'] --This filter matches any event with name equal to 'John'
select "Peter" as name, attribute1, attribute2 --Replaces the current name with 'Peter'
insert into outStream;
Example 1
Input:
{Dilini, 123, true}
Output:
(there'll be no output, since the name doesn't satisfy the condition in the filter, name=='John')
Example 2
Input:
{John, 123, true}
Output:
{Peter, 123, true}
Refer 'Query Projection' capabilities in Siddhi. Check the action: 'Introducing the default value'.
I have a following xml file .
T0020.xml
SAFER
04.02
01.00
REPLACE
2009-09-01T00:00:00
CT
274845
US
AR
55002
I
100
2007-11-06
2009-08-03
I have to insert xml data into following table
IRP_ACCOUNT
Field Name Field Type
FILE_NAME VARCHAR2(120)
IRP_ACCOUNT_NUMBER VARCHAR2(32)
IRP_ACCOUNT_TYPE VARCHAR2(1)
IRP_CARRIER_ID_NUMBER VARCHAR2(12)
IRP_BASE_COUNTRY VARCHAR2(12)
IRP_BASE_STATE VARCHAR2(12)
IRP_STATUS_CODE VARCHAR2(12)
IRP_STATUS_DATE VARCHAR2(10)
IRP_UPDATE_DATE VARCHAR2(10)
My code to insert xml Data into Database is ...
OracleXMLSave oracleXMLSave =new OracleXMLSave(conn, tableNamePro);
//set XSL file.
File xslFileName = new File(CviewFileReader.readFileFromClassPathAsURL(xslFolderLocation + "/" + rootNode.trim() + "/" + xslFileNamePro.trim() + ".xsl").getFile().substring(1).replace("%20", " "));
Reader xsltReader=new FileReader(xslFileName);
oracleXMLSave.setXSLT(xsltReader, null);
//set fileNameParameterValueIn XLS file. so this value insert into database table as "FILE_VALUE" field.
oracleXMLSave.setXSLTParam("FILE_NAME", "'" + onlyXMLfileName + "'");
//set row separter value.
oracleXMLSave.setRowTag(xmlRowTagVal);
//set ignoreCase to true.
oracleXMLSave.setIgnoreCase(true);
//give here XML file path
FileInputStream xmlFileNameForInserting = new FileInputStream(xmlFileName.getAbsoluteFile());
oracleXMLSave.insertXML(xmlFileNameForInserting);
My XSLT File for Inserting Data is ...
When applying this xslt to insert to Database only FILE_NAME value which has been passed through xslt parameter is not inserted into Database.
See below all value are inserted except FILE_NAME.
FILE_NAME IRP_ACCOUNT_NUMBER IRP_ACCOUNT_TYPE IRP_CARRIER_ID_NUMBER IRP_BASE_COUNTRY IRP_BASE_STATE IRP_STATUS_CODE IRP_STATUS_DATE IRP_UPDATE_DATE
55002 I 274845 US AR 100 2007-11-06 2009-08-03
I have stuck on this point Pls help me....still any confusion then let me know...
Thanks in Advance.
Where exactly do you put the <xsl:element> part? Did you declare <xsl:param name="FILE" /> in your stylesheet?