Question

I am new to XSLT.

I have to convert XML to JSON using XSLT.

I have the following xml:

<getEndUserCriteriaListForRangeResponse xmlns="http://xxxx.xxxx.xx.com/">
  <endUserCriteriaList>
    <ns0:endUserCriteria xmlns:ns0="http://xxxx.xxxx.xx.com">
      <ns0:defaultValue>
        <ns0:customValue>PARAMETER</ns0:customValue>
        <ns0:eucValue>PARAMETER</ns0:eucValue>
        <ns0:eucValueId>PARAMETER</ns0:eucValueId>
      </ns0:defaultValue>
      <ns0:eucId>PARAMETER</ns0:eucId>
      <ns0:label>PARAMETER</ns0:label>
      <ns0:ranges>
        <ns0:id>PARAMETER</ns0:id>
        <ns0:rangeName>PARAMETER</ns0:rangeName>
      </ns0:ranges>
      <ns0:status>PARAMETER</ns0:status>
      <ns0:unit>PARAMETER</ns0:unit>
      <ns0:values>
        <ns0:customValue>PARAMETER</ns0:customValue>
        <ns0:eucValue>PARAMETER</ns0:eucValue>
        <ns0:eucValueId>PARAMETER</ns0:eucValueId>
      </ns0:values>
      <ns0:weight>PARAMETER</ns0:weight>
    </ns0:endUserCriteria>
  </endUserCriteriaList>
</getEndUserCriteriaListForRangeResponse>

This is the XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" />
  <xsl:template match="/">
    <xsl:copy-of select="/*" />
  </xsl:template>
</xsl:stylesheet>

And the generated JSON is:

{"@xmlns":"http://xxxx.xxxx.xx.com/","endUserCriteriaList":{"ns0:endUserCriteria":         {"@xmlns:ns0":"http://xxxx.xxxx.xx.com","ns0:defaultValue":{"ns0:customValue":"PARAMETER","ns0:eucValue":"PARAMETER","ns0:eucValueId":"PARAMETER"},"ns0:eucId":"PARAMETER","ns0:label":"PARAMETER","ns0:ranges":{"ns0:id":"PARAMETER","ns0:rangeName":"PARAMETER"},"ns0:status":"PARAMETER","ns0:unit":"PARAMETER","ns0:values":{"ns0:customValue":"PARAMETER","ns0:eucValue":"PARAMETER","ns0:eucValueId":"PARAMETER"},"ns0:weight":"PARAMETER"}}}

In the generated JSON, I need to remove "ns0:". How do I do that?

Was it helpful?

Solution

Here is a generic XSLT that can strip all of the namespaces out of a source document:

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

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

  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

When run on your input, the result is:

<getEndUserCriteriaListForRangeResponse>
  <endUserCriteriaList>
    <endUserCriteria>
      <defaultValue>
        <customValue>PARAMETER</customValue>
        <eucValue>PARAMETER</eucValue>
        <eucValueId>PARAMETER</eucValueId>
      </defaultValue>
      <eucId>PARAMETER</eucId>
      <label>PARAMETER</label>
      <ranges>
        <id>PARAMETER</id>
        <rangeName>PARAMETER</rangeName>
      </ranges>
      <status>PARAMETER</status>
      <unit>PARAMETER</unit>
      <values>
        <customValue>PARAMETER</customValue>
        <eucValue>PARAMETER</eucValue>
        <eucValueId>PARAMETER</eucValueId>
      </values>
      <weight>PARAMETER</weight>
    </endUserCriteria>
  </endUserCriteriaList>
</getEndUserCriteriaListForRangeResponse>

And I presume there is some step in your process pipeline that will convert that to the JSON you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top