Search code examples
xmlxsltxslt-2.0

Remove namespaces and replace by another namespace


My output has namespaces but this should be removed and replaced by another namespace. Is this possible?

Currently, I have an XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:wd="urn:com.workday.report/INT001"
xmlns:pi="urn:com.workday.report/INT001"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="wd:RD">
    <pi:PEE>
        <xsl:for-each-group select="wd:RE" group-by="wd:PG">
            <pi:PG>
                <pi:X>
                    <pi:PGID><xsl:value-of select="wd:PGID"/></pi:PGID>
                    <pi:DF><xsl:value-of select="current-dateTime() - xs:dayTimeDuration('P1D')"/></pi:DF>
                    <pi:DE><xsl:value-of select="current-dateTime()"/></pi:DE>
                </pi:X>
                <xsl:for-each select="current-group()">
                <xsl:call-template name="Content"/>                    
                </xsl:for-each>
            </pi:PG>
        </xsl:for-each-group>
    </pi:PEE>
</xsl:template>

<xsl:template name="Content">
<pi:EE>  
    <pi:L1>
        <pi:EID><xsl:value-of select="wd:EID"/></pi:EID>
        <pi:PGID><xsl:value-of select="wd:PGID"/></pi:PGID>
        <pi:PG><xsl:value-of select="wd:PG"/></pi:PG>
    </pi:L1>
    <pi:L2>
        <pi:EE><xsl:value-of select="wd:EE"/></pi:EE>
        <pi:BD><xsl:value-of select="wd:BD"/></pi:BD>
        <pi:GDR><xsl:value-of select="wd:GDR"/></pi:GDR>
    </pi:L2>
</pi:EE>  
</xsl:template>
</xsl:stylesheet>

And XML:

<wd:RD xmlns:wd="urn:com.workday.report/INT001">
<wd:RE>
<wd:PG>AR</wd:PG>
<wd:PGID>10102</wd:PGID>
<wd:EID>0001</wd:EID>
<wd:EE>Emp1</wd:EE>
<wd:GDR>M</wd:GDR>
<wd:BD>1900-01-01</wd:BD>
</wd:RE>
<wd:RE>
<wd:PG>OR</wd:PG>
<wd:PGID>10101</wd:PGID>
<wd:EID>0002</wd:EID>
<wd:EE>Emp2</wd:EE>
<wd:GDR>F</wd:GDR>
<wd:BD>1900-02-02</wd:BD>
</wd:RE>
<wd:RE>
<wd:PG>OR</wd:PG>
<wd:PGID>10101</wd:PGID>
<wd:EID>0003</wd:EID>
<wd:EE>Emp3</wd:EE>
<wd:BD>1900-03-03</wd:BD>
</wd:RE>
</wd:RD>

My desired output is:

<pi:PEE xmlns:pi="urn:com.workday/picof">
<pi:PG>
   <pi:X>
     <pi:PCID>101</pi:PCID>
     <pi:PGID>10101</pi:PGID>
  </pi:X>
  <pi:EE>
     <pi:L1>
        <pi:EID>0002</pi:EID>
        <pi:PGID>10101</pi:PGID>
        <pi:PG>OR</pi:PG>
     </pi:L1>
     <pi:L2>
        <pi:EE>Emp2</pi:EE>
        <pi:BD>1900-02-02</pi:BD>
        <pi:GDR>F</pi:GDR>
     </pi:L2>
  </pi:EE>
  <pi:EE>
     <pi:L1>
        <pi:EID>0003</pi:EID>
        <pi:PGID>10101</pi:PGID>
        <pi:PG>OR</pi:PG>
     </pi:L1>
     <pi:L2>
        <pi:EE>Emp3</pi:EE>
        <pi:BD>1900-03-03</pi:BD>            
     </pi:L2>
  </pi:EE>
</pi:PG>
<pi:PG>
  <pi:X>
     <pi:PCID>102</pi:PCID>
     <pi:PGID>10102</pi:PGID>
  </pi:X>
  <pi:EE>
     <pi:L1>
        <pi:EID>0001</pi:EID>
        <pi:PGID>10102</pi:PGID>
        <pi:PG>AR</pi:PG>
     </pi:L1>
     <pi:L2>
        <pi:EE>Emp1</pi:EE>
        <pi:BD>1900-01-01</pi:BD>
        <pi:GDR>M</pi:GDR>
     </pi:L2>
  </pi:EE>
