Linq To XML: Writing xsi into an element? - vb.net

Just playing about with LinqToXml and I need to form a xelement as follows:
Dim xe As XElement = _
<Xml>
<ElementOne>
<SubElement></SubElement>
<SubElement></SubElement>
</ElementOne>
<ElementWithXsi xsi:type="XsiForElementWith">
<SubElement></SubElement>
</ElementWithXsi>
</Xml>
This creates an error here: xsi:type "XML namespace prefix 'xsi' is not defined"
Is it possible to write this in Linq to xml?

Well with XML and namespaces any prefix besides the "xml" and the "xmlns" prefixes needs to be defined so you need xmlns:xsi="someURI" (probably xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" but that is a guess, I don't know which namespace you want) in your XML document or, as long as you use VB.NET's XML literals you can use Imports <xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> in program's Imports section.

Related

Exception cx_st_match_element when deserializing XML?

I'm having trouble getting a simple transformation for XML to work in ABAP. I keep getting the exception cx_st_match_element when I try to execute it on a test XML document inside of a report.
I have the following XML that I want to transform into an ABAP internal table:
<?xml version="1.0" encoding="UTF-8"?>
<studenten xmlns="http://www.foo.be/bar/preinschrijvingsflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.foo.be/bar/preinschrijvingsflow bar_studenten.xsd">
<student>
<barGuid>1</barGuid>
<familienaam>Doe</familienaam>
<voornaam>John</voornaam>
<geslacht>1</geslacht>
<nationaliteit>BE</nationaliteit>
<geboortedatum>1995-11-18</geboortedatum>
<geboorteplaats>Antwerpen</geboorteplaats>
<email>John.Doe#gmail.com</email>
<straatNummer>Grote Markt 1 bus 0102</straatNummer>
<postcode>1000</postcode>
<gemeente>Brussel</gemeente>
<land>BE</land>
<telefoonnummer>+32123456789</telefoonnummer>
<academiejaar>2021</academiejaar>
</student>
</studenten>
To this end I defined the following simple transformation I called zc_tr_student:
<?sap.transform simple?>
<tt:transform
xmlns="http://www.foo.be/bar/preinschrijvingsflow"
xmlns:tt="http://www.sap.com/transformation-templates"
xmlns:ddic=" http://www.sap.com/abapxml/types/dictionary">
<tt:root name="studenten" type="ddic:ZCTT_bar_STUDENT"/>
<tt:template>
<studenten>
<tt:loop ref=".studenten" name="studenten">
<student>
<barGuid tt:value-ref="$studenten.bar_guid"/>
<familienaam tt:value-ref="$studenten.familienaam"/>
<voornaam tt:value-ref="$studenten.voornaam"/>
<geslacht tt:value-ref="$studenten.geslacht"/>
<nationaliteit tt:value-ref="$studenten.nationaliteit"/>
<geboortedatum tt:value-ref="$studenten.geboortedatum"/>
<geboorteplaats tt:value-ref="$studenten.geboorteplaats"/>
<email tt:value-ref="$studenten.email"/>
<straat_nummer tt:value-ref="$studenten.straat_nummer"/>
<postcode tt:value-ref="$studenten.postcode"/>
<gemeente tt:value-ref="$studenten.gemeente"/>
<land tt:value-ref="$studenten.land"/>
<telefoonnummer tt:value-ref="$studenten.telefoonnummer"/>
<academiejaar tt:value-ref="$studenten.academiejaar"/>
</student>
</tt:loop>
</studenten>
</tt:template>
</tt:transform>
In the tt:value-refattributes I refer to the field in the DDIC line type of the ABAP internal table corresponding to the tag in the XML.
If I call this simple transformation from an ABAP report like this:
call transformation zc_tr_student
source xml lv_bxml
result studenten = p_student.
The cx_st_match_element is thrown.
I validated both the syntax of the file and its adherence to the schema. The XML file and the XSD file are present in the same directory on the application server. I have no idea why the ST fails as the cx_st_match_element instance does not have any useful information except that it expected a studenten element. That element is clearly present in the XML file as the root element.
I'm inexperienced with defining simple transformations and I can't spot my error myself. Thank you in advance for your help,
Joshua

How to generate namespaces in XML docs with Db2?

I want to generate the following xml doc in db2 and it has several namespaces:
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode"
xmlns:ComIbmDatabase.msgnode="ComIbmDatabase.msgnode"
xmlns:ComIbmWSInput.msgnode="ComIbmWSInput.msgnode"
xmlns:ComIbmWSReply.msgnode="ComIbmWSReply.msgnode"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
xmlns:eflow="http://www.ibm.com/wbi/2005/eflow"
xmlns:utility="http://www.ibm.com/wbi/2005/eflow_utility"
nsURI="myflow/FIPRRCV.msgflow" nsPrefix="myflow_FIPRRCV.msgflow">
This does not work:
`SELECT XMLELEMENT(NAME "ecore:EPackage",
XMLNAMESPACES('eclipse.org/emf/2002/Ecore'; AS "ecore")) as "result"
FROM SYSIBM.SYSDUMMY1 WITH UR;`
How can I define multiple namespaces and use them in elements and attributes?
The function to use is called XMLNAMESPACES. The name suggests it is not a single, but multiple namespaceS are possible. :)
You can provide several namespace declarations in a comma-separated list. Only one namespace can be set as default namespace. Try something like this:
SELECT XMLELEMENT(NAME "ecore:EPackage",
XMLNAMESPACES('eclipse.org/emf/2002/Ecore' AS "ecore",
'example.com/foobar' as "foobar")) as "result"
FROM SYSIBM.SYSDUMMY1
If you need to add attributes with a prefix in their name, then just pass that combined string as attribute name to XMLATTRIBUTES. The xmi:version="2.0" in your example is "xmi:version" as name with a value of 2.0.

