Xml XSLT 1.0转换信息-将同级数据移动到特定同级
我在完成xslt转换时遇到了困难,我非常感谢您的帮助。我花了相当多的时间在XPath和XQuery中使用各种方法。此外,我仅限于XSLT1.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”Xml XSLT 1.0转换信息-将同级数据移动到特定同级,xml,xslt,transform,xslt-1.0,Xml,Xslt,Transform,Xslt 1.0,我在完成xslt转换时遇到了困难,我非常感谢您的帮助。我花了相当多的时间在XPath和XQuery中使用各种方法。此外,我仅限于XSLT1.0 转换涉及对xml订单文件中的产品项进行更改。原始XML文件中有项目,但有些行项目是折扣优惠券参考(请参见下面的dsc-102和dsc-133)。我需要实现的是删除折扣优惠券引用的“orderDetails”节点,并将包含的信息添加到相应的同级产品项中(请参见下面转换的XML示例)。每个折扣券参考在其产品名称末尾指定其相应的产品项目(例如,…[glv-00
<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>
1010
吉姆
琼斯
sho-123
皮革温莎鞋-尺寸10
glv-001
皮手套-小尺寸
glv-003
皮手套-尺寸XLarge
dsc-102
皮手套10%折扣[glv-001][glv-003]
dsc-133
温莎鞋免费送货[sho-123]
转换的XML文件-
下面是我想要实现的转换后的XML。转移已删除两个折扣优惠券引用,并将“折扣优惠券”节点添加到其相应的同级产品项中
<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>
1010
吉姆
琼斯
sho-123
皮革温莎鞋-尺寸10
温莎鞋免费送货
glv-001
皮手套-小尺寸
皮手套打九折
glv-003
皮手套-尺寸XLarge
皮手套打九折
到目前为止我所尝试的-
老实说,我在这个问题上取得的成功非常有限。我与之最接近的是以下几点。然而,这与我的预期结果相去甚远,“matches”是一个XLST2.0函数,我仅限于版本1
<xsl:if test="../OrderDetails[ProductCode = 'DSC-15'] and matches(ProductCode,'AH010585059',i)">DiscountCoupon</xsl:if>
折扣券
如果有人能帮我解决这个问题,或者把我推向正确的方向,我会非常感激
-干杯。以下样式表生成正确的结果:
<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
- 与@lwburk类似的解决方案,但更简单——无
和无:
<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>
1010
吉姆
琼斯
sho-123
皮革温莎鞋-尺寸10
glv-001
皮手套-小尺寸
glv-003
皮手套-尺寸XLarge
dsc-102
皮手套10%折扣[glv-001][glv-003]
dsc-133
温莎鞋免费送货[sho-123]
生成所需的正确结果:
<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>
1010
吉姆
琼斯
sho-123
皮革温莎鞋-尺寸10
温莎鞋免费送货
glv-001
皮手套-小尺寸
皮手套打九折
glv-003
皮手套-尺寸XLarge
皮手套打九折
说明:适当使用和覆盖,以及模板/模式匹配