XSL根据条件选择正确的子字符串

问题描述 投票:0回答:1

我正在从带有 ' ' 分隔符的简单 2 子字符串过渡,该分隔符当前使用前子字符串和后子字符串,但现在我需要修改它以适应数量不同的多个子字符串。我已经尝试了几种不同的方法,但我要么是个菜鸟,甚至无法理解它,要么我认为它不会起作用。

<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>

代码参数的输入是问题所在。我从“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 中匹配的 color_code 的子字符串。如果子字符串包含“_”,则它使用与 XML subtype_code 匹配的子字符串。如果它有“=”和“_”,那么它使用与 XML color_code 和 subtype_code 匹配的子字符串。

我很迷茫,需要帮助。

编辑 1:如果 XML 中的 color_code 为“CX”并且 XML 中的子类型代码为“Cat”,则预期输出将为“9856-CX”。

XSLT 1.0

    <?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>
</component>
</job_components>
</job>
</jobs>
recursion xslt xsl-choose
1个回答
0
投票

您的问题仍然不清楚,但也许您可以以此为起点:

XML

<input>
    <item subtype_code="Cat" color_code="CX"/>
    <item subtype_code="Elephant"/>
</input>

XSLT 1.0

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

<xsl:param name="code">Dog_BX=9500 Dog_CX=9502 Cat_BX=9856-BX Cat_CX=9856-CX Elephant=9787 Cow=8734</xsl:param>

<xsl:template match="/input">
    <output>
        <xsl:for-each select="item">
            <item subtype_code="{@subtype_code}" color_code="{@color_code}">
                <xsl:variable name="key">
                    <xsl:value-of select="@subtype_code"/>
                    <xsl:if test="@color_code">
                        <xsl:value-of select="concat('_', @color_code)"/>
                    </xsl:if>
                    <xsl:text>=</xsl:text>
                </xsl:variable>
                <xsl:value-of select="substring-before(substring-after(concat($code, ' '), $key), ' ')"/>
            </item>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <item subtype_code="Cat" color_code="CX">9856-CX</item>
   <item subtype_code="Elephant" color_code="">9787</item>
</output>
© www.soinside.com 2019 - 2024. All rights reserved.