Parsing SOAP response using NSXMLParser with Swift - web-services

I am trying to parse the Soap response (snippet shown below) using NSXMLParser. However when I print the Element names in the didStartElemnt delegate method I only get the following elements returned.
Element's name is soap:Envelope
Element's name is soap:Body
Element's name is SearchResponse
Element's name is SearchResult
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SearchResponse xmlns="http://www.example.com/">
<SearchResult><?xml version="1.0" encoding="utf-8"?><Results xmlns="http://www.example.com/XMLSchema/SearchResult" xmlns:gms="http://www.def.ghi.uk/CM/gms" xmlns:n2=" (more here + further elements....)
Why do I not see the Results (or any subsequent) element?

Most likely it's the second <?xml . . .> directive that appears inside the <SearchResult>. That directive is only allowed at the start of an xml file.

Related

Why does TinyXml2 put XMLDeclaration at the end?

I'm using TinyXml2 v8.0.0 to create an XML buffer to send to an API. The example includes a declaration. I'm implementing this with:
XMLDocument doc;
doc.InsertEndChild(doc.NewDeclaration());
XMLElement* pRoot = doc.NewElement("Stuff");
doc.InsertFirstChild(pRoot);
The documentation for NewDeclaration states:
If the text param is null, the standard declaration is used.:
<?xml version="1.0" encoding="UTF-8"?>
You can see this as a test in https://github.com/leethomason/tinyxml2/blob/master/xmltest.cpp#L1637
But when I print the buffer out the declaration has been placed at the end of the buffer after a newline:
<Stuff>
</Stuff>
<?xml version="1.0" encoding="UTF-8"?>
Does anyone know why this is happening? I'd expect it to be at the start of the buffer with no newline.
Presumably, that's because you told it to put the declaration as the EndChild and the Stuff element as the FirstChild.

Please explain why I would get a no schema error when there is, in fact, an associated schema?

OK. So I am still learning the ins and outs of XSLT and associating schemas. In my company we use XSLT in a very specific way, to transform XML metadata from one schema to another. (i.e. Dublin Core to PBCore, our house standard metadata to METS, etc.) I have a plain XML file with our standard metadata tags. I transform it using an XSLT that has these declarations:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
<reVTMD xmlns="http://nwtssite.nwts.nara/schema/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.archives.gov/preservation/products/reVTMD.xsd"
recordCreation="2016-03-24T18:13:51.0Z" profile="profile1"
version="version1">
The output XML includes this:
<?xml version="1.0" encoding="UTF-8"?>
<reVTMD xmlns="http://nwtssite.nwts.nara/schema/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.archives.gov/preservation/products/reVTMD.xsd"
recordCreation="2016-03-24T18:13:51.0Z"
profile="profile1"
version="version1">
at the top of the document. But I still get a "There is no schema or DTD associated with the document." in Oxygen when I try to validate the document against the reVTMD schema. What am I doing wrong?
The xsi:schemaLocation attribute as its value needs a list of pairs associating a namespace with a schema location so with the input being in the namespace http://nwtssite.nwts.nara/schema/ and the schema being in the location https://www.archives.gov/preservation/products/reVTMD.xsd you need
xsi:schemaLocation="http://nwtssite.nwts.nara/schema/ https://www.archives.gov/preservation/products/reVTMD.xsd"

XSLT missing namespaces in inner tags that have same namespace declaration as the root element

