如何输出被子字符串化为循环XSLT的字符串

我一直在研究一些代码,该代码如何显示或输出带有循环中的子字符串的带有竖线的字符串/代码。查看XML部分,该节点具有三个值<vb:ecodewithamt>。问题是当我尝试运行<vb:ecodewithamt>时无法显示其他值。同样,在获得子字符串的单词/代码之后,我会将其转换为等效的“ Enumcode”,例如当它是AAA时,它应该= 3,如果BBB应该= 6,并且CCC = 7。我成功地完成了<vb:ABase><vb:AcBase>的转换,分别是'Enumcode'1和2。预先感谢!

这是我的XSLT代码,我已经尝试过此解决方案/代码

    <? xml version='1.0' encoding='UTF-8' ?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:xs="http://www.w3.org/2001/XSL/XMLSchema" exclude-result-prefixes="xs" version="2.0"
                 xmlns:vb="urn:com.vb/pfile"
                 xmlns:xsi="http://www.w3.org/2001/XSL/XMLSchema-instance" >


<xsl:output method="text" />
    <xsl:template match="/" />

         <!-- LineBreak and Delimiter -->
    <xsl:variable select="'&#x0d;&#x0a;'" name="LINE_FEED"/>
    <xsl:variable select="'|'" name="PIPE"/>

       <!-- Enum codes -->

    <xsl:variable select="'1'" name="Enumcode1"/>
    <xsl:variable select="'2'" name="Enumcode2"/>
    <xsl:variable select="'3'" name="Enumcode3"/>
    <xsl:variable select="'6'" name="Enumcode6"/>
    <xsl:variable select="'7'" name="Enumcode7"/>

        <!-- This is the header -->

    <xsl:text>EMPID</xsl:text>
       <xsl:value-of select ="$PIPE"/>
    <xsl:text>EPtypeID</xsl:text>
       <xsl:value-of select ="$PIPE"/>
    <xsl:text>EPAmt</xsl:text>
       <xsl:value-of select ="$PIPE"/>
    <xsl:text>EPPeriod</xsl:text>
       <xsl:value-of select ="$PIPE"/>
    <xsl:text>CurrID</xsl:text>
       <xsl:value-of select ="$PIPE"/>
       <xsl:value-of select ="$LINE_FEED"/>


    <xsl:variable select="vb:EMPID" name="EmployeeID"/>
    <xsl:variable select="vb:work_group/vb:ABase" name="AnBase"/>
    <xsl:variable select="vb:work_group/vb:AcBase" name="accBase"/>
    <xsl:variable select="vb:work_group/vb:PPeriod" name="PayPd"/>
 <xsl:variable select="vb:work_group/vb:Currency/vb:ID[@vb:type='Currency_ID']" name="CurrencyID"/>

    <xsl:if test="$AnBase != ' '">

        <EMPLID>
            <xsl:value-of select ="$EmployeeID"/>
        </EMPLID>
            <xsl:value-of select ="$PIPE"/>
        <EARN1>
            <xsl:value-of select ="$Enumcode1"/>
        </EARN1>
            <xsl:value-of select ="$PIPE"/>
        <Amount>
            <xsl:value-of select ="$AnBase"/>
        </Amount>
            <xsl:value-of select ="$PIPE"/>
        <PayPeriod>
            <xsl:value-of select ="$PayPd"/>
        </PayPeriod>
            <xsl:value-of select ="$PIPE"/>
        <Currency>
            <xsl:value-of select ="$CurrencyID"/>
        </Currency>
            <xsl:value-of select ="$LINE_FEED"/>
    </xsl:if>



