Xslt 整理子元素
对于type=CHOICE的每个属性元素,我想整理CHOICE元素。因此:Xslt 整理子元素,xslt,Xslt,对于type=CHOICE的每个属性元素,我想整理CHOICE元素。因此: <property> <name>lookup_mode</name> <type>CHOICE</type> <defaultValue>1</defaultValue> <choices> <value>1</value> <value>2<
<property>
<name>lookup_mode</name>
<type>CHOICE</type>
<defaultValue>1</defaultValue>
<choices>
<value>1</value>
<value>2</value>
<value>3</value>
</choices>
<choiceNames>
<value>Challenge</value>
<value>Pass Through</value>
<value>Not Supported</value>
</choiceNames>
</property>
查找模式
选择
1.
1.
2.
3.
挑战
通过
不支持
输出为:
<property>
<name>lookup_mode</name>
<type>CHOICE</type>
<defaultValue>1</defaultValue>
<choices>
<choice>
<index>1</index>
<choiceName>Challenge</choiceName>
</choice>
<choice>
<index>2</index>
<choiceName>Pass Through</choiceName>
</choice>
<choice>
<index>3</index>
<choiceName>Not Supported</choiceName>
</choice>
</choices>
</property>
查找模式
选择
1.
1.
挑战
2.
通过
3.
不支持
我尝试了以下模板:
<xsl:template match="propertyDescriptor[type/text()='CHOICE']">
<xsl:copy>
<choices>
<xsl:for-each select="choices/value">
<choice>
<index><xsl:value-of select="."/></index>
<choiceName><xsl:value-of select="../../choiceNames/value"/></choiceName>
</choice>
</xsl:for-each>
</choices>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
如果我使用该选项作为循环的索引,则会对每个选项重复choiceName值,反之亦然:
<property>
<choices>
<choice>
<index>0</index>
<choiceName>Challenge Pass Through Not Supported</choiceName>
</choice>
<choice>
<index>1</index>
<choiceName>Challenge Pass Through Not Supported</choiceName>
</choice>
<choice>
<index>2</index>
<choiceName>Challenge Pass Through Not Supported</choiceName>
</choice>
</choices>
...
0
不支持挑战传递
1.
不支持挑战传递
2.
不支持挑战传递
...
我将关注XSLT Jumpstarter(2015),作者在第6章中讨论了类似的操作,改变了内容的结构和顺序。不包括元素排序规则(如打印机页面排序规则)。我认为有一些股票模式可以遵循?
提前感谢。您想要的结果可以通过以下方式实现:
<xsl:template match="property[type='CHOICE']">
<xsl:copy>
<xsl:copy-of select="name | type | defaultValue"/>
<choices>
<xsl:for-each select="choices/value">
<xsl:variable name="i" select="position()" />
<choice>
<index>
<xsl:value-of select="."/>
</index>
<choiceName>
<xsl:value-of select="../../choiceNames/value[$i]"/>
</choiceName>
</choice>
</xsl:for-each>
</choices>
</xsl:copy>
</xsl:template>
您的尝试不起作用的(主要)原因是您的指导:
<xsl:value-of select="../../choiceNames/value"/>
选择all
choiceNames/value
节点并返回第一个节点(在XSLT 1.0中)或所有节点(在XSLT 2.0中)的字符串值,而不考虑哪个choices/value
节点是当前节点。请考虑向我们显示一个最小但完整的样式表,以便我们重现问题。然后我们可以修复它。请发布您的完整尝试,以便我们可以修复它,而不必从头开始为您编写代码。请注意,您的输入中没有propertyDescriptor
元素。同意前面的评论:要么缩小到更细粒度的问题,要么给出更多的上下文/代码。这将帮助你得到更好的答案。