如何让XslCompiledTransform在根元素中包含xmlns?

时间:2017-04-16 03:38:00

标签: c# xml xslt xslcompiledtransform

我正在尝试使用XslCompiledTransform C#类将一个xml文件转换为另一个xml文件。但是,xmlns属性未被传输。

我的代码:

XmlReader reader = XmlReader.Create("machine1.xml");
XmlWriter writer = XmlWriter.Create("machine2.xml");

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

transform.Transform(reader, writer);

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="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="Disabled" />
</xsl:stylesheet>

输入:

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

这是实际的输出:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMachine xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/CMachines" >
    <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>

这是所需的输出:

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

1 个答案:

答案 0 :(得分:1)

您以前尝试在XSLT中使用xpath-default-namespace,XSLT 1.0不支持。

相反,您需要使用绑定到XML中指定的命名空间的命名空间前缀来匹配该命名空间中的Disabled元素。

试试这个XSLT

<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="cm:Disabled" />
</xsl:stylesheet>

注意,只要名称空间URI匹配,所使用的名称空间前缀是任意的。