remove namespaces, attributes, Xsi from Soap response using code or XSLT - c#

Remove namespaces, attributes, Xsi from Soap response using code or XSLT
Want to transform a soap response to a normal XML(without namespaces, atributes) using C# code (Serializer, XMLDoc, XDoc ) or XSLT.
here is the soap response.
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:Magento"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:catalogProductInfoResponse>
<info xsi:type="ns1:catalogProductReturnEntity">
<product_id xsi:type="xsd:string">3459</product_id>
<sku xsi:type="xsd:string">HK-BP001</sku>
<categories SOAP-ENC:arrayType="xsd:string[0]" xsi:type="ns1:ArrayOfString"/>
<websites SOAP-ENC:arrayType="xsd:string[7]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">1</item>
</websites>
<created_at xsi:type="xsd:string">2016-04-19 01:45:35</created_at>
<has_options xsi:type="xsd:string">1</has_options>
<special_from_date xsi:type="xsd:string">2016-04-19 00:00:00</special_from_date>
<tier_price SOAP-ENC:arrayType="ns1:catalogProductTierPriceEntity[0]" xsi:type="ns1:catalogProductTierPriceEntityArray"/>
<custom_design xsi:type="xsd:string">ultimo/default</custom_design>
<enable_googlecheckout xsi:type="xsd:string">1</enable_googlecheckout>
</info>
</ns1:catalogProductInfoResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
i want transformed xml like :
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Body>
<catalogProductInfoResponse>
<info>
<product_id>3459</product_id>
<sku>HK-BP001</sku>
<categories/>
<websites>
<item>1</item>
</websites>
<created_at>2016-04-19 01:45:35</created_at>
<has_options>1</has_options>
<special_from_date>2016-04-19 00:00:00</special_from_date>
<tier_price/>
<custom_design>ultimo/default</custom_design>
<enable_googlecheckout>1</enable_googlecheckout>
</info>
</catalogProductInfoResponse>
</Body>
</Envelope>

You can use XSLT:
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

Related

How to get XslCompiledTransform to include xmlns in root element?

I'm trying to use XslCompiledTransform C# class to transform one xml file into another. However, the xmlns attribute is not being transferred.
My code:
XmlReader reader = XmlReader.Create("machine1.xml");
XmlWriter writer = XmlWriter.Create("machine2.xml");
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("transform.xsl");
transform.Transform(reader, writer);
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://schemas.datacontract.org/2004/07/CMachines" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<!-- Copy everything not subject to the exceptions below -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<!-- Ignore the disabled element -->
<xsl:template match="Disabled" />
</xsl:stylesheet>
Input:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMachine xmlns="http://schemas.datacontract.org/2004/07/CMachines" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Machine>
<Name>DellM7600</Name>
<ID>1</ID>
<Type>Laptop</Type>
<Disabled>false</Disabled>
<SerialNum>47280420</SerialNum>
</Machine>
<Machine>
<Name>DellD600</Name>
<ID>2</ID>
<Type>Laptop</Type>
<Disabled>false</Disabled>
<SerialNum>53338123</SerialNum>
</Machine>
</ArrayOfMachine>
This is the actual Output:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMachine xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/CMachines" >
<Machine>
<Name>DellM7600</Name>
<ID>1</ID>
<Type>Laptop</Type>
<Disabled>false</Disabled>
<SerialNum>47280420</SerialNum>
</Machine>
<Machine>
<Name>DellD600</Name>
<ID>2</ID>
<Type>Laptop</Type>
<Disabled>false</Disabled>
<SerialNum>53338123</SerialNum>
</Machine>
</ArrayOfMachine>
This is the desired output:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMachine xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/CMachines" >
<Machine>
<Name>DellM7600</Name>
<ID>1</ID>
<Type>Laptop</Type>
<SerialNum>47280420</SerialNum>
</Machine>
<Machine>
<Name>DellD600</Name>
<ID>2</ID>
<Type>Laptop</Type>
<SerialNum>53338123</SerialNum>
</Machine>
</ArrayOfMachine>
You were previously try to use xpath-default-namespace in your XSLT, which is not supported in XSLT 1.0.
Instead, you will need to use namespace prefix, bound to the namespace specified in your XML, to match the Disabled element which is in that namespace.
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<!-- Copy everything not subject to the exceptions below -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<!-- Ignore the disabled element -->
<xsl:template match="cm:Disabled" />
</xsl:stylesheet>
Note the namespace prefix used is arbitrary, as long as the namespace URI matches.

