属性値と xml ドキュメント内の位置に基づいて、出力ドキュメント内の特定の場所に xml ノードを追加する必要があります。
以下の例では、product = "111" が表示されるたびに、product = "222" のサブアイテムに対応するパス属性を取得する必要があります。注意点は、ドキュメント内の相対的な位置が一致している必要があるということです。最初の製品 = "111" の場合、最初の製品 = "222" のパスが必要になります。2 番目の「111」については、2 番目の製品「222」のパスが必要になります。
必要な出力 (現在の製品のパスも追加しますが、問題はありません):
<output>
<product_out id="111">
<path>b</path>
</product_out>
<product_out id="111">
<path>g</path>
</product_out>
<product_out id="111">
<path>i</path>
</product_out>
</output>
私のxml文書。
<?xml version="1.0"?>
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<item product="111">
<sub_item path="a" />
</item>
<item product="222">
<sub_item path="b" />
</item>
<item product="333">
<sub_item path="c" />
</item>
<item product="111">
<sub_item path="d" />
</item>
<item product="111">
<sub_item path="e" />
</item>
<item product="555">
<sub_item path="f" />
</item>
<item product="222">
<sub_item path="g" />
</item>
<item product="555">
<sub_item path="h" />
</item>
<item product="222">
<sub_item path="i" />
</item>
</order>
私のxslドキュメント:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<output>
<xsl:apply-templates />
</output>
</xsl:template>
<xsl:template match="order/item">
<product_out>
<xsl:attribute name="id"><xsl:value-of select="@product"/></xsl:attribute>
<xsl:if test="@product = 111">
<the_correct_sibling_position><xsl:value-of select="1+count(preceding-sibling::item[@product='111'])"/></the_correct_sibling_position>
<path_test1><xsl:value-of select="/order/item[position()=4]/sub_item/@path"/></path_test1>
<path_test2><xsl:value-of select="/order/item[2]/sub_item/@path"/></path_test2>
<path_test3><xsl:value-of select="/order/item[position()=number(1+count(preceding-sibling::item[@product='111']))]/sub_item/@path"/></path_test3>
<path_test4><xsl:value-of select="/order/item[position()=1+count(preceding-sibling::item[@product='111'])]/sub_item/@path"/></path_test4>
</xsl:if>
<path><xsl:value-of select="sub_item/@path[1]"/></path>
</product_out>
</xsl:template>
</xsl:stylesheet>
使用して必要な正しい配列位置を取得できます
1+count(前の兄弟::item[@product='111'])
<the_correct_sibling_position>ノード内の値を出力してテストしました。
ノードの位置をハードコーディングすることもできます:
path_test1 always returns d
path_test2 always returns b
次の 2 つのテストでは、期待どおりの値 b、g が出力されません。
path_test3 always returns a
path_test4 always returns a
製品ノード"111"と"222"の位置に基づいて、b、g、および i を返すパスを取得するにはどうすればよいですか? 前の兄弟変数は、select ステートメントのパス内で期待どおりに機能しません。
助けてください!