2017-08-28 29 views
0

根据以下输入xml,如果orderitem在父级别为“desktop”类型,则更新与父级价格相同的所有子订单项目的价格。 orderitem是递归的,可以有1-n个订单项。为了简单起见,我考虑了3个孩子和1个父母orderitem。 我有另一个涉及依赖的问题,即如果父母有一个objectid并且它与孩子的objectid匹配,然后更新价格。但是,一旦解决,我会问一个新的问题。谢谢。XSLT-根据特定元素的值从父项到子项复制值

<listoforders> 
<Orderitem> 
    <name>Desktop</name> 
    <place>NZ</place> 
    <price>120</price> 
    <Orderitem> 
    <name>Desktop2</name> 
    <place>NZ</place> 
    <price>130</price> 
    </Orderitem> 
    <Orderitem> 
    <name>Desktop3</name> 
    <place>NZ</place> 
    <price>130</price> 
    </Orderitem> 
</Orderitem> 
</listoforders> 

结果:

<listoforders> 
    <Orderitem> 
    <name>Desktop</name> 
    <place>NZ</place> 
    <price>120</price> 
    <Orderitem> 
    <name>Desktop2</name> 
    <place>NZ</place> 
    <price>120</price> 
    </Orderitem> 
    <Orderitem> 
    <name>Desktop3</name> 
    <place>NZ</place> 
    <price>120</price> 
    </Orderitem> 
</Orderitem> 
</listoforders>  

很简单,但是我不能够通过身份规则来做到这一点。我需要在这里使用for-each吗?

<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="orderitem[name='Desktop']/price"> 
<xsl:variable name="temp" select="*[(self::price)]"/> 
<xsl:copy> 
    <xsl:value-of select=$temp </xsl:value-of> 
    <xsl:apply-templates select="*[(child::price)]"/> 
</xsl:copy>  
</xsl:template> 
</xsl:stylesheet> 

在此先感谢。

问候 Krish

回答

0

如何:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="price[../../name='Desktop']"> 
    <xsl:copy-of select="../../price"/>   
</xsl:template> 

</xsl:stylesheet> 
+0

感谢迈克尔,你能好心帮助我理解这一点吧。我的理解是,我们与价格元素相匹配,其名称为'桌面'两代以上,然后通过选择父级价格来复制价格。我对它的工作原理感到困惑。谢谢。 – Krish

+0

我们正在匹配'Price',其祖父母的名字是“Desktop”,然后复制祖父母的价格。这与改变其父母名称为“桌面”的“Orderitem”的价格相同。我们只想改变价格,所以要做的就是直接匹配它,而不是它的父母。 –

+0

那么它如何将价格应用于其子元素(而不是另一个orderitem中的价格)。它是隐含的吗?它不一定是祖父母,也可以是父母。 – Krish

相关问题