XSL Using Params in a contains function

Is it possible to use params as the data for a contains function?
I have a C# file that is passing information to a XSL sheet in the form of param's to make a html page that prints out the data. If I hard code the information it works but if i use params instead it returns nothing yet if i print out the information using a text tag it works so i know the value being passed in should be correct.
<xsl:param name="type"/>
<xsl:param name="filter"/>
<xsl:for-each select="london-schools/school [contains($type, '$filter')]">
that is what I am trying to do, and it just returns the table headings instead of information.
Thanks, Brandon.
Perhaps you meant:
<xsl:for-each select="london-schools/school [contains(type, $filter)]">
It's hard to tell for sure without seeing your input and the expected output - but certainly, if type is the name of a node, then it should not be prefixed by $, and if $filter is a parameter, then it should not be quoted.
Note also that XML is case-sensitive; you mention both type and Type - they are not the same.
Added:
I'm really guessing here, but consider the following:
XML
<records>
<record>
<name>Alpha</name>
<type>Bravo</type>
</record>
<record>
<name>Bravo</name>
<type>Bravo</type>
</record>
<record>
<name>Charlie</name>
<type>Alpha</type>
</record>
<record>
<name>Delta</name>
<type>Alpha</type>
</record>
</records>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="property"/>
<xsl:param name="value"/>
<xsl:template match="/records">
<xsl:copy>
<xsl:for-each select="record[contains(*[name()=$property], $value)]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When the above stylesheet is applied to the input with parameters:
$property = "name"
$value = "Bravo"
the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<records>
<record>
<name>Bravo</name>
<type>Bravo</type>
</record>
</records>
When the parameters are:
$property = "type"
$value = "Bravo"
the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<records>
<record>
<name>Alpha</name>
<type>Bravo</type>
</record>
<record>
<name>Bravo</name>
<type>Bravo</type>
</record>
</records>

missing prefix in soap request c# dynamic web request

I´m using the WsdlImporter to read a wsdl-file and create a dynamic web request. But if I create the request a prefix in my request is missing and the consuming web service can not handle with this request. How can I force, that the prefix will be set?
This is how the request should be:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:urn="urn:sap-com:document:sap:rfc:functions>
<soap:Body>
<urn:K23G_GET_COST_ELEMENTS>
<ID_BEKNZ xmlns="">S</ID_BEKNZ>
<ID_COLLECT_MESSAGES xmlns="">X</ID_COLLECT_MESSAGES>
<ID_GJAHR xmlns="">2016</ID_GJAHR>
<ID_KOKRS xmlns="">K001</ID_KOKRS>
<ID_VERSN xmlns="">000</ID_VERSN>
<ID_WRTTP xmlns="">04</ID_WRTTP>
<ET_MESG xmlns="">
<item />
</ET_MESG>
<ET_RESULTS xmlns="">
<item />
</ET_RESULTS>
<IT_COSEL_KSTAR xmlns="">
<item />
</IT_COSEL_KSTAR>
<IT_COSEL_OBJ xmlns="">
<item />
</IT_COSEL_OBJ>
</urn:K23G_GET_COST_ELEMENTS>
</soap:Body>
</soap:Envelope>
An this is how the request looks like:
The urn before K23G_GET_COST_ELEMENTS is missing
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<K23G_GET_COST_ELEMENTS xmlns:urn="urn:sap-com:document:sap:rfc:functions>
<ID_BEKNZ xmlns="">S</ID_BEKNZ>
<ID_COLLECT_MESSAGES xmlns="">X</ID_COLLECT_MESSAGES>
<ID_GJAHR xmlns="">2016</ID_GJAHR>
<ID_KOKRS xmlns="">K001</ID_KOKRS>
<ID_VERSN xmlns="">000</ID_VERSN>
<ID_WRTTP xmlns="">04</ID_WRTTP>
<ET_MESG xmlns="">
<item />
</ET_MESG>
<ET_RESULTS xmlns="">
<item />
</ET_RESULTS>
<IT_COSEL_KSTAR xmlns="">
<item />
</IT_COSEL_KSTAR>
<IT_COSEL_OBJ xmlns="">
<item />
</IT_COSEL_OBJ>
</urn:K23G_GET_COST_ELEMENTS>
</soap:Body>
</soap:Envelope>
Somebody there who can help me?
Thank you

