每个元素的XSL通过属性值获取元素值



我有以下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">2017-09-08</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">2015-12-31</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>

和规则如果属性中的日期'atr_ext_var_list_max_until'&lt; =当前日期,而不是:

  • ‘atr_storage_num'必须为空

  • ‘atr_shelf_num'必须为空

  • ‘atr_renewable_goods_quantity'设置为0

我的XSL变换(没有规则'atr_ext_var_list_max_until'&lt; =当前日期):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java" 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="/STEP-ProductInformation/Products/Product/Values/Value[@AttributeID = 'atr_ext_var_list_max_until']" />
    <xsl:variable name="bool" select="$date = $currdate" /> 

    <xsl:template match="node()[@AttributeID = 'atr_storage_num']">
            <xsl:variable name="vAttribs" select="@AttributeID | @Derived"/>
            <xsl:variable name="date" select="/STEP-ProductInformation/Products/Product/Values/Value[@AttributeID = 'atr_ext_var_list_max_until']" />
            <Value><xsl:copy-of select="$vAttribs"/><xsl:text>CONST1</xsl:text><xsl:comment><xsl:value-of select="$date" /></xsl:comment></Value>
    </xsl:template>

    <xsl:template match="node()[@AttributeID = 'atr_shelf_num']">       
            <xsl:variable name="vAttribs" select="@AttributeID | @Derived"/>
            <Value><xsl:copy-of select="$vAttribs"/><xsl:text>CONST2</xsl:text></Value>     
    </xsl:template>
    <xsl:template match="node()[@AttributeID = 'atr_renewable_goods_quantity']">
            <xsl:variable name="vAttribs" select="@AttributeID | @Derived"/>
            <Value><xsl:copy-of select="$vAttribs"/><xsl:text>CONST3</xsl:text></Value>         
    </xsl:template> 
</xsl:stylesheet>
<!-- <xsl:comment><xsl:value-of select="$bool" /></xsl:comment> -->

不起作用。使用此XSL后,我得到:

 <?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>
            ...
                <Value AttributeID="atr_renewable_goods_quantity">CONST3</Value>
                <Value AttributeID="atr_ext_var_list_max_until">2017-09-08</Value>              
                <Value AttributeID="atr_shelf_num" Derived="true">CONST2</Value>
                <Value AttributeID="atr_storage_num" Derived="false">CONST1<!--2017-09-08--></Value>
            ...
             </Values>
          </Product>
          <Product ID="prd_119478004" UserTypeID="obj_product" ParentID="prd_eng_10000807">
             <Name>product name2</Name>             
            ...
                <Value AttributeID="atr_ext_var_list_max_until">2015-12-31</Value>             
                <Value AttributeID="atr_shelf_num" Derived="true">CONST2</Value>
                <Value AttributeID="atr_storage_num" Derived="true">CONST1<!--2017-09-08--></Value>
            ...
             </Values>
          </Product>
       </Products>
    </STEP-ProductInformation>

用于调试我使用<comment>并在结果的第二个上下文中是从源的第一个上下文中的值。谁知道我必须如何更改XSL转换以获得正确的上下文值:在源<Value AttributeID="atr_ext_var_list_max_until">2017-09-08</Value>到第一个结果<Value AttributeID="atr_storage_num" Derived="false">CONST1<!--2017-09-08--></Value><Value AttributeID="atr_ext_var_list_max_until">2015-12-31</Value><Value AttributeID="atr_storage_num" Derived="true">CONST1<!--2015-12-31--></Value>的第一个,而不是从源2017-09-08的第一个上下文中值?谢谢!

假设我正确理解您的问题,您只是担心const1日期,您需要更改

<xsl:variable name="date" select="/STEP-ProductInformation/Products/Product/Values/Value[@AttributeID = 'atr_ext_var_list_max_until']" />

to

<xsl:variable name="date" select="../Value[@AttributeID = 'atr_ext_var_list_max_until']" />

您的当前XSL将寻找与该路径匹配的根节点。" .."是指从您要查看的当前节点的父母开始搜索。

相关内容

  • 没有找到相关文章

最新更新