How to retrive value from soap response jmeter

I have this soap response and i need to get the value of the tag platformMessage i writed a xpath query but it doesnt work, checking the log file in jmeter in tells me this "Prefix must resolve to a namespace: ns2", heres the xpath query
/S:Envelope[#xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"]/S:Body/ns2:activateProductResponse[#xmlns:ns2="http://ws.business.api.fulfillmentengine.com/"]/return/platformMessage
heres the xml
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:activateProductResponse xmlns:ns2="http://ws.business.api.fulfillmentengine.millicom.com/">
<return>
<platformCode>1</platformCode>
<platformMessage>Fail operation.El set-top box ya existe. Code Response:|22098: 22098: 1</platformMessage>
<responseCode>13</responseCode>
<responseMessage>Error executing action in platforn</responseMessage>
<UUID>3cb49b29-513e-11e6-b5db-005056807f0c</UUID>
<platformName>INTRAWAY</platformName>
</return>
</ns2:activateProductResponse>
</S:Body>
</S:Envelope>
Using #xmlns:ns2 in the xpath is not valid because despite appearances a namespace is not the same as an attribute.
If you enable "use namespaces", then the following xpath should work:
/S:Envelope/S:Body/ns2:activateProductResponse/return/platformMessage
Or if you want a dirty (and slow) workaround, you can reference the node names like so:
/*[local-name()='Envelope']/*[local-name()='Body']/*/[local-name()='activateProductResponse]/return/platformMessage
you should read:
https://jmeter.apache.org/usermanual/component_reference.html#XPath_Extractor
https://jmeter.apache.org/usermanual/component_reference.html#XPath_Assertion
pay particular attention to:
As a work-round for namespace limitations of the Xalan XPath parser implementation on which JMeter is based, you can provide a Properties file which contains mappings for the namespace prefixes:
prefix1=Full Namespace 1
prefix2=Full Namespace 2
…
You reference this file in jmeter.properties file using the property:
xpath.namespace.config
Look in jmeter.properties for this property and everything is explained clearly

EclipseLink MOXy #XmlPath is returning the incorrect data for contains

I am attempting to unmarshal an XML data stream with EclipseLink MOXy with an XPath using the contains function.
When I apply the sample XPath directly to the sample XML data stream I get the correct value returned (Ref_number_1). However, when I unmarshal this using MOXy, the value that is set for refNumber1 is "Ref_number_2".
Does MOXy not support this type of XPath? It would appear that it at least is understanding it because it's not throwing an error, just setting the wrong value.
Anyone have any experience with this sort of thing? Know of a better approach?
Thanks for any help.
Marshal code:
String s = //xml stream from restful service (see xml example below);
StringReader sr = new StringReader(s);
ReferenceNumber refNum = (ReferenceNumber)marshaller.unmarshal(
new StreamSource(sr));
Member annotation:
#XmlPath("Header/ReferenceNumbers/ReferenceNumber[contains(ReferenceNumberType, \"REF_NUMBER_TYPE_1\")]/ReferenceNumber/text()")
private String refNumber1;
XML data:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Header>
<ReferenceNumbers>
<ReferenceNumber>
<ReferenceNumber>Ref_number_1</ReferenceNumber>
<ReferenceNumberType>REF_NUMBER_TYPE_1</ReferenceNumberType>
</ReferenceNumber>
<ReferenceNumber>
<ReferenceNumber>Ref_number_2</ReferenceNumber>
<ReferenceNumberType>REF_NUMBER_TYPE_2</ReferenceNumberType>
</ReferenceNumber>
</ReferenceNumbers>
</Header>
</Document>
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Currently MOXy does not support XPaths of that form. Currently a conditional check must be on an attribute contained within the element such as (see: http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html).
#XmlPath("personal-info/name[#type='first']/text()")
String firstName;
I have entered the following bug so that we improve the validation that we do on our #XmlPath annotation:
http://bugs.eclipse.org/397101
I am also interested in the use case described in your question. Would you mind entering an enhancement requires against the MOXy component for this:
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink

vb.net linq to xml syntax for documents with xml namespace

I'm trying to grasp the linq to xml 'inline query syntax' features of VB.Net
First I tried with this simple xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Child Name="somename">
<SomeAttribute>SomeValue</SomeAttribute>
</Child>
</Root>
This xml, when loaded in an XDocument, can be loaded and queried as follows:
Dim xdoc = XDocument.Load("sample.xml")
Console.WriteLine(xml.Root.<Child>.#Name)
Then I change the <Root> element in the sample xml file to:
<Root xmlns="http://SomeNamespace">
Now I can't seem to use the convenient 'Axis Properties' syntax anymore... I can only get it to work with the explicit XElement syntax:
Dim ns As XNamespace = "http://SomeNamespace"
' works, but I would like to use the same syntax as above...
Console.WriteLine(xdoc.Descendants(ns + "Child").First().Attribute("Name").Value)
I found the answer here
At first, I didn't know this syntactic feature was called "Axis Properties".
I had to add an Imports statement for the xml namespace:
Imports <xmlns:ns="http://SomeNamespace">
Then you can query with:
xdoc.Root.<ns:Child>.#Name