xml - 为什么这个 XSLT 输出这个 XML?我正在使用 XslCompiledTransform

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

感谢您提供的任何帮助和信息,

马特。

最佳答案

您似乎想转换根元素的名称但复制其属性,因此您想要的只是例如

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

关于xml - 为什么这个 XSLT 输出这个 XML?我正在使用 XslCompiledTransform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3254535/

相关文章:

c++ - C++中的src/文件夹结构?

php - 在 Ubuntu 12.04 上启用 XSL

android - 将 ImageView 和 TextView 放在同一行

当属性值不同时XSLT合并节点

java - junitreport : xslt fails with StackOverflowError when there are many newlines/linefeeds

C# 使用项目中的每个命名空间

ruby-on-rails - ruby /rails : Reopening vs Overwriting a Class

sql - 从 SQL 中读取另存为文本的 XML

java - .classpath xml 中的环境变量

java - 使用 XSD、目录解析器和用于 XSLT 的 JAXP DOM 验证 XML