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

XSLT字符串拆分合并:移除指定PrimeLineNo的OrderLine片段

Alright, let's tackle this problem step by step. The goal is to remove the entire escaped <OrderLine> element with PrimeLineNo="1" from the Inputxml attribute of your XML nodes—even when there are multiple OrderLine entries present.

Solution Approach

Instead of relying on error-prone string manipulation or regex (which can break if the XML structure varies slightly), we'll treat the escaped XML in the Inputxml attribute as actual XML data. Here's the plan:

  1. Decode the escaped string: Convert the Inputxml attribute's escaped XML content into a proper XML node set.
  2. Filter out the target element: Remove any OrderLine element where PrimeLineNo="1".
  3. Re-encode the remaining XML: Turn the filtered node set back into an escaped string to replace the original Inputxml attribute value.
Complete XSLT Code

We'll use XSLT 3.0 here (supported by processors like Saxon-HE) because it has built-in functions for parsing and serializing XML, making the solution clean and reliable:

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

  <!-- Handle elements with the Inputxml attribute -->
  <xsl:template match="*[@Inputxml]">
    <xsl:copy>
      <!-- Copy all original attributes except Inputxml -->
      <xsl:copy-of select="@* except @Inputxml"/>
      <!-- Rebuild the Inputxml attribute with filtered content -->
      <xsl:attribute name="Inputxml">
        <!-- Wrap escaped content in a root node to parse it properly -->
        <xsl:variable name="decoded-content" select="parse-xml(concat('&lt;root&gt;', @Inputxml, '&lt;/root&gt;'))"/>
        <!-- Serialize only the OrderLine elements that don't have PrimeLineNo=1 -->
        <xsl:value-of select="serialize($decoded-content/root/*[not(self::OrderLine[@PrimeLineNo='1'])])"/>
      </xsl:attribute>
      <!-- Copy any child nodes of the original element -->
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- Default template: copy all other nodes and attributes as-is -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Key Details Explained
  • parse-xml(): This function takes the escaped string from Inputxml, wraps it in a temporary <root> element (since XML requires a single root node), and converts it into a usable XML node set.
  • serialize(): Converts the filtered XML nodes back into an escaped string, which we use to update the Inputxml attribute.
  • @* except @Inputxml: Ensures all other attributes of the original element (like Attrib1 and Attrib2 in your test case) are preserved exactly as they are.
  • The filter [not(self::OrderLine[@PrimeLineNo='1'])]: Targets only OrderLine elements with the specific attribute value we want to remove, leaving all other content (like the <Line> element in your first example) untouched.
Test Input & Output

Let's test this with your sample XML:

Test Input

<root>
  <input Inputxml="&lt;Order..&lt;Line PPlineNO=&quot;1&quot;/&gt; &gt;" />
  <Test Attrib1="b" Attrib2="C" Inputxml=" &lt;OrderLine OrderedQty=&quot;1&quot; PrimeLineNo=&quot;1&quot; ShipNode=&quot;ABC&quot; &gt; &lt;/OrderLine &gt; &lt;OrderLine OrderedQty=&quot;1&quot; PrimeLineNo=&quot;2&quot; ShipNode=&quot;ABC&quot; &gt; &lt;/OrderLine &gt;" />
</root>

Test Output

<root>
  <input Inputxml="&lt;Order..&lt;Line PPlineNO=&quot;1&quot;/&gt; &gt;"/>
  <Test Attrib1="b" Attrib2="C" Inputxml="&lt;OrderLine OrderedQty=&quot;1&quot; PrimeLineNo=&quot;2&quot; ShipNode=&quot;ABC&quot;/&gt;"/>
</root>

Notice that the first <input> element's content is unchanged (since there's no OrderLine with PrimeLineNo="1"), and the <Test> element now only has the OrderLine with PrimeLineNo="2".

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:37:11