In an attempt to unravel an XML string using a regular expression, my goal is to construct a coherent string from it.
The XML string represents a complex boolean expression with nested elements.
Currently, I can extract the values involved in equalities, but I'm struggling to capture the AND/OR operators as well as the necessary parentheses for the final output.
Here is a snippet of the XML structure:
<applic id="TCTO_709_PRE_ALL">
<displayText><simplePara>All Aircraft without Extended Range Capability</simplePara></displayText>
<!--BEGIN OR-->
<evaluate andOr="or">
<!-- ( -->
<assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="UHK97000-15" />
<!--BEGIN AND-->
<evaluate andOr="and">
<!-- ( -->
<!--BEGIN OR-->
<evaluate andOr="or">
<!-- ( -->
<assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="UHK97000-10" />
<assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="UHK97000-12" />
<!-- ) -->
</evaluate>
<!--BEGIN OR-->
<evaluate andOr="or">
<!-- ( -->
<assert applicPropertyIdent="TCTO_1Q-9A-709" applicPropertyType="condition" applicPropertyValues="PRE" />
<assert applicPropertyIdent="TCTO_1Q-9A-709" applicPropertyType="condition" applicPropertyValues="NOI" />
<!-- ) -->
</evaluate>
<!-- ) -->
</evaluate>
<!-- ) -->
</evaluate>
</applic>
All <assert>
elements are contained within either AND or OR <evaluate>
elements.
This is the desired outcome based on the provided XML:
(partno="UHK97000-15" or ((partno="UHK97000-10" or partno="UHK97000-12") and (TCTO_1Q-9A-709="PRE" or TCTO_1Q-9A-709="NOI")))
Below is the script I am currently working with:
var sApplic = '<applic id="TCTO_709_PRE_ALL"><displayText><simplePara>All Aircraft without Extended Range Capability</simplePara></displayText><evaluate andOr="or"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="UHK97000-15"></assert><evaluate andOr="and"><evaluate andOr="or"><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="UHK97000-10"></assert><assert applicPropertyIdent="partno" applicPropertyType="prodattr" applicPropertyValues="UHK97000-12"></assert></evaluate><evaluate andOr="or"><assert applicPropertyIdent="TCTO_1Q-9A-709" applicPropertyType="condition" applicPropertyValues="PRE"></assert><assert applicPropertyIdent="TCTO_1Q-9A-709" applicPropertyType="condition" applicPropertyValues="NOI"></assert></evaluate></evaluate></evaluate></applic>';
var sRegXEval = /<assert applicPropertyIdent="(.*?)" applicPropertyType=".*?" applicPropertyValues=(".*?")(\/>|<\/assert>)?/g;
var sMatch = sRegXEval.exec(sApplic);
while (sMatch != null) {
var sFirst = sMatch[1] + "=" + sMatch[2];
document.write("<p>sMatch[" + i +"]" + sFirst);
sMatch = sRegXEval.exec(sApplic);
i++;
}
</script>
Despite running the above script, the results fall short of the anticipated output:
sMatch[0]partno="UHK97000-15"
sMatch[1]partno="UHK97000-10"
sMatch[2]partno="UHK97000-12"
sMatch[3]TCTO_1Q-9A-709="PRE"
sMatch[4]TCTO_1Q-9A-709="NOI"
How can I enhance the code to align with the desired result?
UPDATE: The original XML string has been updated to:
var sApplic = '<datamodule><file>CClasic.sgm</file><applic><displayText><simplePara>Cooking Classics</simplePara></displayText><assert applicPropertyIdent="author" applicPropertyType="prodattr" applicPropertyValues="Crocker"/></applic></datamodule>';
Due to this change, the previous script provided by @trincot is no longer applicable. How can I modify the script to accommodate this new string while also incorporating the display of the file name within the <file>
element?