Hi have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<item>
<books>
<?xml version="1.0" encoding="UTF-8"?>
<Fiction>
<tt:Author>A</tt:Author>
<tt:BookName>45</tt:BookName>
</Fiction>
</books>
</item>
</library>
I want to basically replace the second occurence of the entire xml tag with blank space. So basically replace <?xml version="1.0" encoding="UTF-8"?> string which appears after the <books> opening tag with a space.
Any suggestions? I tried other links but could not get a working solution. The problem is there are ", ? and > in between the xml tag and the string replace function is considering that as an escape sequence character.
This is what I tried:
var stringToReplace = '<?xml version="1.0" encoding="UTF-8"?>';
var string = data.string;
//console.log(string);
var t=0;
var text = string.replace(/stringToReplace/g, function (match) {
t++;
return (t === 2) ? "Not found" : match;
});
console.log(text);
The above still prints both the xml tags
Assuming your XML always looks like this, you can use regular String methods to find the last occurrence of the string and remove it by creating substrings of the XML around it:
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<library>
<item>
<books>
<?xml version="1.0" encoding="UTF-8"?>
<Fiction>
<tt:Author>A</tt:Author>
<tt:BookName>45</tt:BookName>
</Fiction>
</books>
</item>
</library>`;
const strToReplace = '<?xml version="1.0" encoding="UTF-8"?>';
const index = xml.lastIndexOf(strToReplace);
// The new left- and right-sides of the string will omit the strToReplace
const newXml = xml.substring(0, index) + xml.substring(index + strToReplace.length);
console.log(newXml);
Related
I have an XSL/XML/JS file. It was written by someone who is not working here any more, and I normally only deal with SQL, so Im at a loss as to how to achieve what I need to do
Im trying to add some variables into the file within the existing CDATA block. I then use the variables within a function. However, I have tried the below and variations of this, but keep getting a syntax error within the application (Dynamics AX). Am I doing something obviously wrong here, with either how I am declaring the variables or how I am using them? These are the only changes I have made, and without these changes there are no syntax or any other issues/errors.
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:mxm="http://schemas.microsoft.com/dynamics/2008/01/documents/MxmServInterfaceOutboundAif"
xmlns:data="http://www.example.com/data" exclude-result-prefixes="xs xsi xsl">
<xsl:output method="text" encoding="UTF-8" indent="no" />
<msxsl:script language="JScript" implements-prefix="data">
<![CDATA[
//Minor Repairs email address
var MinorsEmail = xxx#domain.com
//Service Dept email address
var ServiceEmail = yyy#domain.com
//Major Repairs email address
var MajorsEmail = zzz#domain.com
//Select appropriate email to use
function EmailFrom(fault)
{
var type = fault.substr(0,2);
if (type == "MI")
{
var ret = MinorsEmail;
}
else
{
var ret = concat(ServiceEmail, "; ",MajorsEmail);
}
return ret;
}
Edit: Adding quotes around the variable values has solved part of the problem. The problem now is that the CONCAT does not function as intended. I get the following error now:
Variable concat has not been declared
Thanks to #Martin Honnen, the answer was to add quote to variable values, and to use + instead of CONCAT:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:mxm="http://schemas.microsoft.com/dynamics/2008/01/documents/MxmServInterfaceOutboundAif"
xmlns:data="http://www.example.com/data" exclude-result-prefixes="xs xsi xsl">
<xsl:output method="text" encoding="UTF-8" indent="no" />
<msxsl:script language="JScript" implements-prefix="data">
<![CDATA[
//Minor Repairs email address
var MinorsEmail = "xxx#domain.com"
//Service Dept email address
var ServiceEmail = "yyy#domain.com"
//Major Repairs email address
var MajorsEmail = "zzz#domain.com"
//Select appropriate email to use
function EmailFrom(fault)
{
var type = fault.substr(0,2);
if (type == "MI")
{
var ret = MinorsEmail;
}
else
{
var ret = ServiceEmail + "; " + MajorsEmail;
}
return ret;
}
This is the xml string stored in a variable "xml".
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:extratask="http://extratask" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn">
<bpmn:process id="Process_1" isExecutable="false">
<bpmn:task id="Task_15xgmrn" name="Select1Select5" extratask:entity="Select1" extratask:action="Select5" />
<bpmn:task id="Task_0ditp3t" name="Select2Select6" extratask:entity="Select2" extratask:action="Select6" />
<bpmn:task id="Task_0p68hrl" name="Select3Select6" extratask:entity="Select3" extratask:action="Select6" />
</bpmn:process>
</bpmn:definitions>
So far, I have just tried to read the nodes "bpmn:task" into console with this code but getting blank array.
if(window.DOMParser){
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml, "text/xml");
}else{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xml);
}
console.log(xmlDoc.getElementsByTagName("bpmn:task"));
please someone make me understand that where I am going wrong, and another thing is that I want to insert some attributes into "bpmn:task" tags.
The attribute xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" denotes a custom namespace for your XML, which means you have to use getElementsByTagNameNS() with the give namespace to get the tags
xmlDoc.getElementsByTagNameNS("http://www.omg.org/spec/BPMN/20100524/MODEL","task")
var xml = '<?xml version="1.0" encoding="UTF-8"?><bpmn:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:extratask="http://extratask" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn"><bpmn:process id="Process_1" isExecutable="false"><bpmn:task id="Task_15xgmrn" name="Select1Select5" extratask:entity="Select1" extratask:action="Select5" /><bpmn:task id="Task_0ditp3t" name="Select2Select6" extratask:entity="Select2" extratask:action="Select6" /><bpmn:task id="Task_0p68hrl" name="Select3Select6" extratask:entity="Select3" extratask:action="Select6" /></bpmn:process></bpmn:definitions>';
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml, "text/xml");
console.log(xmlDoc.getElementsByTagNameNS("http://www.omg.org/spec/BPMN/20100524/MODEL","task"));
Good afternoon Scripters,
I have had some help setting up this structure, which I am using to create a folder on my desktop dynamically based on certain XML elements in my XML strucure, then loop through the XML records, split them into separate files, and put them in their respective folders. So far, it is working perfectly, but I need to apply an XSL that I have which will convert certain attributes into elements. Is there a way in ESTK to apply XSLT with JavaScript upon export?
var root, records, f, n, doc;
doc = app.activeDocument;
root = doc.xmlElements[0];
records = root.evaluateXPathExpression ( "./record" );
n = records.length;
while ( n-- ) {
var ff = new Folder(Folder.desktop + "/" +app.activeDocument.xmlElements.item(0).xmlElements.item(n).xmlElements.item(0).xmlAttributes.item(0).value + "/data/" +
app.activeDocument.xmlElements.item(0).xmlElements.item(n).xmlElements.item(0).xmlElements.item(0).xmlElements.item(1).xmlAttributes.item(1).value + "/" +
app.activeDocument.xmlElements.item(0).xmlElements.item(n).xmlElements.item(0).xmlElements.item(0).xmlElements.item(2).xmlAttributes.item(1).value);
if (!ff.exists)
ff.create();
f = File ( ff +"/"+app.activeDocument.xmlElements.item(0).xmlElements.item(n).xmlElements.item(0).xmlElements.item(0).xmlElements.item(0).xmlAttributes.item(1).value);
records[n].exportFile ( ExportFormat.XML, f);
}
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Root/record">
<record name="{#name}" type="{#type}">
<item name="{item/#name}">
<value>
<xsl:for-each select="item/value/item">
<item name="{#name}">
<value><xsl:value-of select="#value"/></value>
</item>
</xsl:for-each>
</value>
</item>
</record>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
See answer here:
https://forums.adobe.com/thread/1813381
insert before line 13:
app.activeDocument.xmlExportPreferences.allowTransform=true;
app.activeDocument.xmlExportPreferences.transformFilename=File('PATH_TO_YOUR_TRANSFROM_FILE');
Dear All I have soap response with following structure
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://lbs.xius.com/messages/lbs" xmlns:ns2="http://lbs.xius.com/messages/common" xmlns:ns3="http://billing.xius.com/BalanceManagement.wsdl" xmlns:ns4="http://billing.xius.com/BalanceManagement.xsd" xmlns:ns5="http://billing.xius.com/common/header/HeaderDetails.xsd" xmlns:ns6="http://billing.xius.com/common/error/ErrorDetails.xsd" xmlns:ns8="http://lbs.xius.com/services/lbs" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<ns1:SLIAnswer>
<ns1:Ver />
<ns1:ResponseData>
<ns1:ResultAndAddInfo>
<ns1:Result>
<ns1:Result>SYSTEM_FAILURE</ns1:Result>
<ns1:ResultId>1</ns1:ResultId>
</ns1:Result>
</ns1:ResultAndAddInfo>
</ns1:ResponseData>
</ns1:SLIAnswer>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
From this string I want to fetch all the child node in "ResponseData" node , and its values
how I can do this using javascript
You can use DOMParser() along with namespaced DOM functions.
var parser = new DOMParser();
var doc = parser.parseFromString(xml, "application/xml");
var responseData = doc.getElementsByTagNameNS('http://lbs.xius.com/messages/lbs', 'ResponseData');
Is there a way to get the name of an attribute of an XML node using javascript.
Lets take this as a sample XML
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Count name="EmployeeCount">100</Count>
<employee id="9999" >Harish</employee>
<Salary>
<year id="2000">50 Grands</year>
<year id="2001">75 Grands</year>
<year id="2002">100 Grands</year>
</Salary>
</Employees>
I am loading XML using ActiveXObject.As you can see not all elements have attributes.I need to list all attributes like
name
id
id
id
id
Try this:
var nodes = xml.selectNodes("//#*")
for(var i=0; i < nodes.length; i++)
{
alert(nodes[i].nodeName);
}