【问题标题】:Sort xml nodes in particular order按特定顺序对 xml 节点进行排序
【发布时间】:2023-01-11 00:52:46
【问题描述】:

我对xslt不是很好,下面是我的xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="printJob">
    <xsl:copy>
        <xsl:apply-templates select="printDoc[@type!='adhoc']" />
        <xsl:apply-templates select="printDoc[@type='adhoc']">
        
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

我的 xslt 的基本目的是转换我的 xml,以便所有打印文档具有属性的元素@Type='临时'应该在它们的父(printJob)列表中排在最后,所有其他元素应该检索它们现有的顺序。

当我所有的 printDoc 元素都包含“Type”属性时,我当前的 xslt 工作正常,但在某些 xml 中,“printDoc”元素缺少“Type”属性。

【问题讨论】:

  • [@Type!='adhoc'] 表示必须有一个 Type 属性,其值不是 adhoc[not(@Type='adhoc')] 还涵盖了没有 Type 属性的情况。

标签: xml xslt


【解决方案1】:

如果您确实在使用 XSLT 2,正如您在代码中的版本所暗示的那样,那么我认为您的要求的字面实现将是简单地使用

<xsl:apply-templates select="* except printDoc[@type='adhoc'], printDoc[@type='adhoc']"/>

XSLT 3 sample(可以用 XSLT 2 的身份模板替换 xsl:mode 声明)。

对于 XSLT 1,我会使用

<xsl:apply-templates select="*[not(self::printDoc[@type='adhoc'])]"/>
<xsl:apply-templates select="printDoc[@type='adhoc']"/>

【讨论】:

  • 出现以下错误。预期表达式结束,发现“except”。
  • @ChetanMehra,您使用哪个 XSLT 2 处理器,您到底遇到了哪个错误?
  • @ChetanMehra,这表明您没有使用 XSLT 2 处理器,不知道您为什么在问题中使用 version="2.0" 来呈现代码。无论如何,请参阅 XSLT 1.0 替代答案的编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
  • 2015-01-13
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多