c#


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>
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.

Related Links

Tooltip for intersecting items in a listbox
Get all assignments for Mechanical Turk using the .NET API
while reading from a socket
Do we make object of class having private construtor?
Checkbox in the header of a DataGridView in any column
Resizing a JPEG image in C# reduces its resolution
Accurate, Multithreaded Equivalent for System.Timers.Timer in Silverlight?
Trying to get the largest int in each line of a file and sum the result
c# static public method
How to start a Task class instance of MS TPL with some delay?
Extract an image from a video hosted on a web site
How to get the value from XML?
Encoding differences between using WebClient and WebRequest?
execute a C# method asynchronously using Threading in windows service
AJAX Tab Container - Tab Panels stopped working
how to get coordinates of scroll on RichTextBox?

Categories

HOME
swift3
redux
ide
pyspark
file-upload
liquibase
squeak
app-store
localhost
autocad
zabbix
install
qt4
ephesoft
annyang
pyodbc
segmentation-fault
oms
jcodemodel
wowza
ldap-query
adroitlogic
guidewire
email-attachments
desktop-application
chef-recipe
xforms
ejb-3.1
gnu-screen
matlab-gui
ithit-webdav-server
aws-certificate-manager
sasl
rollupjs
windows-10-mobile
bytebuffer
boost-log
visual-prolog
android-scrollview
voyager
javafxports
sdl-2
realloc
queryover
cowboy
test-kitchen
datagridviewcombobox
colorbox
head
taiga
gradlew
logan-square
android-doze-and-standby
mef
text-formatting
geminabox
flask-socketio
extjs3
dotnet-cli
webmatrix
opticalflow
reloaddata
zedgraph
nls
esprima
stripes
dimensionality-reduction
scriptengine
qtruby
jde
jcalendar
factorization
trendline
apache2-module
sqldataadapter
yfiles
bootstrap-tabs
hexagonal-tiles
p4python
litedb
opennebula
shunting-yard
pytz
indic
disparity-mapping
amzi-prolog
imaplib
mencoder
dylan
sygic
record-locking
panelgrid
infinity.js
uikit-dynamics
symbian3
magicsuggest
pageload
architectural-patterns
gssapi
derived-class
aspmenu-control
nsundomanager
cgaffinetransform
vectorwise
excellibrary
s60
webdb
dynamic-scope
focus-stealing
retrospectiva
stackless

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App