You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于XSLT 1.0实现XML节点排序:将A、B节点前置的技术问询

Solution for Reordering XML Nodes in XSLT 1.0 (BizTalk Compatible)

I get it—working with XSLT 1.0 in an older BizTalk environment can feel restrictive, but we can absolutely achieve your goal of moving all <Message> nodes containing <A> or <B> to the front, while keeping the <Order> and <Article> related messages in their original sequence.

Here's a straightforward XSLT 1.0 stylesheet that does exactly what you need:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <!-- Identity template: copies all nodes/attributes by default -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Custom template to reorder the Message nodes inside Messages -->
    <xsl:template match="Messages">
        <xsl:copy>
            <!-- Apply templates to all Message nodes, sorted by priority -->
            <xsl:apply-templates select="Message">
                <!-- Sort rule: prioritize A/B messages first -->
                <xsl:sort 
                    select="number(local-name(*) = 'A' or local-name(*) = 'B')" 
                    data-type="number" 
                    order="descending"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

How this works:

  1. Identity Template: This handles copying all existing XML content (nodes, attributes, text) exactly as-is, so we only need to override the behavior for the <Messages> node where we need reordering.
  2. Sort Logic:
    • local-name(*) = 'A' or local-name(*) = 'B' checks if the child node of a <Message> is either <A> or <B>. This returns a boolean (true or false).
    • The number() function converts that boolean to 1 (for true) or 0 (for false).
    • Using order="descending" ensures that all messages with a sort value of 1 (A/B) come before those with 0 (Order/Article).
    • Crucially, XSLT 1.0 preserves the original document order for nodes that have the same sort key. So your A/B messages stay in the order they appeared in the input, and the Order/Article messages also retain their original sequence—perfect for your requirement.

Testing this with your sample input will produce exactly the output you provided, with all A/B messages grouped at the start, followed by the original sequence of Order/Article messages.

内容的提问来源于stack exchange,提问作者JohanB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:13:11