I want to add a node to an XML document using xslt. I am using msxsl as processor. The XML document has this structure:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PlatformShortName>SDK_NAME</PlatformShortName>
</PropertyGroup>
</Project>
The XSL rule inserts the desired node:
(Edit: added the XSL namespace)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
exclude-result-prefixes="ms">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="ms:Project/ms:PropertyGroup">
<PropertyGroup>
<xsl:element name="PlatformInstructionSet">AMRv7</xsl:element>
<xsl:apply-templates/>
</PropertyGroup>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
But the result has moved the namespace attribute from Project
to PropertyGroup
.
(Edit: I want the Project
and the PropertyGroup
identical to the input.)
<Project>
<PropertyGroup xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PlatformInstructionSet>AMRv7</PlatformInstructionSet>
<PlatformShortName>SDK_NAME</PlatformShortName>
</PropertyGroup>
</Project>
It's necessary to avoid it. How can I only add an node without changing the structure? Additionall I would like to have the new node inserted like the other nodes.