【问题标题】:XSLT 1.0 Tansfomation - Moving Sibling data to Specific SiblingsXSLT 1.0 转换 - 将同级数据移动到特定同级
【发布时间】:2012-02-03 18:54:07
【问题描述】:

我在进行 xslt 转换时遇到了麻烦,我非常感谢您提供一些帮助。我花了很多时间在 XPath 和 XQuery 中使用各种方法。另外,我仅限于 xslt 1.0。

转换涉及对 xml 订单文件中的产品项目进行更改。原始 XML 文件包含项目,但某些行项目是折扣券参考(请参阅下面的 dsc-102 和 dsc-133)。我需要实现的是删除折扣优惠券引用的“orderDetails”节点,并将包含信息添加到它们相应的同级产品项目中(请参阅下面的转换后的 XML 示例)。每个折扣券参考在其产品名称的末尾指定其对应的产品项目(例如….[glv-001][glv-003])。

原始 XML 文件 - 以下是原始 XML 文件,其中包含 1 个订单、3 个产品项目和 2 个折扣券参考。折扣参考“dsc-102”对应于 2 个产品项目“glv-001”和“glv-003”。折扣参考“dsc-133”对应于 1 个产品项目“sho-123”。

<xmldata>
<Order>
    <orderID>1010</orderID>
    <custFirstName>Jim</custFirstName>
    <custLastName>Jones</custLastName>
    <orderDetails>
        <productCode>sho-123</productCode>
        <productName>Leather Windsor Shoes - size 10</productName>
    </orderDetails>
    <orderDetails>
        <productCode>glv-001</productCode>
        <productName>Leather gloves - size Small</productName>
    </orderDetails>
    <orderDetails>
        <productCode>glv-003</productCode>
        <productName>Leather gloves - size XLarge</productName>
    </orderDetails>
    <orderDetails>
        <productCode>dsc-102</productCode>
        <productName>10% Discount for Leather Gloves [glv-001][glv-003]</productName>
    </orderDetails>
    <orderDetails>
        <productCode>dsc-133</productCode>
        <productName>Free Shipping for Windsor Shoes [sho-123]</productName>
    </orderDetails>
</Order>

转换后的 XML 文件 - 下面是我想要实现的转换后的 XML。转移已删除两个折扣优惠券引用,并在其对应的同级产品项目中添加了一个“discountCoupon”节点。

<xmldata>
<Orders>
    <orderID>1010</orderID>
    <custFirstName>Jim</custFirstName>
    <custLastName>Jones</custLastName>
    <orderDetails>
        <productCode>sho-123</productCode>
        <productName>Leather Windsor Shoes - size 10</productName>
        <discountCoupon>Free Shipping for Windsor Shoes</discountCoupon>
    </orderDetails>
    <orderDetails>
        <productCode>glv-001</productCode>
        <productName>Leather gloves - size Small</productName>
        <discountCoupon>10% Discount for Leather Gloves</discountCoupon>
    </orderDetails>
    <orderDetails>
        <productCode>glv-003</productCode>
        <productName>Leather gloves - size XLarge</productName>
        <discountCoupon>10% Discount for Leather Gloves</discountCoupon>
    </orderDetails>
</Orders>

到目前为止我所做的尝试 - 老实说,我在这个问题上取得的成功非常有限。我与它最接近的是以下内容。但是,这与我的预期结果相差甚远,“匹配”是 XLST 2.0 函数,我仅限于版本 1。

<xsl:if test="../OrderDetails[ProductCode = 'DSC-15'] and matches(ProductCode,'AH010585059',i)">DiscountCoupon</xsl:if>

如果有人能帮我解决这个问题或推动我朝着正确的方向前进,我将不胜感激。

-干杯。

