1

We are trying to add two additional namespace/attributes in our XML file. The structure is an external definition (XSD) and we'd like to know if it would be possible if we can add two new attribute/namespaces via XSLT or this should be included in the external defintion? (Of course, updating the external defintion is the easiest way out.)

I've already looked at several questions here like:
adding attribute to the node
Adding namespace to child elements using xslt
XSLT transforming is throwing error

But I'm still clueless as to how to make this work. I'm a virgin with regard to XSLT - no experience at all. Would like to know if this is possible via XSLT.

As-is

<ns2:ProcessCommunication xmlns:ns2="http://URL">
   <ns2:communication>
      <ns2:CommunicationTemplateAbbreviation>INV</ns2:CommunicationTemplateAbbreviation>
      <ns2:CommunicationValues>
         <ns2:CommunicationValue>
            <ns2:FinancialValue>205029</ns2:FinancialValue>
            <ns2:Title>Net</ns2:Title>
         </ns2:CommunicationValue>
      </ns2:CommunicationValues>
      <ns2:CustomFields>
         <ns2:CustomField>
            <ns2:Name>SomeValue</ns2:Name>
            <ns2:Answer>
               <ns2:Value>1</ns2:Value>
            </ns2:Answer>
         </ns2:CustomField>
         <ns2:CustomField>
            <ns2:Name>Transaction Currency</ns2:Name>
            <ns2:Answer>
               <ns2:Value>EUR</ns2:Value>
            </ns2:Answer>
         </ns2:CustomField>
      </ns2:CustomFields>
   </ns2:communication>
</ns2:ProcessCommunication>

To-be:

<ns2:ProcessCommunication xmlns:ns2="http://URL">
   <ns2:communication>
      <ns2:CommunicationTemplateAbbreviation>INV</ns2:CommunicationTemplateAbbreviation>
      <ns2:CommunicationValues>
         <ns2:CommunicationValue>
            <ns2:FinancialValue>205029</ns2:FinancialValue>
            <ns2:Title>Net</ns2:Title>
         </ns2:CommunicationValue>
      </ns2:CommunicationValues>
      <ns2:CustomFields>
         <ns2:CustomField>
            <ns2:Name>SomeValue</ns2:Name>
            <ns2:Answer>
               <ns2:Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">1</ns2:Value>
            </ns2:Answer>
         </ns2:CustomField>
         <ns2:CustomField>
            <ns2:Name>Transaction Currency</ns2:Name>
            <ns2:Answer>
               <ns2:Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">EUR</ns2:Value>
            </ns2:Answer>
         </ns2:CustomField>
      </ns2:CustomFields>
   </ns2:communication>
</ns2:ProcessCommunication>

There's an additional

i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema"

in the node, Value.

I am only able to go until here, which is pretty useless.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>

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

<xsl:template match="node()">
<xsl:copy>
        <xsl:attribute name="i:type">a:string</xsl:attribute>
        <xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

I try and use <xsl:template match="Answer/Value"> and this doesn't work.

Community
  • 1
  • 1
Arvin
  • 13
  • 4

1 Answers1

1

You were just missing the namespace declaration on the XSLT itself. This should work:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"  xmlns:ns2="http://URL">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

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

<xsl:template match="ns2:Answer/ns2:Value">
<xsl:copy>
        <xsl:attribute name="i:type">a:string</xsl:attribute>
        <xsl:attribute name="xmlns:a">http://www.w3.org/2001/XMLSchema</xsl:attribute>
        <xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
  • Thanks! But the requirement was: i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema" and the output became: xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="a:string" So it should have been, "xmlns:a". Is this even logical? Or something is wrong on their end? – Arvin Sep 17 '13 at 02:36
  • @Arvin I've added the `xmlns:a` attribute, and it should give the correct output now. –  Sep 17 '13 at 02:59