为什么这个XML是由这个XSLT输出的?我正在使用XslCompiledTransform

时间:2010-07-15 10:09:33

标签: xml xslt namespaces

有人可以告诉我为什么会这样吗?

我的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"属性复制到每个子元素,但我不知道如何阻止它。

感谢您提供的所有帮助和信息,

1 个答案:

答案 0 :(得分: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>