【问题讨论】:

    标签: xml xslt transform xslt-1.0


    【解决方案1】:

    类似于@lwburk 的解决方案,但更简单——没有 &lt;xsl:if&gt;&lt;xsl:variable&gt; 没有:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
      "orderDetails[not(starts-with(productCode, 'dsc-'))]">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
       <xsl:apply-templates mode="coupon" select=
       "../orderDetails[starts-with(productCode, 'dsc-')]
                        [contains(productName,
                               concat('[', current()/productCode, ']')
                              )
                         ]/productName
       "/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="orderDetails[starts-with(productCode, 'dsc-')]"/>
    
     <xsl:template match="productName" mode="coupon">
      <discountCoupon>
       <xsl:value-of select="substring-before(., ' [')"/>
      </discountCoupon>
     </xsl:template>
    </xsl:stylesheet>
    

    当此转换应用于提供的 XML 文档时

    <Order>
        <orderID>1010</orderID>
        <custFirstName>Jim</custFirstName>
        <custLastName>Jones</custLastName>
        <orderDetails>
            <productCode>sho-123</productCode>
            <productName>Leather Windsor Shoes - size 10</productName>
        </orderDetails>
        <orderDetails>
            <productCode>glv-001</productCode>
            <productName>Leather gloves - size Small</productName>
        </orderDetails>
        <orderDetails>
            <productCode>glv-003</productCode>
            <productName>Leather gloves - size XLarge</productName>
        </orderDetails>
        <orderDetails>
            <productCode>dsc-102</productCode>
            <productName>10% Discount for Leather Gloves [glv-001][glv-003]</productName>
        </orderDetails>
        <orderDetails>
            <productCode>dsc-133</productCode>
            <productName>Free Shipping for Windsor Shoes [sho-123]</productName>
        </orderDetails>
    </Order>
    

    产生想要的正确结果

    <Order>
       <orderID>1010</orderID>
       <custFirstName>Jim</custFirstName>
       <custLastName>Jones</custLastName>
       <orderDetails>
          <productCode>sho-123</productCode>
          <productName>Leather Windsor Shoes - size 10</productName>
          <discountCoupon>Free Shipping for Windsor Shoes</discountCoupon>
       </orderDetails>
       <orderDetails>
          <productCode>glv-001</productCode>
          <productName>Leather gloves - size Small</productName>
          <discountCoupon>10% Discount for Leather Gloves</discountCoupon>
       </orderDetails>
       <orderDetails>
          <productCode>glv-003</productCode>
          <productName>Leather gloves - size XLarge</productName>
          <discountCoupon>10% Discount for Leather Gloves</discountCoupon>
       </orderDetails>
    </Order>
    

    说明:正确使用和覆盖 identity rule,以及模板/模式匹配。

    【讨论】:

      【解决方案2】:

      以下样式表产生正确的结果:

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:output indent="yes" omit-xml-declaration="yes"/>
          <xsl:strip-space elements="*"/>
          <xsl:template match="@*|node()">
              <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
          </xsl:template>
          <xsl:template match="orderDetails[not(starts-with(productCode, 'dsc'))]">
              <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
                  <xsl:variable name="discount"
                      select="../orderDetails[starts-with(productCode, 'dsc') and 
                            contains(productName, 
                               concat('[', current()/productCode, ']'))]/productName"/>
                  <xsl:if test="$discount">
                      <discountCoupon>
                          <xsl:value-of select="substring-before($discount, ' [')"/>
                      </discountCoupon>
                  </xsl:if>
              </xsl:copy>
          </xsl:template>
          <xsl:template match="orderDetails[starts-with(productCode, 'dsc')]"/>
      </xsl:stylesheet>
      

      注意事项及说明:

      • 恒等变换通过不变复制大多数节点
      • 折扣在处理时被复制到非折扣元素中
      • 折扣orderDetails 被忽略

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-12
        • 1970-01-01
        • 2016-10-24
        • 2015-05-27
        • 1970-01-01
        • 1970-01-01
        • 2015-03-20
        • 1970-01-01
        相关资源
        最近更新 更多