【问题标题】:simple XSL sorting issue简单的 XSL 排序问题
【发布时间】:2011-03-09 16:30:05
【问题描述】:

我正在尝试使用 xsl 对 xml 进行排序。从 xml.com 获得了一些样本。它看起来合乎逻辑且直观。我试过了,有些它没有排序。我很难理解这一点。

这是我用于排序的 Xsl

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>
 <xsl:template match="SharePointSites">
  <xsl:copy>
   <xsl:apply-templates>
    <xsl:sort select="Document/@Name"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>

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


</xsl:stylesheet>

下面是我要排序的 XML。输出也是一样的。标签层次结构的缺失并不明显。正如我从 xml.com 示例中了解到的那样,我还尝试使用上面的匹配和选择标签指定完整的标签层次结构。

 <SharePointSites>

<Site Url="http://workspace.imperial.ac.uk/Activities/default.aspx" Name="Activities">

<Directory Name="Public">
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Imperial Activities Limited reg no etc.doc" Name="Imperial Activities Limited reg no etc.doc"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Property Enqiry Form.DOC" Name="Property Enqiry Form.DOC"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/New Property Enquiry Form.doc" Name="New Property Enquiry Form.doc"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/52 Princes Gardens.pdf" Name="52 Princes Gardens.pdf"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Silwood Web site Photo's.ppt" Name="Silwood Web site Photo's.ppt"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Service charge.pdf" Name="Service charge.pdf"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/SPIP-G.pdf" Name="SPIP-G.pdf"/>
<Document Url="http://workspace.imperial.ac.uk/Activities/Public/Silwood Business Park pictures.doc" Name="Silwood Business Park pictures.doc"/>
</Directory>
<Directory Name="Internal"/>
</Site>
</SharePointSites>

outup 还是一样的。这是我在 XML 文档上应用转换的方式。

XslCompiledTransform myXslTrans = new XslCompiledTransform();

            //load the Xsl 
            myXslTrans.Load(@"C:\My code\dotNet Development\SharepointXML\WebService1\SharepointSiteContent.xslt");                

            //do the actual transform of Xml document
            myXslTrans.Transform(aDoc, null, TransformedxmlWriter);

            // Set to document
            aTransforemdDoc.InnerXml = aTransformedStrbulider.ToString();

【问题讨论】:

    标签: xml sorting xslt


    【解决方案1】:

    您在错误的级别进行排序。如果要对文档进行排序,则需要一个与 &lt;Directory&gt; 匹配并包含 apply-templates 的模板。

    如果您所做的只是将输入复制到输出并进行排序,请在 Google 上搜索“xsl 身份转换”并添加与“目录”匹配的模板。

    解决办法

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="Directory">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="node()">
            <xsl:sort select="@Name"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    在您匹配Directory 的位置和apply-templates 内部,上下文节点依次是每个Document。所以排序只需@Name

    详细解释:

    1. 第一个模板是“身份模板”(记住这个表格,你会经常用到它)。
    2. 第二个模板专门处理“目录”节点。
      • 第一个 apply-templates 复制任何属性
      • 第二个apply-templates在排序后复制子节点
      • 这两个模板都隐式重用身份转换模板

    【讨论】:

    • 评论没有格式化选项。因此作为答案发布,请参阅我上面的帖子。
    • 非常感谢 Jim 的详细解释。当您在上一篇文章中提到时,我了解了身份转换,但没有应用它,因为我想先解决问题然后尝试优化它。使用您的 xsl 输出仍然相同。应用转换时我做错了什么吗?我已经添加了问题正文中的代码。
    • 破解了。这是代码中的一个愚蠢的错误。再次感谢所有的帮助和支持。
    猜你喜欢
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2011-01-08
    • 2011-01-01
    相关资源
    最近更新 更多