2010-07-15 19 views
2

有人能告訴我爲什麼會發生這種情況嗎?爲什麼這個XML是由這個XSLT輸出的?我正在使用XslCompiledTransform

我的XML是:

<?xml version="1.0" encoding="utf-8" ?> 
<example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XMLCompositeQuoteswithSelect.xsd"> 
    <title>some text goes here</title> 
    <atopelem>a top elem</atopelem> 
    <date>today</date> 
    <anode anodeattr="an attribute"> 
     <asubnode>a subnode</asubnode> 
     <somemorecontent>more content</somemorecontent> 
    </anode> 
    <anode anodeattr="another attribute"> 
     <asubnode>another subnode</asubnode> 
     <somemorecontent>even more content</somemorecontent> 
    </anode> 
</example> 

我的XSL是:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet 
version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="XMLCompositeQuoteswithSelect.xsd" 
exclude-result-prefixes="msxsl xsl xsi xmlns"> 

<xsl:output method="xml" indent="yes"/> 

<xsl:template match="/"> 
    <exampleelems> 
     <xsl:copy-of select="*/title | */atopelem | */date" /> 
     <xsl:for-each select="*/anode"> 
      <xsl:element name="node"> 
       <xsl:element name="anodeattr"> 
        <xsl:value-of select="@anodeattr"/> 
       </xsl:element> 
       <xsl:apply-templates /> 
      </xsl:element> 
     </xsl:for-each> 
    </exampleelems> 
</xsl:template> 

<xsl:template match="/anode/*" > 
    <xsl:copy > 
     <xsl:apply-templates /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="node()|@*" > 
    <xsl:copy /> 
</xsl:template> 
</xsl:stylesheet> 

我的輸出XML是:

<?xml version="1.0" encoding="utf-16"?> 
<exampleelems> 
    <title xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">some text goes here</title> 
    <atopelem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">a top elem</atopelem> 
    <date xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">today</date> 
    <node> 
     <anodeattr>an attribute</anodeattr> 
     <asubnode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
     <somemorecontent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
    </node> 
    <node> 
     <anodeattr>another attribute</anodeattr> 
     <asubnode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
     <somemorecontent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
    </node> 
</exampleelems> 

以及執行這個(稍微修剪)是代碼:

// create the xsl transformer 
    XslCompiledTransform t = new XslCompiledTransform(true); 
    t.Load(reader); 

    // create the writer which will output the transformed xml 
    StringBuilder sb = new StringBuilder(); 
    //XmlWriterSettings tt = new XmlWriterSettings(); 
    //tt.Encoding = new UTF8Encoding(false); 
    XmlWriter results = XmlWriter.Create(new StringWriter(sb));//, tt); 

    // write the transformed xml out to a stringbuilder 
    t.Transform(input, null, results); 

正如你可能猜到的,我真的不希望xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"屬性被複制到每個子元素,但我不知道如何阻止它。

感謝您的任何和所有幫助和信息,

馬特。

回答

2

您似乎想要轉換根元素的名稱,但複製其屬性,因此您只需要

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="example"> 
    <exampleelems> 
     <xsl:apply-templates select="@* | node()"/> 
    </exampleelems> 
    </xsl:template> 

    <xsl:template match="anode"> 
    <node> 
     <xsl:apply-templates select="@* | node()"/> 
    </node> 
    </xsl:template> 

    <xsl:template match="anode/@anodeattr"> 
    <xsl:element name="{name()}"> 
     <xsl:value-of select="."/> 
    </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 
+0

謝謝,這很好,現在我正在使用它來處理真正的XML。一個延伸問題:你怎麼說「在這個層面上,這個元素,但沒有其他人」。我想我可能不像我想的那樣瞭解模板。 – 2010-07-15 14:23:30

+0

&@Matt W:這個答案是正確的,但缺少解釋。模式實例名稱空間在每個節點**的作用域中,但將文檔根目錄**寫入輸入。如果命名空間聲明不在輸出**根元素**中(請參閱您在樣式表中輸出它的方式),它將在每個元素中添加到每個元素中,這些元素的範圍是'copy'和'copy-的'指示。 – 2010-07-15 15:04:34

+0

馬特,我認爲你最好在一個新問題中更詳細地解釋你想要達到的目標,「在這個層面上,這個元素,而不是其他人」。 如果您只想選擇一個特定的子元素(名爲「foo」)來處理簡單的操作,例如''而不是''。 – 2010-07-15 16:28:07