Leth
Leth

Reputation: 1059

Remove namespace prefix for elements generated by XSLT map

My XSLT map generates elements which all have the ns0 prefix on them. Here is an example:

<ns0:EXF_tImportTableLog class="entity">
    <ns0:AmountInclVat>2457.7</ns0:AmountInclVat>
    <ns0:CompanyId>DC</ns0:CompanyId>
    <ns0:Credit>0</ns0:Credit>

I want to remove these prefixes. This is the beginning of my XSLT code:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var ScriptNS0 userCSharp ScriptNS1" version="1.0" xmlns:st="http://schemas.microsoft.com/dynamics/2008/01/sharedtypes" xmlns:ns0="http://Edi-Portal/Datacon/Documents/PurchaseInvoice" xmlns:ScriptNS0="http://schemas.microsoft.com/BizTalk/2003/ScriptNS0" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp" xmlns:ScriptNS1="http://schemas.microsoft.com/BizTalk/2003/ScriptNS1">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <xsl:apply-templates select="/ns0:PurchaseInvoice_Cdm_Xml" />
  </xsl:template>
  <xsl:template match="/ns0:PurchaseInvoice_Cdm_Xml">

I think my problem is that there are no default namespace declared, so the mapper automatically puts the prefix on every element. I am using the visual mapper in Visual studio, so this code is auto-generated.

The input schema does not have this prefix, so I am unsure why it is being generated. Is it possible to fix in the visual mapper, or do I need to change something in the XSLT code?

Upvotes: 0

Views: 1020

Answers (1)

Michael Kay
Michael Kay

Reputation: 163498

The fragment of your XSLT code that you've shown us doesn't include any instructions that create elements, so we can't tell you specifically what you are doing wrong.

The true problem isn't the prefixes, it is that the elements have the wrong expanded name (expanded name = namespace URI + local name).

There are three ways of creating an element in the result tree:

(a) using a literal result element. In this case the expanded name of the element in the result tree will be the same as the expanded name of the element in the stylesheet

(b) using xsl:copy/xsl:copy-of. In this case the expanded name of the element in the result tree will be the same as the expanded name of the element in the source document

(c) using xsl:element. (c1) with a namespace attribute, in which case the element will be in the namespace identified by that attribute, or (c2) with no namespace attribute, in which case the namespace depends on the in-scope namespaces in the stylesheet.

I've just seen that you are auto-generating the code using VS mapper. I hate using such tools myself because it makes debugging so much more complex.

Upvotes: 1

Related Questions