首页 文章

XSLT 1.0 Tansfomation - 将兄弟姐妹数据移动到特定兄弟姐妹

提问于
浏览
3

我在制作xslt转换时遇到了麻烦,我真的很感激它的一些帮助 . 我花了很多时间在XPath和XQuery中使用各种方法 . 另外,我被限制为xslt 1.0 .

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

Original XML File - 下面是原始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>

Transformed XML File - 以下是我想要实现的转换后的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>

What I have Tried So far - 说实话,我对这个问题的成功非常有限 . 我最接近它的是以下 . 但是,它与我的预期结果相差甚远,'matches'是XLST 2.0函数,我仅限于版本1 .

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

如果有人能帮我解决这个问题,或者让我朝着正确的方向努力,我会非常感激 .

-干杯 .

2 回答

  • 2

    A solution similar to that of @lwburk, but simpler -- no <xsl:if> 且没有 <xsl:variable>

    <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>
    

    when this transformation is applied on the provided XML document

    <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>
    

    the wanted, correct result is produced

    <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>
    

    Explanation :适当使用和覆盖 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>
    

    Notes and explanation:

    • 标识转换将大多数节点复制为未更改

    • 折扣在处理时会复制到非折扣元素中

    • 折扣 orderDetails 被忽略

相关问题