markf78
markf78

Reputation: 627

XmlCompiledTransform duplicates namespace when replacing element's value

I'm trying to use XslCompiledTransform C# class to transform one xml file into another. However, the namespace is being included a second time in one of my elements (SerialNum). What am I doing wrong?

Here's my C# code:

// Create a reader to read books.xml
XmlReader reader = XmlReader.Create("machine1.xml");

// Create a writer for writing the transformed file.
XmlWriter writer = XmlWriter.Create("machine2.xml");

XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("transform.xsl");

// Execute the transformation.
transform.Transform(reader, writer);

Here's my XSL:

<xsl:stylesheet version="1.0" xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <!-- Copy everything not subject to the exceptions below -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
<!-- Ignore the disabled element -->
<xsl:template match="cd:Disabled" />
  <!-- Reset the value of the serial num element to 0 -->
  <xsl:template match="cm:SerialNum">
    <SerialNum>0</SerialNum>
  </xsl:template>
</xsl:stylesheet>

Input:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMachine xmlns="http://schemas.datacontract.org/2004/07/CMachines" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Machine>
      <Name>DellM7600</Name>
      <ID>1</ID>
      <Type>Laptop</Type>
      <Disabled>false</Disabled>
      <SerialNum>47280420</SerialNum>
    </Machine>
    <Machine>
      <Name>DellD600</Name>
      <ID>2</ID>
      <Type>Laptop</Type>
      <Disabled>false</Disabled>
      <SerialNum>53338123</SerialNum>
    </Machine>
    </ArrayOfMachine>

Output:

<?xml version="1.0" encoding="utf-8"?><ArrayOfMachine xmlns="http://schemas.datacontract.org/2004/07/CMachines" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Machine>
      <Name>DellM7600</Name>
      <ID>1</ID>
      <Type>Laptop</Type>
      <SerialNum xmlns="" xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines">0</SerialNum>
    </Machine>
    <Machine>
      <Name>DellD600</Name>
      <ID>2</ID>
      <Type>Laptop</Type>
      <SerialNum xmlns="" xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines">0</SerialNum>
    </Machine>
    </ArrayOfMachine>

Desired Output:

<?xml version="1.0" encoding="utf-8"?><ArrayOfMachine xmlns="http://schemas.datacontract.org/2004/07/CMachines" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Machine>
      <Name>DellM7600</Name>
      <ID>1</ID>
      <Type>Laptop</Type>
      <SerialNum>0</SerialNum>
    </Machine>
    <Machine>
      <Name>DellD600</Name>
      <ID>2</ID>
      <Type>Laptop</Type>
      <SerialNum>0</SerialNum>
    </Machine>
    </ArrayOfMachine>

Upvotes: 0

Views: 87

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

Just replace:

  <xsl:template match="cm:SerialNum">
    <SerialNum>0</SerialNum>
  </xsl:template>

with:

  <xsl:template match="cm:SerialNum">
    <xsl:copy>0</xsl:copy>
  </xsl:template>

What you have now is creating a new SerialNum element in no-namespace - unlike the original, which inherits its parent's namespace. That's why you see the xmlns="" declaration: it shows that the element is in no-namespace, unlike its parent.

The xmlns:cm="http://schemas.datacontract.org/2004/07/CMachines" is simply inherited from the xsl:stylesheet ancestor. You could eliminate it by adding a exclude-result-prefixes="cm" attribute to the xsl:stylesheet element - but it's much simpler just to copy the original SerialNum element with its original namespace and no inheritance from the stylesheet.

Upvotes: 1

Related Questions