获取XML并转换为具有上下文更改的另一种xml格式

时间:2017-11-09 16:05:24

标签: xml xslt xslt-1.0

你能帮我用xslt将xml转换成另一种格式吗?我有一个输入XML需要转换为另一种格式,但我尝试使用XSLT,但徒劳无功。请提供示例代码。

这是源XML

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
   <ns0:Message1>
      <STEP-ProductInformation ExportTime="2017-11-03 14:45:02" ExportContext="en-EN" ContextID="en-EN" WorkspaceID="Main" UseContextLocale="false">
         <Products>
            <Product ID="prd_388061006" UserTypeID="obj_product" ParentID="prd_en_10000201">
               <Name>name</Name>
               <ClassificationReference ClassificationID="cls_1_6434100" Type="ref_product_to_erpgroup"/>
               <AssetCrossReference AssetID="173994" Type="ref_primary_product_image"/>
               <Values>
                  <Value AttributeID="atr_ext_var_list_ed_010_KRL">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_010_KRL">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_ed_011_KRS">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_011_KRS">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_nr">388061006</Value>
                  <Value AttributeID="atr_ext_var_list_ed_012_TVR">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_012_TVR">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_sd_013_ELS">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_ed_013_ELS">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_014_PSK">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_sd_015_ODN">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_ed_014_PSK">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_002_KLM">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_sd_003_VLD">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_ed_002_KLM">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_ed_003_VLD">9999-12-31</Value>
                  <Value AttributeID="atr_ext_ar_nr">388061</Value>
                  <Value AttributeID="atr_ext_var_list_ed_004_RYA">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_004_RYA">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_sd_005_YAR">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_sd_006_TUL">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_ed_005_YAR">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_ed_006_TUL">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_ed_001_SCH">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_001_SCH">2017-06-12</Value>
                  <Value AttributeID="atr_ext_var_list_ed_015_ODN">9999-12-31</Value>
                  <Value AttributeID="atr_pt_brand" ID="184985">1977</Value>
                  <Value AttributeID="atr_ext_var_list_max_until">31-dec-9999</Value>
                  <Value AttributeID="atr_pt_calc" Derived="true">name 1</Value>
               </Values>

            </Product>
            <Product ID="prd_15004" UserTypeID="obj_product" ParentID="prd_en_10005897">
               <Name>banana</Name>
               <ClassificationReference ClassificationID="cls_loy_bonus_current_5002_klm" Type="ref_loyalty_promotion"/>
               <ClassificationReference ClassificationID="cls_1_7361100" Type="ref_product_to_erpgroup"/>
               <AssetCrossReference AssetID="128178" Type="ref_primary_product_image"/>
               <Values>
                  <Value AttributeID="atr_ext_var_list_ed_010_KRL">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_010_KRL">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_ed_011_KRS">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_011_KRS">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_nr">15004</Value>
                  <Value AttributeID="atr_ext_var_list_ed_012_TVR">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_012_TVR">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_sd_013_ELS">2017-08-08</Value>
                  <Value AttributeID="atr_loy_current_promo_number">42010</Value>
                  <Value AttributeID="atr_ext_var_list_ed_013_ELS">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_014_PSK">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_sd_015_ODN">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_ed_014_PSK">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_002_KLM">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_sd_003_VLD">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_ed_002_KLM">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_ed_003_VLD">9999-12-31</Value>
                  <Value AttributeID="atr_ext_ar_nr">15</Value>
                  <Value AttributeID="atr_ext_var_list_ed_004_RYA">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_004_RYA">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_sd_005_YAR">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_sd_006_TUL">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_ed_005_YAR">9999-12-31</Value>
                  <Value AttributeID="atr_loy_current_promo_name">extra bonuses</Value>
                  <Value AttributeID="atr_loy_current_promo_end_date">2017-10-24</Value>
                  <Value AttributeID="atr_loy_current_promo_start_date">2017-10-11</Value>
                  <Value AttributeID="atr_ext_var_list_ed_006_TUL">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_ed_001_SCH">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_001_SCH">2017-08-08</Value>
                  <Value AttributeID="atr_ext_var_list_ed_015_ODN">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_max_until">31-dec-9999</Value>
                  <Value AttributeID="atr_pt_calc" Derived="true">name 2</Value>
               </Values>

            </Product>
            <Product ID="prd_1569001" UserTypeID="obj_product" ParentID="prd_en_10005921">
               <Name>name</Name>
               <ClassificationReference ClassificationID="cls_loy_bonus_current_5002_klm" Type="ref_loyalty_promotion"/>
               <ClassificationReference ClassificationID="cls_1_7361200" Type="ref_product_to_erpgroup"/>
               <ClassificationReference ClassificationID="cls_1_7361700" Type="ref_product_to_erpgroup"/>
               <AssetCrossReference AssetID="161621" Type="ref_primary_product_image"/>
               <AssetCrossReference AssetID="188035" Type="ref_secondary_image"/>
               <Values>
                  <Value AttributeID="atr_ext_var_list_ed_010_KRL">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_010_KRL">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_ed_011_KRS">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_011_KRS">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_nr">1569001</Value>
                  <Value AttributeID="atr_ext_var_list_ed_012_TVR">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_012_TVR">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_sd_013_ELS">2017-04-05</Value>
                  <Value AttributeID="atr_loy_current_promo_number">42010</Value>
                  <Value AttributeID="atr_ext_var_list_ed_013_ELS">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_014_PSK">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_sd_015_ODN">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_ed_014_PSK">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_002_KLM">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_sd_003_VLD">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_ed_002_KLM">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_ed_003_VLD">9999-12-31</Value>
                  <Value AttributeID="atr_ext_ar_nr">1569</Value>
                  <Value AttributeID="atr_ext_var_list_ed_004_RYA">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_004_RYA">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_sd_005_YAR">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_sd_006_TUL">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_ed_005_YAR">9999-12-31</Value>
                  <Value AttributeID="atr_loy_current_promo_name">extra bonuses</Value>
                  <Value AttributeID="atr_loy_current_promo_end_date">2017-10-24</Value>
                  <Value AttributeID="atr_loy_current_promo_start_date">2017-10-11</Value>
                  <Value AttributeID="atr_ext_var_list_ed_006_TUL">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_ed_001_SCH">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_sd_001_SCH">2017-04-05</Value>
                  <Value AttributeID="atr_ext_var_list_ed_015_ODN">9999-12-31</Value>
                  <Value AttributeID="atr_ext_var_list_max_until">31-dec-9999</Value>
                  <Value AttributeID="atr_pt_calc" Derived="true">name 3</Value>
               </Values>

            </Product>
         </Products>
      </STEP-ProductInformation>
   </ns0:Message1>
