这个XSLT非常奇怪 - 错误地获取字符串长度

时间:2017-09-18 10:23:26

标签: xml xslt xslt-1.0

我有以下xml

<?xml version="1.0" encoding="UTF-8"?>
<STEP-ProductInformation ExportTime="2016-08-08 18:21:16" ExportContext="en-EN" ContextID="en-EN" WorkspaceID="Main" UseContextLocale="false">
<Classifications>
...
</Classifications>
   <Products>
      <Product ID="prd_119481004" UserTypeID="obj_product" ParentID="prd_eng_10000807">
         <Name>product name1</Name>
         <ClassificationReference ClassificationID="cls_0121781" Type="ref_etc_online_catalog"/>
         <Values>
            <Value AttributeID="atr_web_preoder_sell_type" ID="117184">23</Value>
            <Value AttributeID="atr_self_checkout_weight">0.13</Value>
            <Value AttributeID="atr_picking_weight_deviation">10</Value>
            <Value AttributeID="atr_renewable_goods_quantity">1000</Value>
            <Value AttributeID="atr_ext_var_gtin">atr_renewable_goods_quantity</Value>
            <Value AttributeID="atr_ext_bas_volume">90</Value>
            <Value AttributeID="atr_ext_var_uom_bas">ST</Value>
            <Value AttributeID="atr_space_planning_height">9,5</Value>
            <Value AttributeID="atr_space_planning_width">6</Value>
            <Value AttributeID="atr_ext_ar_gr_nr_1">6454100</Value>
            <MultiValue AttributeID="atr_ru_storage_temperature1">
               <Value>25</Value>
            </MultiValue>
            <Value AttributeID="atr_space_planning_depth">3</Value>
            <Value AttributeID="atr_ext_var_list_max_until">14-sep-2017</Value>
            <Value AttributeID="atr_ru_vat" Derived="true">18</Value>
            <Value AttributeID="atr_pt_calc" Derived="true">fgsdg</Value>
            <Value AttributeID="atr_ru_shelf_life_val_uom" Derived="true">3</Value>
            <Value AttributeID="atr_shelf_num" Derived="true">3</Value>
            <Value AttributeID="atr_storage_num" Derived="false">280035555</Value>
            <Value AttributeID="atr_ru_bas_volume" Derived="true">0.09</Value>
            <Value AttributeID="atr_bypass_order" Derived="true">1017</Value>
            <Value AttributeID="atr_ru_shelflife_temperature1_formula" Derived="true">25°C</Value>
            <Value AttributeID="atr_picking_zone" Derived="true">1017</Value>
         </Values>
      </Product>
      <Product ID="prd_119478004" UserTypeID="obj_product" ParentID="prd_eng_10000807">
         <Name>product name2</Name>
         <ClassificationReference ClassificationID="cls_0121781" Type="ref_etc_online_catalog"/>
         <Values>
            <Value AttributeID="atr_web_preoder_sell_type" ID="117184">123</Value>
            <Value AttributeID="atr_self_checkout_weight">0.14</Value>
            <Value AttributeID="atr_picking_weight_deviation">10</Value>
            <Value AttributeID="atr_ext_var_gtin">4607177761992</Value>
            <Value AttributeID="atr_ext_bas_volume">90</Value>
            <Value AttributeID="atr_ext_var_uom_bas">ST</Value>
            <Value AttributeID="atr_space_planning_height">9,5</Value>
            <Value AttributeID="atr_space_planning_width">6</Value>
            <Value AttributeID="atr_ext_ar_gr_nr_1">6454100</Value>
            <MultiValue AttributeID="atr_ru_storage_temperature1">
               <Value>+25</Value>
            </MultiValue>
            <Value AttributeID="atr_space_planning_depth">3</Value>
            <Value AttributeID="atr_ext_var_list_max_until">31-dec-9999</Value>
            <Value AttributeID="atr_ru_vat" Derived="true">18</Value>
            <Value AttributeID="atr_pt_calc" Derived="true">dgheyareatgraehgh</Value>
            <Value AttributeID="atr_ru_shelf_life_val_uom" Derived="true">3</Value>
            <Value AttributeID="atr_shelf_num" Derived="true">3</Value>
            <Value AttributeID="atr_storage_num" Derived="true">28003</Value>
            <Value AttributeID="atr_ru_bas_volume" Derived="true">0.09</Value>
            <Value AttributeID="atr_bypass_order" Derived="true">1017</Value>
            <Value AttributeID="atr_ru_shelflife_temperature1_formula" Derived="true">25 °C</Value>
            <Value AttributeID="atr_picking_zone" Derived="true">1017</Value>
         </Values>
      </Product>
   </Products>