I would like to create xml like this:
<rns:RootElement xmlns:rns="urn:root-element" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:root-element root-element.xsd">
<rns:DocumentWrapper>
<ins:InnerDoc xmlns:ins="urn:inner-doc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:inner-doc inner-doc.xsd">
<ins:Value>Some text</ins:Value>
</ins:InnerDoc>
</rns:DocumentWrapper>
</rns:RootElement>
With this template:
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<rns:RootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rns="urn:root-element"
xsi:schemaLocation="urn:root-element root-element.xsd">
<rns:DocumentWrapper>
<ins:InnerDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ins="urn:inner-doc"
xsi:schemaLocation="urn:inner-doc inner-doc.xsd">
<ins:Value><xsl:value-of select="//*[local-name()='SomeNode']"/></ins:Value>
</ins:InnerDoc>
</rns:DocumentWrapper>
</rns:RootElement>
</xsl:template>
But instead of result that i wanted this template gave me a little bit different result:
<rns:RootElement xmlns:rns="urn:root-element" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:root-element root-element.xsd">
<rns:DocumentWrapper>
<ins:InnerDoc xmlns:ins="urn:inner-doc" xsi:schemaLocation="urn:inner-doc inner-doc.xsd">
<ins:Value>Some text</ins:Value>
</ins:InnerDoc>
</rns:DocumentWrapper>
</rns:RootElement>
As you can see, in transformation result, InnerDoc element lacks definition of xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace, is there any way to prevent missing of this namespace?
As the namespace declaration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" is present on the root element it is in scope for the child and descendant elements and does not need to be repeated for the ins:InnerDoc element. I don't know of any way to enforce the output of duplicated namespace declarations with XSLT.
Some other APIs have a way to suppress them, like LINQ to XML with the OmitDuplicateNamespaces on the SaveOptions https://msdn.microsoft.com/en-us/library/system.xml.linq.saveoptions(v=vs.110).aspx, but that option was added rather to suppress duplicate namespace declarations when serializing LINQ to XML trees, not to enforce them.

extract value from an bad xml string

First of all i would like to big thanks to everyone in this forum.
I was trying to extract one xml tag value in the below given xml string.
Input String was :-
<?xml version="1.0" encoding="UTF-8"?>
<nonpublicExecutionReportAcknowledgement xmlns="http://www.fpml.org/FpML-5/recordkeeping" fpmlVersion="5-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fpml.org/FpML-5/recordkeeping /../xmls/SDR/recordkeeping/fpml-main-5-5.xsd">
<header>
<inReplyTo messageIdScheme="www.abc.com/msg_id">sit:GDS:1644644:1442512894123:SRD0IFR119094084</inReplyTo>
<sentBy>DTCCEU</sentBy>
<sendTo>RRasdfjasdfasdkllkd4</sendTo>
<creationTimestamp>2015-10-14T16:47:30Z</creationTimestamp>
</header>
<originalMessage>
<nonpublicExecutionReport fpmlVersion="5-5" xmlns="http://www.fpml.org/FpML-5/recordkeeping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<messageId messageIdScheme="www.abc.com/msg_id">sit:GDS:1644644:1442512894123:SRD0IFR119094084</messageId>
<sentBy messageAddressScheme="http://www.fpml.org/coding-scheme/external/cftc/interim-compliant-identifier">RRasdfjasdfasdkllkd4</sentBy>
<sendTo>DTCCEU</sendTo>
<creationTimestamp>2015-10-14T05:54:38Z</creationTimestamp>
</header>
</nonpublicExecutionReport>
</originalMessage>
</nonpublicExecutionReportAcknowledgement>
Regex i was used to extract messageId was "(?<=>).*?.(?=\</messageId)" which is working fine. But when it comes as single xml string it was not working as expected.
Failing for the below input string.
<?xml version="1.0" encoding="UTF-8"?><nonpublicExecutionReportAcknowledgement xmlns="http://www.fpml.org/FpML-5/recordkeeping" fpmlVersion="5-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fpml.org/FpML-5/recordkeeping /../xmls/SDR/recordkeeping/fpml-main-5-5.xsd"><header><inReplyTo messageIdScheme="www.abc.com/msg_id">sit:GDS:1644644:1442512894123:SRD0IFR119094084</inReplyTo><sentBy>DTCCEU</sentBy><sendTo>RRasdfjasdfasdkllkd4</sendTo> <creationTimestamp>2015-10-14T16:47:30Z</creationTimestamp></header><originalMessage><nonpublicExecutionReport fpmlVersion="5-5" xmlns="http://www.fpml.org/FpML-5/recordkeeping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><header> <messageId messageIdScheme="www.abc.com/msg_id">sit:GDS:1644644:1442512894123:SRD0IFR119094084</messageId><sentBy messageAddressScheme="http://www.fpml.org/coding-scheme/external/cftc/interim-compliant-identifier">RRasdfjasdfasdkllkd4</sentBy><sendTo>DTCCEU</sendTo><creationTimestamp>2015-10-14T05:54:38Z</creationTimestamp></header></nonpublicExecutionReport> </originalMessage></nonpublicExecutionReportAcknowledgement>
Output required was :- "sit:GDS:1644644:1442512894123:SRD0IFR119094084"
the value should get extracted between
<messageId messageIdScheme="www.abc.com/msg_id"> and </messageId>
Could you please help me to build the regex string for the above problem it will be a great help.
Cheers,
KS
How about:
<inReplyTo messageIdScheme="www.abc.com/msg_id">([a-zA-Z0-9:]+)</inReplyTo>

