0

This was fixed by adding a template for the root node, which automatically added all the needed prefixes to it, with no additional change.

This is the final XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:bfp="http://www.test.com/breakFixProcess/3.0/"
                xmlns:dm="http://www.test.com/dataModel/3.0/"
                xmlns:xmlns="http://www.w3.org/2000/xmlns/"
                exclude-result-prefixes="xmlns xs">
  <xsl:template match="/bfp:*">
    <bfp:Message>
    <xsl:apply-templates select="@* , node()"/>
    </bfp:Message>
  </xsl:template>
  <xsl:template match="bfp:* | @bfp:*">
    <xsl:element name="bfp:{local-name()}">
      <xsl:for-each select="@*">
        <xsl:attribute name="{name(.)}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates select="@* , node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="dm:*|@dm:*">
    <xsl:element name="dm:{local-name()}">
      <xsl:for-each select="@*">
        <xsl:attribute name="{name(.)}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates select="@* , node()"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

I'm trying to find a way to include a prefix in a field, without having the xmlns prefix redefined.

I've been trying to include an additional prefix to the root element using

  < xsl:attribute name="xmlns:dm">www.test.com < /xsl:attribute >

But, since XMLNS is not defined, then it adds its definition, which is processed as an attempt to redefine the xmlns prefix.

Is there a way to include this? The dm prefix is defined in my stylesheet.

This is my XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:bfp="http://www.test.com/breakFixProcess/3.0/"
                xmlns:dm="http://www.test.com/dataModel/3.0/"
                xmlns="http://www.w3.org/2000/xmlns/"
                exclude-result-prefixes="xmlns">
  <xsl:template match="bfp:* | @bfp:*">
    <xsl:element name="bfp:{local-name()}" >
      <xsl:for-each select="@*">
        <xsl:attribute name="{name(.)}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:attribute name="xmlns:dm">http://www.test.com/dataModel/3.0/</xsl:attribute>
      <xsl:apply-templates select="@* , node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="dm:* | @dm:*">
    <xsl:element name="dm:{local-name()}">
      <xsl:for-each select="@*">
        <xsl:attribute name="{name(.)}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates select="@* , node()"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Which adds the xmlns:xmlns prefix to the resulting XML.

fernandonos
  • 173
  • 1
  • 4
  • 15
  • You should read up on the `namespace` attribute of `xsl:element` and friends. Basically: (1) declare the namespace mapping to a prefix on your `xsl:stylesheet`, (2) use the prefix when generating qnames (`xsl:element name="prefix:name"`) and make sure to set the namespace URI. – user268396 Nov 28 '17 at 20:18
  • Thanks for your comment. I added more details to the description of the problem, which I hope makes things clearer. – fernandonos Nov 28 '17 at 20:24
  • Consider to add a minimal input sample and then the output you want and the one you get currently. – Martin Honnen Nov 28 '17 at 20:46
  • The intent to serialize a namespace declaration through attribute serialization is explicitly disallowed by the [spec](https://www.w3.org/TR/xslt-10/#creating-attributes). Because you are using XSLT 2.0 you have the `xsl:namespace` instruction as stated in the proposed duplicated question. – Alejandro Aug 16 '19 at 19:43

0 Answers0