我在进行 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>
如果有人可以帮助我解决这个问题,或者在正确的方向上插入我,我将不胜感激。
-干杯。
最佳答案
类似于@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>
说明:适当使用和覆盖 identity rule ,以及模板/模式匹配。
关于xml - XSLT 1.0 Tansfomation - 将兄弟数据移动到特定的兄弟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8751145/