XSLT 1.0转换 - 将兄弟节点数据移动到特定的兄弟节点

3

我遇到了一些困难,需要进行XSLT转换。我已经尝试了各种XPath和XQuery的方法,并且受限于XSLT 1.0版本。这个转换涉及对XML订单文件中的产品项目进行更改。原始XML文件有Items,但是某些行项目是折扣券引用(见下面的dsc-102和dsc-133)。我需要实现的是删除折扣券引用的'orderDetails'节点,并将其包含信息添加到相应的兄弟产品项目中(请参见下面的转换后的XML示例)。每个折扣券引用在其产品名称末尾指定了其对应的产品项目(例如….[glv-001][glv-003])。

原始XML文件 - 以下是包含1个订单、3个产品项目和2个折扣券引用的原始XML文件。折扣引用'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>

我尝试过的方法 - 说实话,我在解决这个问题上取得了相当有限的成功。我所能做到的最接近结果的方法如下。然而,它距离我的预期结果还有很长一段路要走,“matches”是一个XLST 2.0函数,而我被限制使用版本1。

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

如果有人能帮我解决这个问题或者指导我正确的方向,我会非常感激。
-谢谢。
2个回答

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 会被忽略

2

与 @lwburk 相似的解决方案,但更简单 -- 不使用 <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>

当应用此转换到提供的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>

解释:适当使用和覆盖身份规则,以及模板/模式匹配。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接