</pi:PG>
</pi:PEE>

I want to remove xmlns:wd="urn:com.workday.report/INT001" xmlns:pi="urn:com.workday.report/INT001" xmlns:xs="http://www.w3.org/2001/XMLSchema" and be replaced by xmlns:pi="urn:com.workday/picof"

Also, if you noticed, any blank elements should be removed as well. For Emp3, GDR is empty.


Solution

  • I think you can simply shorten the code by making use of xpath-default-namespace and then, as you only want to output result elements for those input elements that exist, you can use apply-templates as needed to map input elements to result elements with templates:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xpath-default-namespace="urn:com.workday.report/INT001"
        xmlns:pi="urn:com.workday/picof"
        exclude-result-prefixes="#all"
        version="3.0">
    
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="RD">
          <pi:PEE>
              <xsl:for-each-group select="RE" group-by="PG">
                  <pi:PG>
                      <pi:X>
                        <pi:PGID><xsl:value-of select="PGID"/></pi:PGID>
                        <pi:DF><xsl:value-of select="current-dateTime() - xs:dayTimeDuration('P1D')"/></pi:DF>
                        <pi:DE><xsl:value-of select="current-dateTime()"/></pi:DE>
                      </pi:X>
                      <xsl:apply-templates select="current-group()"/>
                  </pi:PG>
              </xsl:for-each-group>
          </pi:PEE>
      </xsl:template>
    
      <xsl:template match="RE">
          <pi:EE>
              <pi:L1>
                  <xsl:apply-templates select="EID, PGID, PG"/>
              </pi:L1>
              <pi:L2>
                  <xsl:apply-templates select="EE, BD, GDR"/>
              </pi:L2>
          </pi:EE>
      </xsl:template>
    
      <xsl:template match="RE/*">
          <xsl:element name="pi:{local-name()}">
              <xsl:apply-templates/>
          </xsl:element>
      </xsl:template>
    
    </xsl:stylesheet>
    

    That transforms your presented input at https://xsltfiddle.liberty-development.net/6qVRKwH into the result

    <pi:PEE xmlns:pi="urn:com.workday/picof">
       <pi:PG>
          <pi:X>
             <pi:PGID>10102</pi:PGID>
             <pi:DF>2018-09-16T17:49:47.15+02:00</pi:DF>
             <pi:DE>2018-09-17T17:49:47.15+02:00</pi:DE>
          </pi:X>
          <pi:EE>
             <pi:L1>
                <pi:EID>0001</pi:EID>
                <pi:PGID>10102</pi:PGID>
                <pi:PG>AR</pi:PG>
             </pi:L1>
             <pi:L2>
                <pi:EE>Emp1</pi:EE>
                <pi:BD>1900-01-01</pi:BD>
                <pi:GDR>M</pi:GDR>
             </pi:L2>
          </pi:EE>
       </pi:PG>
       <pi:PG>
          <pi:X>
             <pi:PGID>10101</pi:PGID>
             <pi:DF>2018-09-16T17:49:47.15+02:00</pi:DF>
             <pi:DE>2018-09-17T17:49:47.15+02:00</pi:DE>
          </pi:X>
          <pi:EE>
             <pi:L1>
                <pi:EID>0002</pi:EID>
                <pi:PGID>10101</pi:PGID>
                <pi:PG>OR</pi:PG>
             </pi:L1>
             <pi:L2>
                <pi:EE>Emp2</pi:EE>
                <pi:BD>1900-02-02</pi:BD>
                <pi:GDR>F</pi:GDR>
             </pi:L2>
          </pi:EE>
          <pi:EE>
             <pi:L1>
                <pi:EID>0003</pi:EID>
                <pi:PGID>10101</pi:PGID>
                <pi:PG>OR</pi:PG>
             </pi:L1>
             <pi:L2>
                <pi:EE>Emp3</pi:EE>
                <pi:BD>1900-03-03</pi:BD>
             </pi:L2>
          </pi:EE>
       </pi:PG>
    </pi:PEE>
    

    That has a different order for the pi:PG elements but you haven't explained why your result has those elements in a different order than the corresponding input elements and I don't see any attempt in your posted XSLT to change the order so I post this as a suggestion, if the order is not right you need to edit your question and explain in what way you want to reorder or sort the groups you create.