</STEP-ProductInformation>
  

和规则如果属性中的日期&#39; atr_ext_var_list_max_until&#39; &lt; =当前   日期比:

‘atr_storage_num’ must be empty
‘atr_shelf_num’ must be empty
‘atr_renewable_goods_quantity’ set to 0

我的XSL转换:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" xmlns:StringBuffer="java.lang.StringBuffer" xmlns:date="http://exslt.org/dates-and-times"  exclude-result-prefixes="java date StringBuffer" version="1.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:variable name="currdate" select="java:format(java:java.text.SimpleDateFormat.new('yyyy-MM-dd'), java:java.util.Date.new())" />
            <xsl:variable name="date" select="../Value[@AttributeID = 'atr_ext_var_list_max_until']" />         
            <xsl:variable name="datenumber" select="translate($date, '-', '')" />
            <xsl:variable name="currdatenumber" select="translate($currdate, '-', '')" />
            <xsl:variable name="bool" select="$datenumber &lt;= $currdatenumber" />

            <xsl:template match="node()[@AttributeID = 'atr_storage_num']">
            <xsl:variable name="vAttribs" select="@AttributeID | @Derived"/>
            <xsl:variable name="date" select="../Value[@AttributeID = 'atr_ext_var_list_max_until']" />

    <xsl:variable name="dd" select="substring-before($date, '-')"/> 
    <xsl:variable name="mmm-yyyy" select="substring-after($date, '-')"/>    
    <xsl:variable name="mmm" select="substring-before($mmm-yyyy, '-')"/>
    <xsl:variable name="yyyy" select="substring-after($mmm-yyyy, '-')"/>
    <xsl:choose>
      <xsl:when test="$mmm = 'JAN'">01</xsl:when>
      <xsl:when test="$mmm = 'FEB'">02</xsl:when>
      <xsl:when test="$mmm = 'MAR'">03</xsl:when>
      <xsl:when test="$mmm = 'APR'">04</xsl:when>
      <xsl:when test="$mmm = 'MAY'">05</xsl:when>
      <xsl:when test="$mmm = 'JUN'">06</xsl:when>
      <xsl:when test="$mmm = 'JUL'">07</xsl:when>
      <xsl:when test="$mmm = 'AUG'">08</xsl:when>
      <xsl:when test="$mmm = 'sep'">09</xsl:when>
      <xsl:when test="$mmm = 'OCT'">10</xsl:when>
      <xsl:when test="$mmm = 'NOV'">11</xsl:when>
      <xsl:when test="$mmm = 'dec'">12</xsl:when>
    </xsl:choose>
    <xsl:variable name="mmm1" select="substring($yyyy,2,0)"/>
    <xsl:variable name="yyyy1" select="substring($yyyy, -4)"/>
    <xsl:variable name="con" select="concat($dd, $yyyy)"/>
    <xsl:variable name="day" select="string-length($con)"/>


    <!--<xsl:variable name="con1" select="StringBuffer:reverse(StringBuffer:new($con))"/>-->
    <xsl:value-of select="$con" />

            <xsl:variable name="datenumber" select="translate($date, '-', '')" />
            <xsl:variable name="currdatenumber" select="translate($currdate, '-', '')" />
            <xsl:variable name="bool" select="$con &lt;= $currdatenumber" />
            <xsl:choose>
            <xsl:when test="$bool">
            <Value><xsl:copy-of select="$vAttribs"/><xsl:text></xsl:text><xsl:comment><xsl:value-of select="$bool" /></xsl:comment></Value>
            </xsl:when>
            <xsl:otherwise>
            <Value><xsl:copy-of select="node()|@*"/></Value>
            </xsl:otherwise>
            </xsl:choose>
    </xsl:template>

        <xsl:template match="node()[@AttributeID = 'atr_shelf_num']">
            <xsl:variable name="vAttribs" select="@AttributeID | @Derived"/>
            <xsl:variable name="date" select="../Value[@AttributeID = 'atr_ext_var_list_max_until']" />
            <xsl:variable name="datenumber" select="translate($date, '-', '')" />
            <xsl:variable name="currdatenumber" select="translate($currdate, '-', '')" />
            <xsl:variable name="bool" select="$datenumber &lt;= $currdatenumber" />     
            <xsl:choose>
            <xsl:when test="$bool">
            <Value><xsl:copy-of select="$vAttribs"/><xsl:text></xsl:text></Value>
            </xsl:when>
            <xsl:otherwise>
            <Value><xsl:copy-of select="node()|@*"/></Value>
            </xsl:otherwise>
            </xsl:choose>
    </xsl:template>

    <xsl:template match="node()[@AttributeID = 'atr_renewable_goods_quantity']">
            <xsl:variable name="vAttribs" select="@AttributeID | @Derived"/>
            <xsl:variable name="date" select="../Value[@AttributeID = 'atr_ext_var_list_max_until']" />
            <xsl:variable name="datenumber" select="translate($date, '-', '')" />
            <xsl:variable name="currdatenumber" select="translate($currdate, '-', '')" />
            <xsl:variable name="bool" select="$datenumber &lt;= $currdatenumber" />     
            <xsl:choose>
            <xsl:when test="$bool">
            <Value><xsl:copy-of select="$vAttribs"/><xsl:text>0</xsl:text></Value>
            </xsl:when>
            <xsl:otherwise>
            <Value><xsl:copy-of select="node()|@*"/></Value>
            </xsl:otherwise>
            </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