</ns0:Messages>

,输出

    <?xml version="1.0" encoding="UTF-8"?>
<Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
   <Message1>
      <Id>SAP_ERP_MATNR</Id>
      <Timestamp/>
      <UserName/>
      <SourceSystemId/>
      <Products>
         <IdOrigin>SAP_ERP_MATNR</IdOrigin>
         <Id>388061</Id>
         <Name>name 1</Name>
         <Description>name 1</Description>
      </Products>
      <Products>
         <IdOrigin>SAP_ERP_MATNR</IdOrigin>
         <Id>388061</Id>
         <Name>name 1</Name>
         <Description>name 1</Description>
      </Products>
      <Products>
         <IdOrigin>SAP_ERP_MATNR</IdOrigin>
         <Id>388061</Id>
         <Name>name 1</Name>
         <Description>name 1</Description>
      </Products>
   </Message1>
   <Message2/>
   <Message3/>
</Messages>
在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" xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge"  exclude-result-prefixes="java date StringBuffer" version="1.0">

    <xsl:output method="xml" indent="yes"/>
      <xsl:template match="node()|@*">
      <xsl:variable name="valueFromSource" select=".//Product"/>
<Messages>
    <Message1>
    <Id>SAP_ERP_MATNR</Id>
    <Timestamp></Timestamp>
    <UserName></UserName>
    <SourceSystemId></SourceSystemId>
    <xsl:for-each select="$valueFromSource">
    <Products>
        <IdOrigin>SAP_ERP_MATNR</IdOrigin>
        <Id><xsl:value-of select="..//Value[@AttributeID='atr_ext_ar_nr']"/></Id>
        <Name><xsl:value-of select="/ns0:Messages/ns0:Message1/STEP-ProductInformation/Products/Product/Values/Value[@AttributeID='atr_pt_calc']"/></Name>
        <Description><xsl:value-of select="/ns0:Messages/ns0:Message1/STEP-ProductInformation/Products/Product/Values/Value[@AttributeID='atr_pt_calc']"/></Description>
    </Products>
    </xsl:for-each>
    </Message1>
    <Message2>

    </Message2>
    <Message3>

    </Message3>
</Messages>
    </xsl:template>
</xsl:stylesheet>

但我希望得到那个输出

<?xml version="1.0" encoding="UTF-8"?>
<Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
   <Message1>
      <Id>SAP_ERP_MATNR</Id>
      <Timestamp/>
      <UserName/>
      <SourceSystemId/>
      <Products>
         <IdOrigin>SAP_ERP_MATNR</IdOrigin>
         <Id>388061</Id>
         <Name>name 1</Name>
         <Description>name 1</Description>
      </Products>
      <Products>
         <IdOrigin>SAP_ERP_MATNR</IdOrigin>
         <Id>15</Id>
         <Name>name 2</Name>
         <Description>name 2</Description>
      </Products>
      <Products>
         <IdOrigin>SAP_ERP_MATNR</IdOrigin>
         <Id>1569</Id>
         <Name>name 3</Name>
         <Description>name 3</Description>
      </Products>
   </Message1>
   <Message2/>
   <Message3/>
</Messages>

名称2而不是名称1,名称3而不是名称1。 15而不是388061,1569而不是388061。

我应该在XSL中更改以获得正确的结果? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

xsl:value-of中的两个表达式以/ns0:Messages/开头,这意味着他们将从文档节点开始选择,最终将选择它在文档中找到的第一个值,无论您在何处地定位。

xsl:for-each更改为此,其中包含相对于当前Product节点的表达式。

<xsl:for-each select="$valueFromSource">
    <Products>
        <IdOrigin>SAP_ERP_MATNR</IdOrigin>
        <Id><xsl:value-of select="Values/Value[@AttributeID='atr_ext_ar_nr']"/></Id>
        <Name><xsl:value-of select="Values/Value[@AttributeID='atr_pt_calc']"/></Name>
        <Description><xsl:value-of select="Values/Value[@AttributeID='atr_pt_calc']"/></Description>
    </Products>
</xsl:for-each>