在 xsl:if 中使用 XSL:sort
Using XSL:sort within xsl:if
我只是想了解 XSL 的工作原理……主要是。我有 3 个节点(STUX、KbnApp、KbnStorge),我想以相同的方式进行转换,除了它们的“服务器”子节点列表的排序方式。我想使用一个模板来完成此操作,该模板使用 xsl:if 为“STUX”选择替代排序方法。但我无法让它工作。 STUX 服务器应按降序排列,其他服务器应按当前顺序排列。
这是我的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:sort 的 xsl:if 来执行此操作。谁能告诉我如何格式化 XSL 来做到这一点?每次我尝试放置 xsl:if 时,XSL 都无法 parsed/won 应用,我也不知道为什么。 PS 我确定我在这里做的事情有些不对,但天哪,我让它工作了 :) 我绝对愿意学习如何做得更好!
编辑:更正了一个拼写错误
<?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>
供参考...想要的结果
<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
类别进行排序。
我只是想了解 XSL 的工作原理……主要是。我有 3 个节点(STUX、KbnApp、KbnStorge),我想以相同的方式进行转换,除了它们的“服务器”子节点列表的排序方式。我想使用一个模板来完成此操作,该模板使用 xsl:if 为“STUX”选择替代排序方法。但我无法让它工作。 STUX 服务器应按降序排列,其他服务器应按当前顺序排列。
这是我的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:sort 的 xsl:if 来执行此操作。谁能告诉我如何格式化 XSL 来做到这一点?每次我尝试放置 xsl:if 时,XSL 都无法 parsed/won 应用,我也不知道为什么。 PS 我确定我在这里做的事情有些不对,但天哪,我让它工作了 :) 我绝对愿意学习如何做得更好! 编辑:更正了一个拼写错误
<?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>
供参考...想要的结果
<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
类别进行排序。