在变量$ con我得到09142017和12319999,但我需要yyyyMMdd。带索引的子串没有帮助,与$ mmm1,$ dd,$ yyyy的组合也没有帮助。我检查了字符串长度($ con),值非常奇怪:09142017为096,12319999为126。

我的XSL有什么不对?如何获得以下日期格式 - yyyyMMdd? 谢谢你的回答!

1 个答案:

答案 0 :(得分:1)

为了获得所需的日期格式,需要修改一些事情

将从<xsl:choose>收到的值分配给变量mon。目前<xsl:choose>未分配值。

<xsl:variable name="mon">
    <xsl:choose>
        <xsl:when test="$mmm = 'JAN' or $mmm='jan'">01</xsl:when>
        <xsl:when test="$mmm = 'FEB' or $mmm='feb'">02</xsl:when>
        <xsl:when test="$mmm = 'MAR' or $mmm='mar'">03</xsl:when>
        <xsl:when test="$mmm = 'APR' or $mmm='apr'">04</xsl:when>
        <xsl:when test="$mmm = 'MAY' or $mmm='may'">05</xsl:when>
        <xsl:when test="$mmm = 'JUN' or $mmm='jun'">06</xsl:when>
        <xsl:when test="$mmm = 'JUL' or $mmm='jul'">07</xsl:when>
        <xsl:when test="$mmm = 'AUG' or $mmm='aug'">08</xsl:when>
        <xsl:when test="$mmm = 'SEP' or $mmm='sep'">09</xsl:when>
        <xsl:when test="$mmm = 'OCT' or $mmm='oct'">10</xsl:when>
        <xsl:when test="$mmm = 'NOV' or $mmm='nov'">11</xsl:when>
        <xsl:when test="$mmm = 'DEC' or $mmm='dec'">12</xsl:when>
    </xsl:choose>
</xsl:variable>

更新以下声明

<xsl:variable name="con" select="concat($dd, $yyyy)"/>

<xsl:variable name="con" select="concat($yyyy,$mon,$dd)" />

这应该在YYYYMMDD中提供日期格式。

<con>20170914</con>

请优化XSLT,因为模板中有许多冗余代码和逻辑。