Monday 15 April 2013

web services - How to use xslt to add namespaces to xml -


I have a SOAP web service that has been defined before the contract - request and response xml is defined by xsds which That is the number of different namespaces, and 100 elements defined in XSDS. However, the web service calls a legacy layer which does not use namespace in xml. So I have a change layer between the web service and the legacy layer, which uses XSLT to change the request and response XML. On the way in the transaction layer, the use of xslt is to throw the namespace prefixes from xpareille xml, which is working fine, because against it there are only a small number of namespace prefix for the match.

While on the way I need an XSLT which will add the prefix of names back into the reaction, and I'm not sure how to do it. Reactions can include dozens of different element types; Which can be one of several different namespaces in xsds. For example, I could have a response like this:

   & Lt; Customer Name = "Jim" /> & Lt; / Order & gt;  

I need to make changes to:

  & lt; Order xmlns: types1 = "http://types1.company.com" xmlns: types2 = "http://types2.company.com" & gt; & Lt; Types1: item name = "table" /> & Lt; Type 2: Client Name = "Jim" /> & Lt; / Order & gt;  

Is this the only way to do that there is a large table in XSLT, which matches the element name of the reaction against the prefix (e.g., "item", "customer") must be used?

Or would it be better to correct some code that is loaded in xml in xsd, and then matches elements of the elements in xsd and thus gets the correct namespace?

I think that something like this can work:

   & Lt; / XSL: Copy & gt; & Lt; / XSL: Templates & gt; & Lt ;! - Elements have been recreated with names - & gt; & Lt; Xsl: template match = "*" & gt; & Lt; Xsl: variable name = "ns-uri" & gt; & Lt; XSL: Select & gt; & Lt; Xsl: when test = "name () = 'item' and name (..) = 'order'" & gt; & Lt; XSL: Text & gt; Http: //types1.company.com< / XSL: text & gt; & Lt; / XSL: When & gt; & Lt; Xsl: when test = "name () = 'customer' and name (..) = 'command'" & gt; & Lt; XSL: Text & gt; Http: //types2.company.com< / XSL: text & gt; & Lt; / XSL: When & gt; & Lt ;! - Otherwise: nothing - & gt; & Lt; / XSL: Select & gt; & Lt; / XSL: variable & gt; & Lt ;! - Create element with correct namespace - & gt; & Lt; Xsl: element name = "{name ()}" namespace = "{$ ns-uri}" & gt; & Lt; Xsl: apply-select template = "node () | @ *" /> & Lt; / XSL: element & gt; & Lt; / XSL: Templates & gt; & Lt; / XSL: stylesheet & gt;  

The output for me is:

  & lt; Order & gt; & Lt; Item name = "table" xmlns = "http://types1.company.com" & gt; & Lt; / Item & gt; & Lt; Customer name = "gym" xmlns = "http://types2.company.com" & gt; & Lt; / Customer & gt; & Lt; / Order & gt;  

Without prefix, which you have, Infoset is exactly the same.


No comments:

Post a Comment