Cake PeekXml does not "ignore" namespace - cakebuild

Is it a bug or per design that xmlns attribute is not ignored?
(cake version 0.33.0)
With an Xml like so (a too simplified nuspec file):
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<!-- Continuously updated elements -->
<version>3.0.0</version>
</metadata>
</package>
I do a naÏve call
var x = XmlPeek( "my.nuspec", "/package/metadata/version/text()" );
ad get the result x==null.
So I specify the namespace like so:
var settings = new XmlPeekSettings{
Namespaces = new Dictionary<string, string> {{
"ps", "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"
}}
};
var x = XmlPeek( "my.nuspec", "/ps:package/ps:metadata/ps:version/text()", settings);
and get the result x==3.0.0 I anticipated.

It is not a bug.
To ignore the namespace you can use namespace agnostic xpath such as local-name():
var x = XmlPeek( "my.nuspec", "/*[local-name() = 'package']/*[local-name() = 'metadata']/*[local-name() = 'version']/text()");
or if you have only one version node:
var x = XmlPeek( "my.nuspec", "//*[local-name()='version']/text()");
but be careful with documents with large numbers of elements - this can become very slow.

Related

Karate: Replace text at Envelope level [duplicate]

I need to parse and print ns4:feature part. Karate prints it in json format. I tried referring to this answer. But, i get 'ERROR: 'Namespace for prefix 'xsi' has not been declared.' error, if used suggested xPath. i.e.,
* def list = $Test1/Envelope/Body/getPlan/planSummary/feature[1]
This is my XML: It contains lot many parts with different 'ns' values, but i have given here an extraxt.
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/>
<S:Body>
<ns9:getPlan xmlns:ns10="http://xmlschema.test.com/xsd_v8" xmlns:ns9="http://xmlschema.test.com/srv/SMO_v4" xmlns:ns8="http://xmlschema.test.com/xsd/Customer_v2" xmlns:ns7="http://xmlschema.test.com/xsd/Customer/Customer_v4" xmlns:ns6="http://schemas.test.com/eca/common_types_2_1" xmlns:ns5="http://xmlschema.test.com/xsd/Customer/BaseTypes_1_0" xmlns:ns4="http://xmlschema.test.com/xsd_v4" xmlns:ns3="http://xmlschema.test.com/xsd/Enterprise/BaseTypes/types/ping_v1" xmlns:ns2="http://xmlschema.test.com/xsd/common/exceptions/Exceptions_v1_0">
<ns9:planSummary xsi:type="ns4:Plan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns5:code>XPBSMWAT</ns5:code>
<ns5:description>Test Plan</ns5:description>
<ns4:category xsi:nil="true"/>
<ns4:effectiveDate>2009-11-05</ns4:effectiveDate>
<ns4:sharingGroupList>
<ns4:sharingCode>CAD_DATA</ns4:sharingCode>
<ns4:contributingInd>true</ns4:contributingInd>
</ns4:sharingGroupList>
<ns4:feature>
<ns5:code>ABC</ns5:code>
<ns5:description>Service</ns5:description>
<ns5:descriptionFrench>Service</ns5:descriptionFrench>
<ns4:poolGroupId xsi:nil="true"/>
<ns4:switchCode/>
<ns4:type/>
<ns4:dtInd>false</ns4:dtInd>
<ns4:usageCharge>0.0</ns4:usageCharge>
<ns4:connectInd>false</ns4:connectInd>
</ns4:feature>
</ns9:planSummary>
</ns9:getPlan>
</S:Body>
</S:Envelope>
This is the xPath i used;
Note: I saved above xml in a separate file test1.xml. I am just reading it and parsing the value.
* def Test1 = read('classpath:PP1/data/test1.xml')
* def list = $Test1/Envelope/Body/*[local-name()='getPlan']/*[local-name()='planSummary']/*[local-name()='feature']/*
* print list
This is the response i am getting;
16:20:10.729 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] [
"ABC",
"Service",
"Service",
"",
"",
"",
"false",
"0.0",
"false"
]
How can i get the same in XML?
This is interesting, I haven't seen this before. The problem was you have an attribute with a namespace xsi:nil="true" which is causing problems when you take a sub-set of the XML but the namespace is not defined anymore. If you remove it first, things will work.
Try this:
* remove Test1 //poolGroupId/#nil
* def temp = $Test1/Envelope/Body/getPlan/planSummary/feature
Another approach you could have tried is to do a string replace to remove troublesome stuff in the XML before doing XPath.
EDIT: added info on how to do a string replace using Java. The below will strip out the entire xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns4:Plan" part.
* string temp = Test1
* string temp = temp.replaceAll("xmlns:xsi[^>]*", "")
* print temp
So you get the idea. Just use regex.
Also see: https://stackoverflow.com/a/50372295/143475

Search and get content from data in XML and then place that value in another tag using powershell

Sample text file contains:
`<?xml version="1.0" encoding="UTF-8" ?>
<Document xmlns ="urn:iso:std:iso:20022:tech:xsd:camt.056.001.01"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FIToFIPmtCxlReq>
<Assgnmt>
<Id>ID123456</Id>
<Assgnr>
<Agt>
<FinInstnId>
<BIC>BICSEND</BIC>
</FinInstnId>
</Agt>
</Assgnr>
<Assgne>
<Agt>
<FinInstnId>
<BIC>BICRCV</BIC>
</FinInstnId>
</Agt>
</Assgne>
<CreDtTm>2020-12-16T09:05:15.0Z</CreDtTm>
</Assgnmt>
<CtrlData>
<NbOfTxs>1</NbOfTxs>
<CtrlSum>0</CtrlSum>
</CtrlData>
<Undrlyg>
<TxInf>
<CxlId>20201216.105.19344855940590400</CxlId>
<OrgnlGrpInf>
<OrgnlMsgId>REF123456789</OrgnlMsgId>
<OrgnlMsgNmId>pacs.008</OrgnlMsgNmId>
</OrgnlGrpInf>
<OrgnlInstrId>FT123456</OrgnlInstrId>
<OrgnlEndToEndId>NOTPROVIDED</OrgnlEndToEndId>
<OrgnlTxId>20201216.100.02202020</OrgnlTxId>
<OrgnlIntrBkSttlmAmt Ccy="EUR">25.23</OrgnlIntrBkSttlmAmt>
<OrgnlIntrBkSttlmDt>2020-12-16</OrgnlIntrBkSttlmDt>`
Please be informed that I would like to code PowerShell to extract the data in tag <OrgnlIntrBkSttlmAmt> (please note that the data length can change since this is an amount field) and then replace the "0" in tag <CtrlSum> with "25.23".
Can someone help me with this.
Thank you for your time.
The xml you show us is invalid as it is missing the following closing tags:
</TxInf>
</Undrlyg>
</FIToFIPmtCxlReq>
</Document>
If I add these, you could do this to update the value in the <CtrlSum> tag:
# load the xml from file
[xml]$xml = Get-Content -Path 'D:\Test\test.xml' -Raw
# get the amount from the 'OrgnlIntrBkSttlmAmt' tag
$amount = $xml.Document.FIToFIPmtCxlReq.Undrlyg.TxInf.OrgnlIntrBkSttlmAmt.'#text'
# use that amount to put in the 'CtrlSum' tag
$xml.Document.FIToFIPmtCxlReq.CtrlData.CtrlSum = $amount
# save the updated xml to file
$xml.Save('D:\Test\test.xml')

Accessing SOAP services with Google Apps Script

I'm trying to use Google Apps Scripts to call a SOAP service call, and I've been trying to tinker with a number of ways to get a response; however, I keep getting an error. I've defaulted to trying to send an exact copy of a message stored in my spreadsheet that I know works through another service... still no luck. Here's the Apps Script Code:
function getVesselSummaryXMLStringFromName() {
var wsdl = SoapService.wsdl("http://cgmix.uscg.mil/xml/PSIXData.asmx?WSDL");
Logger.log(wsdl.getServiceNames());
var uscgService = wsdl.getPSIXData();
var sheet = SpreadsheetApp.getActiveSheet();
//Get working SOAP message
var envelope = sheet.getRange("D1:D1").getValues();
Logger.log(envelope);
var result = uscgService.getenvelope;
Logger.log(result);
}
The SOAP message I'm sending that works through http://www.soapclient.com/soapclient is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://cgmix.uscg.mil" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ><SOAP-ENV:Body><tns:getVesselSummaryXMLString xmlns:tns="http://cgmix.uscg.mil"><tns:VesselID></tns:VesselID><tns:VesselID></tns:VesselID><tns:VesselName>Atlantic Salvor</tns:VesselName><tns:VesselName></tns:VesselName><tns:CallSign></tns:CallSign><tns:CallSign></tns:CallSign><tns:VIN></tns:VIN><tns:VIN></tns:VIN><tns:HullNum></tns:HullNum><tns:HullNum></tns:HullNum><tns:Flag></tns:Flag><tns:Flag></tns:Flag><tns:Service></tns:Service><tns:Service></tns:Service><tns:BuildYear></tns:BuildYear><tns:BuildYear></tns:BuildYear></tns:getVesselSummaryXMLString></SOAP-ENV:Body></SOAP-ENV:Envelope>
The following code returns an answer, so maybe you can help.
...
var url = 'http://cgmix.uscg.mil/xml/PSIXData.asmx?WSDL';
var wsdl = SoapService.wsdl(url);
var servicePSIXData = wsdl.getPSIXData();
var params = Xml.element('getVesselSummaryXMLString', [
Xml.attribute('xmlns', 'http://cgmix.uscg.mil'),
Xml.element('VesselID', ['']),
Xml.element('VesselName', ['Atlantic Salvor']),
Xml.element('CallSign', ['']),
Xml.element('VIN', ['']),
Xml.element('HullNum', ['']),
Xml.element('Flag', ['']),
Xml.element('Service', ['']),
Xml.element('BuildYear', [''])
]);
var result = servicePSIXData.getVesselSummaryXMLString(params);
Logger.log(result.toXmlString());
...
UPDATE
Both Xml Services and Soap Services are considered deprecated.

xpath soap namespace grabbing items

I am feeling like i am doing something really not correct.
When doing a soap they return me with an xml which may or may not contain an error.
I would like to check if the error exists if not read the values.
somehow, I can't grab it directly :(
Below is a sample return of something with results and one which gives an error (name not found)
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/ccr01/searchPersonResponse</wsaAction>
<wsaMessageID>urn:uuid:b75d2932-5687-4871-9d07-3b74b084978a</wsaMessageID>
<wsaRelatesTo>urn:uuid:9112d870-248d-4d07-acd0-d88e4a48d547</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-061df7b5-32a2-4021-852d-2df98953e076">
<wsuCreated>2011-05-27T12:11:45Z</wsuCreated>
<wsuExpires>2011-05-27T12:16:45Z</wsuExpires>
</wsuTimestamp>
</envHeader>
<soapBody>
<searchPersonResponse xmlns="http://www.rechtspraak.nl/namespaces/ccr01">
<searchPersonResult>
<CCR_WS xmlns="http://www.rechtspraak.nl/namespaces/ccr">
<curandus>
<ccn>1</ccn>
<cur_voornamen>Jan</cur_voornamen>
<cur_voorvoegsels>van</cur_voorvoegsels>
<cur_achternaam>Beek</cur_achternaam>
<geboorte_datum>1980-01-02</geboorte_datum>
<geboorte_plaats>Werkendam</geboorte_plaats>
</curandus>
</CCR_WS>
</searchPersonResult>
</searchPersonResponse>
</soapBody>
</soapEnvelope>
and the one without results
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/ccr01/searchPersonResponse</wsaAction>
<wsaMessageID>urn:uuid:b75d2932-5687-4871-9d07-3b74b084978a</wsaMessageID>
<wsaRelatesTo>urn:uuid:9112d870-248d-4d07-acd0-d88e4a48d547</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-061df7b5-32a2-4021-852d-2df98953e076">
<wsuCreated>2011-05-27T12:11:45Z</wsuCreated>
<wsuExpires>2011-05-27T12:16:45Z</wsuExpires>
</wsuTimestamp>
</envHeader>
<soapBody>
<searchPersonResponse xmlns="http://www.rechtspraak.nl/namespaces/ccr01">
<searchPersonResult>
<CCR_WS xmlns="http://www.rechtspraak.nl/namespaces/ccr">
<exceptie errorcode="1">No Results found.</exceptie>
</CCR_WS>
</searchPersonResult>
</searchPersonResponse>
</soapBody>
</soapEnvelope>
Here is my code to select the namespace, then check
$results = simplexml_load_string($response);
$results->registerXPathNamespace('ccr','http://www.rechtspraak.nl/namespaces/ccr');
$lijst = $results->xpath('//ccr:CCR_WS');
$errorcode = $lijst[0]->exceptie->attributes()->errorcode;
$error = $lijst[0]->exceptie;
if (isset($errorcode) AND $errorcode != "") {
// do things with the error code
} else {
$lijst = $results->xpath('//ccr01:searchPersonResult');
$cur = $lijst[0]->CCR_WS->curandus;
echo $cur->ccn."<BR>";
echo $cur->cur_voornamen."<BR>";
echo $cur->cur_voorvoegsels."<BR>";
echo $cur->cur_achternaam."<BR>";
echo $cur->geboorte_datum."<BR>";
echo $cur->geboorte_plaats."<BR>";
}
surely there is a better way of grabbing
$lijst[0]->exceptie->attributes()->errorcode
for example...
...Don't know if this is a "better way" to everyone, but here is a direct XPath expression to select the errorcode. You can make it shorter and less efficient by dropping steps and using // (in the beginning or in the middle). Attributes are selected with # (or with attribute:: axis if you prefer the longer syntax). If attribute (or the exceptie element) doesn't exist, nothing is returned.
/*/*/ccr01:searchPersonResponse/ccr01:searchPersonResult/ccr:CCR_WS/ccr:exceptie/#errorcode
Remember to register all the namespace prefixes that yo use in your XPath expression.