<xsl:if test="$accBase != ' '">

        <EMPLID>
            <xsl:value-of select ="$EmployeeID"/>
        </EMPLID>
            <xsl:value-of select ="$PIPE"/>
        <EARN1>
            <xsl:value-of select ="$Enumcode1"/>
        </EARN1>
            <xsl:value-of select ="$PIPE"/>
        <Amount>
            <xsl:value-of select ="$accBase"/>
        </Amount>
            <xsl:value-of select ="$PIPE"/>
        <PayPeriod>
            <xsl:value-of select ="$PayPd"/>
        </PayPeriod>
            <xsl:value-of select ="$PIPE"/>
        <Currency>
            <xsl:value-of select ="$CurrencyID"/>
        </Currency>
            <xsl:value-of select ="$LINE_FEED"/>
    </xsl:if>

    <xsl:for-each select"vb:earn_group">
        <EMPLID>
            <xsl:value-of select ="$EmployeeID"/>
        </EMPLID>

           <!-- Here is the part of my substring -->

        <EARN1>
           <xsl:choose>
                     <xsl:when test="substring(vb:ecodewithamt,1,3 = 'AAA' ">
            <xsl:value-of select ="$Enumcode3"/>
           </xsl:when>
           <xsl:when test="substring(vb:ecodewithamt,3 = 'BBB' ">
            <xsl:value-of select ="$Enumcode6"/>
           </xsl:when>
           <xsl:when test="substring(vb:ecodewithamt,3 = 'CCC' ">
            <xsl:value-of select ="$Enumcode7"/>
           </xsl:when>
           <xsl:otherwise>
                <xsl:text></xsl:text>
           </xsl:otherwise>
        </EARN1>

            <!-- End of substring -->

        <PayPeriod>
            <xsl:value-of select ="$PayPd"/>
        </PayPeriod>
            <xsl:value-of select ="$PIPE"/>
        <Currency>
            <xsl:value-of select ="$CurrencyID"/>
        </Currency>
            <xsl:value-of select ="$LINE_FEED"/>
        </xsl:for-each>
    </xsl:for-each>
<xsl:stylesheet>

这是XML示例:

<? xml version='1.0' encoding='UTF-8' ?>

<vb:Report_info xmlns:vb="urn:com.vb/pfile">
    <vb:Report_start>
        <vb:EMPID>1111</vb:EMPID>
        <vb:work_group>
            <vb:ABase>3333</vb:ABase>
            <vb:AcBase>2222</vb:AcBase>
            <vb:PPeriod>AY</vb:PPeriod>
            <vb:Currency vb:Descriptor="XXX">
                <vb:ID vb:type="Currency_ID">XXX</vb:ID>
            </vb:Currency>
        </vb:work_group>
        <vb:earn_group>
            <vb:ecodewithamt>AAA|9999</vb:ecodewithamt>
            <vb:ecodewithamt>BBB|8888</vb:ecodewithamt>
            <vb:ecodewithamt>CCC|7777</vb:ecodewithamt>
        </vb:earn_group>
    </vb:Report_start>
</vb:Report_info>

我得到:

EMPID|EPtypeID|EPAmt|EPPeriod|CurrID

1111|1|3333|AY|XXX
1111|2|2222|AY|XXX
1111|3

输出应为:

1111|1|3333|AY|XXX
1111|2|2222|AY|XXX
1111|3|9999|AY|XXX
1111|6|8888|AY|XXX
1111|7|7777|AY|XXX
qq88744875q 回答:如何输出被子字符串化为循环XSLT的字符串

如果我完全忽略您的代码,而只专注于给定的输入和预期的输出,则会得到:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:vb="urn:com.vb/pfile">
<xsl:output method="text" />
<xsl:strip-space elements="*"/>

<xsl:template match="vb:Report_start">
    <xsl:variable name="line-begin">
        <xsl:value-of select="vb:EMPID"/>
        <xsl:text>|</xsl:text>
    </xsl:variable>

    <xsl:variable name="line-end">
        <xsl:text>|</xsl:text>
        <xsl:value-of select="vb:work_group/vb:PPeriod"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="vb:work_group/vb:Currency/vb:ID[@vb:type='Currency_ID']"/>
        <xsl:text>&#13;&#10;</xsl:text>
    </xsl:variable>

    <!-- PART A -->
    <xsl:value-of select="$line-begin"/>
    <xsl:text>1|</xsl:text>
    <xsl:value-of select="vb:work_group/vb:ABase"/>
    <xsl:value-of select="$line-end"/>

    <xsl:value-of select="$line-begin"/>
    <xsl:text>2|</xsl:text>
    <xsl:value-of select="vb:work_group/vb:AcBase"/>
    <xsl:value-of select="$line-end"/>

    <!-- PART B -->
    <xsl:for-each select="vb:earn_group/vb:ecodewithamt">
        <xsl:variable name="code" select="substring-before(.,'|')" />
        <xsl:value-of select="$line-begin"/>
        <xsl:choose>
            <xsl:when test="$code='AAA'">3</xsl:when>
            <xsl:when test="$code='BBB'">6</xsl:when>
            <xsl:when test="$code='CCC'">7</xsl:when>
        </xsl:choose>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="substring-after(.,'|')" />
        <xsl:value-of select="$line-end"/>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

其中的某些部分是基于猜测的-仍然,将其应用于示例XML时的结果是:

1111|1|3333|AY|XXX
1111|2|2222|AY|XXX
1111|3|9999|AY|XXX
1111|6|8888|AY|XXX
1111|7|7777|AY|XXX
本文链接:https://www.f2er.com/3149419.html

大家都在问