在 xsl:if 中使用 XSL:sort [英] Using XSL:sort within xsl:if

查看:25
本文介绍了在 xsl:if 中使用 XSL:sort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想弄清楚 XSL 是如何工作的……主要是.我有 3 个节点(STUX、KbnApp、KbnStorge),除了它们的服务器"列表如何转换之外,我想以相同的方式进行转换.孩子们被排序.我想用一个单一的模板来做到这一点,该模板使用 xsl:if 为STUX"选择另一种排序方法.但我无法让它工作.STUX 服务器应该以降序结束,其他服务器应该保持它们当前所处的任何顺序.

I'm just figuring out how XSL works... mostly. I have 3 nodes (STUX, KbnApp, KbnStorge) that I want to transform in the same way except for how their list of "Servers" children are sorted. I wanted to do this with a single template that uses an xsl:if to choose an alternate sorting method for the "STUX". But I couldn't get it to work. The STUX Servers should end up in descending order, the others should stay in whatever order they are currently in.

这是我的 XML

<?xml version="1.0" encoding="UTF-8"?>
<Categories>
  <STUX>
    <Servers>stuxsh01</Servers>
    <Servers>stuxsh03</Servers>
    <Servers>stuxsh02</Servers>
    <UnitTest>Pass</UnitTest>
  </STUX>
  <KbnApp>
    <Servers>stks01</Servers>
    <Servers>stks03</Servers>
    <Servers>stks02</Servers>
    <UnitTest>Pass</UnitTest>
  </KbnApp>
  <KbnStorage>
    <Servers>stksnfs01</Servers>
    <Servers>stksnfs02</Servers>
    <UnitTest>Fail</UnitTest>
  </KbnStorage>
</Categories>

这个转换给了我想要的结果,但我不得不制作两个几乎相同的模板,浪费了线条.我认为应该有一种方法可以使用一个模板和一个 xsl:if 来做到这一点,其中包含一个 xsl:sort.谁能告诉我如何格式化 XSL 来做到这一点?每次我尝试放置 xsl 时:如果 XSL 无法解析/将不适用,我不知道为什么.PS 我敢肯定我在这里做了一些不正确的事情,但是天哪,我让它工作了:) 我绝对愿意学习如何做得更好!更正了一个错字

This transform gives me the result that I want, but I've had to make two templates that are almost identical, wasting lines. I think there should be a way to do this with one template and an xsl:if with an xsl:sort inside it. Can anyone tell me how I would format XSL to do that? Every time I've tried putting an xsl:if the XSL can't be parsed/won't apply and I don't know why. PS I'm SURE there is things I've done here that aren't proper, but by golly I got it to work :) I'm definitely open to learning how to do it better! Corrected a typo

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<categories>
<countCategories><xsl:value-of select="count(child::*)" /></countCategories>
<xsl:apply-templates />
</categories>
</xsl:template>
<xsl:template match="KbnApp|KbnStorage">
<Category>
<Name><xsl:value-of select="name()" /></Name>
<countServers><xsl:value-of select="count(Servers)" /></countServers>
<xsl:copy-of select="UnitTest" />
<xsl:for-each select="Servers">
<Server><xsl:value-of select="."/></Server>
</xsl:for-each>
</Category>
</xsl:template>
<xsl:template match="STUX">
<Category>
<Name><xsl:value-of select="name()" /></Name>
<countServers><xsl:value-of select="count(Servers)" /></countServers>
<xsl:copy-of select="UnitTest" />
<xsl:for-each select="Servers">
<xsl:sort order="descending"/>
<Server><xsl:value-of select="."/></Server>
</xsl:for-each>
</Category>
</xsl:template>
</xsl:stylesheet>

供参考...想要的结果

For reference...The desired result

<categories>
   <countCategories>1</countCategories>
  <Category>
      <Name>STUX</Name>
      <countServers>3</countServers>
      <UnitTest>Pass</UnitTest>
      <Server>stuxsh03</Server>
      <Server>stuxsh02</Server>
      <Server>stuxsh01</Server>
   </Category>
  <Category>
      <Name>KbnApp</Name>
      <countServers>3</countServers>
      <UnitTest>Pass</UnitTest>
      <Server>stks01</Server>
      <Server>stks03</Server>
      <Server>stks02</Server>
   </Category>
  <Category>
      <Name>KbnStorage</Name>
      <countServers>2</countServers>
      <UnitTest>Fail</UnitTest>
      <Server>stksnfs01</Server>
      <Server>stksnfs02</Server>
   </Category>
</categories>

推荐答案

尝试更改:

<xsl:sort order="descending"/>

到:

<xsl:sort select="self::*[parent::STUX]" order="descending"/>

然后您可以在匹配所有类别的模板中使用它,并且只对 STUX 类别进行排序.

Then you can use this in a template that matches all your categories, and have only the STUX category sorted.

这篇关于在 xsl:if 中使用 XSL:sort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