Nant - Get Newest Folder

Is there a relatively simple way in nant, without writing a custom task, to get the name of the newest folder in a certain directory? Recursion is not needed. I have been trying to do it with directory::get-creation-time and a foreach loop and if statements, yada yada. It's too complex, and I'm about to create a custom task instead. However, I suspect there is some simpler way to do it via existing nant features.
I believe you're correct in stating that doing this in a pure nant fashion might pose to be messy, especially the way properties work in nant. If you don't want to write a custom task, you can always use the script task. For example:
<?xml version="1.0"?>
<project name="testing" basedir=".">
<script language="C#" prefix="test" >
<code>
<![CDATA[
[Function("find-newest-dir")]
public static string FindNewestDir( string startDir ) {
string theNewestDir = string.Empty;
DateTime theCreateTime = new DateTime();
DateTime theLastCreateTime = new DateTime();
string[] theDirs = Directory.GetDirectories( startDir );
for ( int theCurrentIdx = 0; theCurrentIdx < theDirs.Length; ++theCurrentIdx )
{
if ( theCurrentIdx != 0 )
{
DateTime theCurrentDirCreateTime = Directory.GetCreationTime( theDirs[ theCurrentIdx ] );
if ( theCurrentDirCreateTime >= theCreateTime )
{
theNewestDir = theDirs[ theCurrentIdx ];
theCreateTime = theCurrentDirCreateTime;
}
}
else
{
theNewestDir = theDirs[ theCurrentIdx ];
theCreateTime = Directory.GetCreationTime( theDirs[ theCurrentIdx ] );
}
}
return theNewestDir;
}
]]>
</code>
</script>
<property name="dir" value="" overwrite="false"/>
<echo message="The newest directory is: ${test::find-newest-dir( dir )}"/>
</project>
With this, one should be able to call the function to get the newest directory. The implementation of the actual function could be changed to be anything (optimized a bit more or whatever), but I've included a quick one for reference on how to use the script task. It produces output like the following:
nant -D:dir=c:\
NAnt 0.85 (Build 0.85.2478.0; release; 10/14/2006)
Copyright (C) 2001-2006 Gerry Shaw
http://nant.sourceforge.net
Buildfile: file:///C:/tmp/NAnt.build
Target framework: Microsoft .NET Framework 2.0
[script] Scanning assembly "jdrgmbuy" for extensions.
[echo] The newest directory is: C:\tmp
BUILD SUCCEEDED
Total time: 0.3 seconds.