How to display localized date?

How do i have XSLT display a localized date to the user (in the locale of the user-agent).
For example, given an xml date in ISO format (2013-09-04T10:46:19.658):
<?xml version="1.0" encoding="utf-8"?>
<Stuff>
<Created>2013-09-04T10:46:19.658</Created>
</Stuff>
And the beginnings of a stylesheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" version="5.0" doctype-system="about:legacy-compat"/>
<xsl:template match="/Stuff">
<html>
<body>
Created: <xsl:value-of select="format-date('Created')"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The desired output depends, of course, on the locale of the user-agent (e.g. the client's browser):
Created: 01/11/34
Created: 05.09.13
Created: 05.09.2013
Created: 05/09 2013
Created: 05/09/2013
Created: 05-09-13
Created: 05-09-2013
Created: 09.05.2013
Created: 09/05/2013
Created: 13.09.05
Created: 2013.09.05
Created: 2013.09.05.
Created: 2013/09/05
Created: 2013/9/5
Created: 2013-09-05
Created: 2013-9-5
Created: 29/10/34
Created: 5. 9. 2013
Created: 5.09.2013
Created: 5.9.2013
Created: 5.9.2013 г.
Created: 5.9.2013.
Created: 5//09//2013
Created: 5/09/2013
Created: 5/9/2013
Created: 5/9/2556
Created: 5-9-2013
Created: 9/5/2013
Does XSLT support localization?
It runs on the client
The transformation of XML into HTML happens on the client. Since the transformation happens on the client, the client (obviously) knows its own locale.
For example, the client is given some xml:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso_Handheld.xslt' media='handheld'?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso_iPhone.xslt' media='only screen and (max-device-width: 480px)'?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso.xslt' media='all'?>
<Stuff>
<Created>2013-09-04T10:46:19.658</Created>
</Stuff>
Notice the lines where the client in instructed which XSLT to use.
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso_Handheld.xslt' media='handheld'?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso_iPhone.xslt' media='only screen and (max-device-width: 480px)'?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso.xslt' media='all'?>
The User-Agent fetches the XSLT, transforms the XML, and displays it to the user. All this processing happens in the locale of the client.
Standard XSLT 1.0 does not have any function to localize dates.
XSLT 2.0 has date and date-time formatting functions supporting localizations: see here but the implementation can vary - for example Saxon seem not to implement the calendar / language part.
Specific XSLT implementation have extension function supporting date-time localization - see for example this.
Even if you have a formatting function supporting localization you will still have the problem of find and pass the correct locale - if the XSLT is running on the server and the result is rendered in a browser the locale of the server could be the wrong one, and so you'll need to extract the locale information from the HTTP headers or with some JavaScript and use it on the server.