【问题标题】:Why is this XML being output by this XSLT? I'm using the XslCompiledTransform为什么这个 XSLT 会输出这个 XML?我正在使用 XslCompiledTransform
【发布时间】:2011-03-16 08:02:16
【问题描述】:

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

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

感谢所有帮助和信息,

马特。

【问题讨论】:

    标签: xml xslt namespaces


    【解决方案1】:

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

    <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。一个扩展问题:你怎么说“在这个级别,这个元素,但没有其他元素”。我想我可能不像我想象的那样理解模板。
    • & @Matt W:这个答案是正确的,但缺少解释。模式实例命名空间在每个节点的范围内但文档根进入输入。如果命名空间声明不在输出 root element 中(请参阅您如何在样式表中输出它),则序列化面将添加到每个元素中,该元素在copy 的范围内,并且copy-of 说明。
    • 马特,我认为你最好在一个新问题中更详细地解释你想用“在这个级别,这个元素但没有其他元素”来实现什么。如果您只想选择某个子元素(名为“foo”)来处理简单的操作,例如&lt;xsl:apply-templates select="foo"/&gt; 而不是 &lt;xsl:apply-templates/&gt;
    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多