How to call c# function in xsl

im trying to call a c# function in xsl. I have to map some values into a xml. There are 3 principal components
-xsl
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var"
xmlns:ns0="http://iti/serv/dataloader"
xmlns:HelpersNS0="http://ri/clus/mapperhelpers/v1.0/I/F/C/CustomComponents ">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/*[local-name()='InvokeDataLoader']">
<ns0:scriptToExecute>
<xsl:value-of select="HelpersNS0:GetDataLoaderPath()"/>
</ns0:scriptToExecute>
</xsl:template>
</xsl:stylesheet>
-xml where i get c# assembly
<?xml version="1.0" encoding="UTF-8" ?>
<ExtensionObjects>
<ExtensionObject
Namespace="http://ri/clus/mapperhelpers/v1.0/I/F/C/CustomComponents"
AssemblyName="G.T.I_Fatt.CustomComponents, Version=1.0.0.0,Culture=neutral, PublicKeyToken=6ecedb456a4a8c16"
ClassName="G.T.I_Fatt.CustomComponents.MapperHelpers" />
</ExtensionObjects>
-xml to transform
<?xml version="1.0" encoding="UTF-8" ?>
<InvokeDataLoader xmlns="http://iti/serv/dataloader">
<scriptToExecute/>
<inputFile/>
</InvokeDataLoader>
But when i try to transform i get this error: "namespace does not contain any functions"
I notice you have xmlns:HelpersNS0="http://ri/clus/mapperhelpers/v1.0/I/F/C/CustomComponents ts"> in the XSLT yet Namespace="http://ri/clus/mapperhelpers/v1.0/I/F/C/CustomComponents" (without the ts) in the other file. So the namespace does not match.

Optimal Method to Minify an XML in C# 3.0

Coding Platform: ASP.NET C#
I have an XML like this.
<Items>
<Map id="35">
<Terrains>
<Item id="1" row="0" column="0"/>
<Item id="1" row="0" column="1"/>
<Item id="1" row="0" column="2"/>
<Item id="1" row="0" column="3"/>
<Item id="1" row="0" column="4"/>
</Terrains>
</Map>
</Items>
I would like to minify this to
<Its>
<Map id="30">
<Te>
<It id="1" r="0" c="0"/>
<It id="1" r="0" c="1"/>
<It id="1" r="0" c="2"/>
<It id="1" r="0" c="3"/>
<It id="1" r="0" c="4"/>
</Te>
</Map>
</Its>
Then I am converting this to JSON using James Newton-King's JSON Converter.
The idea is to minify the xml data to the maximum as it contains tens of thousands of lines.
My questions are
What is the optimal method to minify the xml as mentioned above?
Now its done like XML-MinifyXML-Convert to JSON. Can I do it in two steps?(XML-Minify while converting to JSON)
Is James Newton-King's JSON converter a bit overkill for this simple conversion?
Please provide code snippets also if possible.
I suspect GZIP (via GZipStream, or simply via IIS, noting that you need to enable dynamic compression for the json mime-type) would be both simpler and smaller, but if you are using serializarion, simply adding some [XmlElement(...)] / [XmlAttribute(...)] should do it. Of course, if size is your concern, can I also suggest something like protobuf-net, which gives an extremely dense binary output.
If you aren't using serialisation, then this looks an ideal fit for some "xslt":
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy><xsl:apply-templates select="#* | node()"/></xsl:copy>
</xsl:template>
<xsl:template match="/Items">
<Its><xsl:apply-templates/></Its>
</xsl:template>
<xsl:template match="/Items/Map/Terrains">
<Te><xsl:apply-templates/></Te>
</xsl:template>
<xsl:template match="/Items/Map/Terrains/Item">
<It id="{#id}" r="{#row}" c="{#column}"><xsl:apply-templates select="*"/></It>
</xsl:template>
</xsl:stylesheet>
(with C#:)
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("Condense.xslt"); // cache and re-use this object; don't Load each time
xslt.Transform("Data.xml", "Smaller.xml");
Console.WriteLine("{0} vs {1}",
new FileInfo("Data.xml").Length,
new FileInfo("Smaller.xml").Length);

Categories

Resources