每个元素的XSL按属性值获取元素值

时间:2017-09-08 15:08:36

标签: xml xslt xpath 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">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 ? 谢谢!

1 个答案:

答案 0 :(得分:0)

假设我正确理解您的问题而您只关心CONST1日期,则需要更改

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

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

您当前的XSL将从根目录中查找与该路径匹配的节点。 “..”表示从您正在查看的当前节点的父节点开始搜索。