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

XSLT 1.0多子串匹配问题:按条件筛选对应itemNo

问题背景

原本使用substring-before和substring-after处理空格分隔的两个子串,现需适配数量可变的多子串场景。输入的cmp_code参数格式从"Dog=9500 Cat=9502"变为"Dog_BX=9500 Dog_CX=9502 Cat_BX=9856-BX Cat_CX=9856-CX Elephant=9787 Cow=8734",部分子串含下划线(_)和等号(=),部分仅含等号。其中动物名称为subtype_code,BX/CX为color_code,需匹配XML中对应的subtype_code和color_code,输出对应子串值作为itemNo(如XML中color_code为CX、subtype_code为Cat时,输出9856-CX),最终用于生成发送至SAGE300的JSON数据。现有XSLT模板无法满足需求,寻求XSLT 1.0解决方案。

现有模板代码
<xsl:template name="print_cmp_code_by_color_and_subtype"><xsl:param name="code"/><xsl:param name="color_code"/><xsl:param name="subtype_code"/>
        <xsl:choose>
            <xsl:when test="contains($code,' ')">
                <xsl:call-template name="print_cmp_code_by_color_and_subtype">
                    <xsl:with-param name="code" select="normalize-space(substring-before($code,' '))"/>
                    <xsl:with-param name="color_code" select="$color_code"/>
                    <xsl:with-param name="subtype_code" select="$subtype_code"/>
                </xsl:call-template>
                <xsl:call-template name="print_cmp_code_by_color_and_subtype">
                    <xsl:with-param name="code" select="normalize-space(substring-after($code,' '))"/>
                    <xsl:with-param name="color_code" select="$color_code"/>
                    <xsl:with-param name="subtype_code" select="$subtype_code"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="contains($code,'=')">
                    <xsl:if test="normalize-space(substring-before($code,'='))=$color_code">
                        <xsl:value-of select="normalize-space(substring-after($code,'='))"/>
                    </xsl:if>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
调用代码
<itemNo>
                    <xsl:choose>
                        <xsl:when test="cmp_code">
                            <xsl:choose>
                                <xsl:when test="contains(cmp_code,'=')">
                                    <xsl:call-template name="print_cmp_code_by_color">
                                        <xsl:with-param name="code" select="cmp_code"/>
                                        <xsl:with-param name="color_code" select="colors/color[col_type='external']/col_code"/>
                                        <xsl:with-param name="subtype_code" select="cmp_options/coption/opt_values/opt_value[(ov_keywords_list/keyword='SAGE') and (ov_active='true')]/ov_description"/>
                                    </xsl:call-template>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="cmp_code"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="cmp_series_code"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </itemNo>
示例XML
<?xml version="1.0" encoding="UTF-8"?>
<jobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xfatcom2.xsd" generated="2024-04-04T16:14:13" version="2.0">
<job>
<PKEY>1</PKEY>
<ID>19444</ID>
<job_id>162</job_id>
<job_year>2024</job_year>
<job_name>9999999999</job_name>
<job_components>
<component>
<PKEY>26</PKEY>
<FKEY>1</FKEY>
<cmp_code>Dog_BX=9500 Dog_CX=9502 Cat_BX=9856-BX Cat_CX=9856-CX Elephant=9787 Cow=8734</cmp_code>
<colors>
<color>
<PKEY>38</PKEY>
<FKEY>36</FKEY>
<col_type>external</col_type>
<col_code>BX</col_code>
</color>
</colors>
<cmp_options>
<coption>
<opt_values>
<opt_value>
<ov_description>Cat</ov_description>
<ov_keywords_list>
<keyword>SAGE</keyword>
</ov_keywords_list>
<ov_active>true</ov_active>
</opt_value>
</opt_values>
</coption>
</cmp_options>
</component>
<component>
<PKEY>26</PKEY>
<FKEY>1</FKEY>
<cmp_code>Dog_BX=9500 Dog_CX=9502 Cat_BX=9856-BX Cat_CX=9856-CX Elephant=9787 Cow=8734</cmp_code>
<colors>
<color>
<PKEY>38</PKEY>
<FKEY>36</FKEY>
<col_type>external</col_type>
<col_code>CX</col_code>
</color>
</colors>
<cmp_options>
<coption>
<opt_values>
<opt_value>
<ov_description>Dog</ov_description>
<ov_keywords_list>
<keyword>SAGE</keyword>
</ov_keywords_list>
<ov_active>true</ov_active>
</opt_value>
</opt_values>
</coption>
</cmp_options>
</component>
<component>
<PKEY>26</PKEY>
<FKEY>1</FKEY>
<cmp_code>Dog_BX=9500 Dog_CX=9502 Cat_BX=9856-BX Cat_CX=9856-CX Elephant=9787 Cow=8734</cmp_code>
<colors>
<color>
<PKEY>38</PKEY>
<FKEY>36</FKEY>
<col_type>external</col_type>
<col_code>CX</col_code>
</color>
</colors>
<cmp_options>
<coption>
<opt_values>
<opt_value>
<ov_description>Elephant</ov_description>
<ov_keywords_list>
<keyword>SAGE</keyword>
</ov_keywords_list>
<ov_active>true</ov_active>
</opt_value>
</opt_values>
</coption>
</cmp_options>
</component>
</job_components>
</job>
</jobs>
期望输出示例
"itemNo": "9856-CX",
"qty": "1",
"jobNo": "230108",
"batchNo": "-01",
"materials": [
{
"itemNo": "6285-BX",
"qty": "6.677",
"UOM": "FEET",
"location": "230108"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 14:48:11