Rocky
Rocky

Reputation: 5

How to add xsi type value namespace?

I'm very new to XSLT, can you please let me know XSLT code to be used to convert below input to below output XML?

This is my input XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="tutorials.xsl"?>
<n:Envelope xmlns:n="http://schemas.xmlsoap.org/soap/envelope/">
   <n:Header>
  </n:Header>
   <n:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
      <ns1:sayHello xmlns:ns1="http://webservice_product/helloworld">
         <toWhom>Micky</toWhom>
         <toMe>123</toMe>
         <objAs>
            <id>323232</id>
         </objAs>
      </ns1:sayHello>
   </n:Body>
</n:Envelope>

This is my desired output XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="tutorials.xsl"?>
<n:Envelope xmlns:n="http://schemas.xmlsoap.org/soap/envelope/">
   <n:Header>
  </n:Header>
   <n:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
      <ns1:sayHello xmlns:ns1="http://webservice_product/helloworld" xsi:type="ns698:Product" xmlns:ns698="urn:objects.prodcuts.com">
         <toWhom>Micky</toWhom>
         <toMe>123</toMe>
         <objAs>
            <id>323232</id>
         </objAs>
      </ns1:sayHello>
   </n:Body>
</n:Envelope>

I tried hard to achieve desired output XML, it didn't help me. Moreover Stack Overflow is not allowing me to paste all my XSL code.

Upvotes: 0

Views: 5647

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167446

Your desired XML output is not namespace well-formed as the xsi:type attribute used the prefix xsi without any declaration for it. Assuming you want to add that declaration on the ns1:sayHello element you could use

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:n="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:ns1="http://webservice_product/helloworld"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="ns1:sayHello">
  <ns1:sayHello xsi:type="ns698:Product" xmlns:ns698="urn:objects.prodcuts.com">
    <xsl:apply-templates select="@* | node()"/>
  </ns1:sayHello>
</xsl:template>

</xsl:stylesheet>

When I apply that stylesheet with Saxon 6.5.5 to the input

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="tutorials.xsl"?>
<n:Envelope xmlns:n="http://schemas.xmlsoap.org/soap/envelope/">
   <n:Header>
  </n:Header>
   <n:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
      <ns1:sayHello xmlns:ns1="http://webservice_product/helloworld">
         <toWhom>Micky</toWhom>
         <toMe>123</toMe>
         <objAs>
            <id>323232</id>
         </objAs>
      </ns1:sayHello>
   </n:Body>
</n:Envelope>

I get the result

<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="tutorials.xsl"?><n:Envelope xmlns:n="http://schemas.xmlsoap.org/soap/envelope/">
   <n:Header>
  </n:Header>
   <n:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
      <ns1:sayHello xmlns:ns1="http://webservice_product/helloworld" xmlns:ns698="urn:objects.prodcuts.com" xmlns:xsi="h
ttp://www.w3.org/2001/XMLSchema-instance" xsi:type="ns698:Product">
         <toWhom>Micky</toWhom>
         <toMe>123</toMe>
         <objAs>
            <id>323232</id>
         </objAs>
      </ns1:sayHello>
   </n:Body>
</n:Envelope>

Upvotes: 3

Related Questions