I have some xml structure like that:
<Work>
<Good id ="1">
<outputList>
<outputRow id = "111" pid = "1" pos ="null" desc="List 1"/>
<outputRow id = "112" pid = "111" pos ="null" desc="Category 1"/>
<outputRow id = "113" pid = "112" pos ="1.1" desc="Position 1.1"/>
<outputRow id = "114" pid = "113" pos ="1.1.1" desc="Position 1.1.1"/>
</outputList>
</Good>
<Good id ="2">
<outputList>
<outputRow id = "111" pid = "1" pos ="null" desc="List 1"/>
<outputRow id = "112" pid = "111" pos ="null" desc="Category 1"/>
<outputRow id = "113" pid = "112" pos ="1.1" desc="Position 1.1"/>
<outputRow id = "114" pid = "113" pos ="1.1.1" desc="Position 1.1.1"/>
<outputRow id = "211" pid = "1" pos ="null" desc="List 2"/>
<outputRow id = "212" pid = "211" pos ="null" desc="Category 3"/>
<outputRow id = "213" pid = "212" pos ="3.1" desc="Position 3.1"/>
<outputRow id = "214" pid = "213" pos ="3.1.1" desc="Position 3.1.1"/>
</outputList>
</Good>
<Work>
I want output like that: For each good, for each list in each listOutput for this good I want to see a tree of the list positions. For the example above I wanna see:
Good (1) List 1. Category 1. Position 1.1 Position 1.1.1
Good (2) List 1. Category 1. Position 1.1 Position 1.1.1
List 2. Category 3. Position 3.1 Position 3.1.1
I use xsl:key to get child elements by parentId:
<xsl:key name="elementsByPid" match="ns1:outputRow[@pid]" use="@pid" />
This key function is using in recursion to create a tree of list categories and positions:
<xsl:template match="/ns1:Work/ns1:Good">
<w:p wsp:rsidR="007E1332" wsp:rsidRDefault="007E1332" wsp:rsidP="007E1332">
<w:pPr>
<w:spacing w:before="40" />
</w:pPr>
<w:r wsp:rsidRPr="00557C88">
<w:rPr>
<w:b-cs />
<w:i-cs />
<w:lang w:val="EN-US" />
</w:rPr>
<w:t>
<xsl:text>Good (</xsl:text>
<xsl:value-of select="position()"></xsl:value-of>
<xsl:text>) </xsl:text>
</w:t>
</w:r>
</w:p>
<xsl:apply-templates select="./ns1:outputList" mode="table" />
</xsl:template>
<xsl:template match="/ns1:Work/ns1:Good/ns1:outputList" mode="table">
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="./ns1:outputRow[@pid=1]" mode="row" />
</xsl:template>
<xsl:template match="/ns1:Work/ns1:Good/ns1:outputList/ns1:outputRow" mode="row">
<w:p wsp:rsidR="007E1332" wsp:rsidRDefault="007E1332" wsp:rsidP="007E1332">
<w:pPr>
<w:spacing w:before="40" />
</w:pPr>
<w:r wsp:rsidRPr="00557C88">
<w:rPr>
<w:b-cs />
<w:i-cs />
<w:lang w:val="EN-US" />
</w:rPr>
<w:t>
<xsl:text>Пункт:</xsl:text>
<xsl:value-of select="./@pos"/>
<xsl:text>Описание: </xsl:text>
<xsl:value-of select="./@desc"/>
</w:t>
</w:r>
</w:p>
<xsl:apply-templates select="key('elementsByPid', ./@id)" mode="row"/>
</xsl:template>
The problem is that key match pattern seem to search for @pid in all outputRows, but I need to make it search only in outputRows for current good from context. Is it possible to achieve this anyway?
You need to create a key which concatenates the id of the good with the pid of the outputRow:
<xsl:key name="elementsByPid" match="ns1:outputRow[@pid]" use="concat(ancestor::ns1:Good/@id, '|', @pid)" />
Then use key('elementsByPid', concat(ancestor::ns1:Good/@id, '|', @pid))
.