Remove XSLT auto generated empty namespace xmlns=””

This is the last article of this year.
Today I have worked with XSLT and I found a great things that at the beginning can be strange.

My XML

<XmlOffert xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CompanyId>COMP_ID</CompanyId>
  <CompanyName>COMP_NAME</CompanyName>
  <Market>MI1</Market>
  <OffertValue>
    <OffertRow>
      <Hour>3</Hour>
      <Quantity>0.003</Quantity>
      <Price>3</Price>
      <Purpose>Buy</Purpose>
    </OffertRow>
    <OffertRow>
      <Hour>4</Hour>
      <Quantity>0.001</Quantity>
      <Price>1</Price>
      <Purpose>Buy</Purpose>
    </OffertRow>
  </OffertValue>
</XmlOffert>

My XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="ISO-8859-1" indent="yes" />
  <xsl:decimal-format name="european" decimal-separator=',' grouping-separator='.' />

   <xsl:template match="OffertValue">
    <xsl:param name="market" />

    <PIPTransaction>
      <BidSubmittal>
        <Market>
          <xsl:value-of select="$market" />
        </Market>
        <BidQuantity UnitOfMeasure='MWh'>
          <xsl:value-of select='format-number(Quantity, "#.##0,###","european")' />
        </BidQuantity>
        <EnergyPrice>
          <xsl:value-of select="Price" />
        </EnergyPrice>
      </BidSubmittal>
    </PIPTransaction>
  </xsl:template>

  <xsl:template match="/">
    <PIPEDocument xmlns='urn:XML-PIPE' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='urn:XML-PIPE PIPEDocument.xsd' Version='1.0'>
      <xsl:apply-templates select="XmlOffert/OffertValue">
        <xsl:with-param name="market" select="XmlOffert/Market" />
      </xsl:apply-templates>
    </PIPEDocument>
  </xsl:template>
</xsl:stylesheet>

My XSLT Result

<PIPEDocument xmlns='urn:XML-PIPE' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='urn:XML-PIPE PIPEDocument.xsd' Version='1.0'>
  <PIPTransaction xmlns=''> <-- xmlns added by xslt!
    <BidSubmittal>
      <Market>MI1</Market>
      <BidQuantity UnitOfMeasure='MWh'>0,003</BidQuantity>
      <EnergyPrice>3</EnergyPrice>
    </BidSubmittal>
    <BidSubmittal>
      <Market>MI1</Market>
      <BidQuantity UnitOfMeasure='MWh'>0,001</BidQuantity>
      <EnergyPrice>1</EnergyPrice>
    </BidSubmittal>
  </PIPTransaction>
</PIPEDocument>

This things seems strange but XSLT does this why without namespace inside a for-each or apply-templates if the first tag doesn’t specify a namespace it have to override the default parent namespace with an empty namespace.

The solution to this behaviour will appear very strange:
you have to ADD the parent namespace to the first node of the template and XSLT will REMOVE it for you. Cause xslt understands that you wanna use the same namespace as the parent and it is not necessary.

Here the fix
XSLT

   <xsl:template match="OffertValue">
    <xsl:param name="market" />

    <PIPTransaction xmlns='urn:XML-PIPE'> <-- Add the namespace of the root
      <BidSubmittal>
        <Market>
          <xsl:value-of select="$market" />
        </Market>
        <BidQuantity UnitOfMeasure='MWh'>
          <xsl:value-of select='format-number(Quantity, "#.##0,###","european")' />
        </BidQuantity>
        <EnergyPrice>
          <xsl:value-of select="Price" />
        </EnergyPrice>
      </BidSubmittal>
    </PIPTransaction>
  </xsl:template>

The result will be
My XSLT Result

<PIPEDocument xmlns='urn:XML-PIPE' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='urn:XML-PIPE PIPEDocument.xsd' Version='1.0'>
  <PIPTransaction> <-- No xmlns="" added by xslt!
    <BidSubmittal>
      <Market>MI1</Market>
      <BidQuantity UnitOfMeasure='MWh'>0,003</BidQuantity>
      <EnergyPrice>3</EnergyPrice>
    </BidSubmittal>
    <BidSubmittal>
      <Market>MI1</Market>
      <BidQuantity UnitOfMeasure='MWh'>0,001</BidQuantity>
      <EnergyPrice>1</EnergyPrice>
    </BidSubmittal>
  </PIPTransaction>
</PIPEDocument>

Here in my example also how to use number format use Europe culture info.
I shown also how to pass parameter to a template

Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in Uncategorized e contrassegnata con , , , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...