Xslt xsl:key从上下文匹配当前节点
我有这样的xml结构:Xslt xsl:key从上下文匹配当前节点,xslt,xslt-1.0,xslkey,Xslt,Xslt 1.0,Xslkey,我有这样的xml结构: <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"
<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>
我想要这样的输出:
对于每个商品,对于每个列表中的每个列表输出对于这个商品,我希望看到列表位置的树。
对于上面的例子,我想看看:
好(1)
清单1。
第一类。
立场1.1
位置1.1.1
好(2)
清单1。
第一类。
立场1.1
位置1.1.1
清单2。
第3类。
立场3.1
位置3.1.1
我使用xsl:key按parentId获取子元素:
<xsl:key name="elementsByPid" match="ns1:outputRow[@pid]" use="@pid" />
该键函数在递归中用于创建列表类别和位置的树:
<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>
好(
)
Пункт:
Описание:
问题是键匹配模式似乎在所有outputRows中搜索@pid,但我需要让它只在outputRows中搜索上下文中的当前商品。无论如何,是否有可能实现这一点?您需要创建一个键,将商品的id与输出流的pid连接起来:
<xsl:key name="elementsByPid" match="ns1:outputRow[@pid]" use="concat(ancestor::ns1:Good/@id, '|', @pid)" />
然后使用键('elementsByPid',concat(祖先::ns1:Good/@id,|',@pid))