2017-08-28 4 views
0

以下の入力xmlに基づいて、受注明細が親レベルで「デスクトップ」タイプの場合、すべての子オーダーアイテムの価格を親価格と同じに更新します。 orderitemは再帰的で、内部に1-n個のorderitemを持つことができます。私は3人の子供と1人の親のオーダーアイテムを考えました。 私は依存関係を含む別の質問があります。つまり、親が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

おかげでマイケルは、あなたは親切に私はこのてください理解するのに役立ちます。私が理解しているのは、「デスクトップ」という名前の2つの世代を上位にして、親レベルの価格を選択して価格をコピーする価格要素と一致しているということです。私はそれがどのように動作するか少し混乱しています。ありがとう。 – Krish

+0

祖父母の名前が「デスクトップ」である「価格」と一致し、次に祖父母から価格をコピーしています。これは、親の名前が「デスクトップ」である「Orderitem」の価格を変更するのと同じことです。私たちは価格を変更したいだけなので、スマートなことは親ではなく直接それにマッチさせることです。 –

+0

それで、それは子供の要素に価格を適用する(そして別の注文項目の価格ではない)。それは暗黙のことですか?それは必然的に祖父母ではなく、親でもあります。 – Krish

関連する問題