Xml xsl选择具有子值的节点并将其作为参数传递
我在访问我想要的节点时遇到了一些问题 以下是我的简化xml:Xml xsl选择具有子值的节点并将其作为参数传递,xml,xslt,Xml,Xslt,我在访问我想要的节点时遇到了一些问题 以下是我的简化xml: <chain> <components> <component> <place>2</place> <name>bbb</name> </component> <component> <place
<chain>
<components>
<component>
<place>2</place>
<name>bbb</name>
</component>
<component>
<place>1</place>
<name>aaa</name>
</component>
<component>
<place>3</place>
<name>ccc</name>
</component>
</components>
</chain>
还有我的xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:template match="text()"/>
<xsl:template name="writeComponent">
<xsl:param name="my_component"/>
<xsl:value-of select="$my_component/name"/>
</xsl:template>
<xsl:template match="/chain/components">
<xsl:for-each select="component">
<xsl:variable name="nextPlace" select="./place + 1"/>
<xsl:call-template name="writeComponent">
<xsl:with-param name="my_component" select='component'/>
</xsl:call-template>
is connected to
<xsl:call-template name="writeComponent">
<xsl:with-param name="my_component" select="component[place = '$nextPlace']"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
连接到
我需要在参数中使用不同的节点调用同一个模板
谢谢你的帮助,谢谢
编辑:我找到了解决问题的方法
电话:
<xsl:call-template name="writeComponent">
<xsl:with-param name="component_param" select="."/>
</xsl:call-template>
is connected to
<xsl:call-template name="writeComponent">
<xsl:with-param name="component_param" select="/chain/components/component[place = $nextPlace]"/>
</xsl:call-template>
连接到
在被调用的模板中,您需要在
select=“component[place='$nextPlace']
中循环一个,您需要select=“component[place=$nextPlace]
来正确引用变量。但是由于每个
的处理组件
元素,我看不出
如何在示例XML中选择任何内容。模板匹配的整个组合,对于每个模板和调用模板似乎都有问题,您可能希望向我们展示您所拥有的输入样本的结果,然后我们可以建议一种更干净、更简单的XSLT方法。您能添加您预期的输出吗?问题不太清楚,因为我认为,当它已经在组件上循环时,只有一个调用模板就足够了。我添加了所需的输出,我的问题是我不能将节点作为参数传递。你能解释请求转换背后的逻辑吗?我不知道aaa到底是如何连接到bbb
,除了两个是兄弟姐妹(但是为什么不aaa也连接到ccc
?)之外。我使用xml来描述这样的组件链:[aaa->bbb->ccc]。顺便说一句,我的输出是一个文本文件:我已经找到了解决方案(请参见我编辑的问题),我添加了所需的输出,我的问题是我无法将节点作为参数传递
<xsl:call-template name="writeComponent">
<xsl:with-param name="component_param" select="."/>
</xsl:call-template>
is connected to
<xsl:call-template name="writeComponent">
<xsl:with-param name="component_param" select="/chain/components/component[place = $nextPlace]"/>
</xsl:call-template>