簡體   English   中英

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

[英]How to get XslCompiledTransform to include xmlns in root element?

我正在嘗試使用XslCompiledTransform C#類將一個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>

以前,您曾嘗試在XSLT中使用xpath-default-namespace ,而XSLT 1.0不支持該xpath-default-namespace

相反,您將需要使用綁定到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匹配,使用的名稱空間前綴都是任意的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM