使用如下所示的简单XML
<value>
<num>
<accession>111</accession>
<sequence>AAA</sequence>
<score>4000</score>
</num>
</value>我想知道是否可以从先前存储在变量中的节点访问特定节点。XSLT代码非常简短,可以更好地解释我想说的内容
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value/num">
<xsl:variable name="node">
<xsl:copy-of select="current()"/>
</xsl:variable>
<root>
<xsl:copy-of select="$node"/>
</root>
</xsl:template>
</xsl:stylesheet>因此,我将节点存储在变量" node“中。然后,我可以使用$node打印节点的内容。
(编辑) XML输出
<root>
<num>
<accession>111</accession>
<sequence>AAA</sequence>
<score>4000</score>
</num>
</root>我想要做的是打印子节点的内容,如下所示
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value/num">
<xsl:variable name="node">
<xsl:copy-of select="current()"/>
</xsl:variable>
<root>
<xsl:copy-of select="$node/accession"/>
</root>
</xsl:template>
</xsl:stylesheet>但它给出一个错误(组件返回故障代码: 0x80600008 nsIXSLTProcessor.transformToFragment) (检查here)
(编辑)我想要的XML是
<root>
<accession>111</accession>
</root>注意:问题不在于如何获得此输出。问题是,如何使用所提供的XSLT中的变量来获得此输出。
(EDIT:SOLVED)实际上这是可能的,但正如注释中所指出的,如果需要节点集,则变量的值必须使用"select“属性赋值。所以这段代码无法工作,因为变量有一个树片段,而不是存储在其中的节点集(阅读更多信息here)
谢谢!
发布于 2011-12-19 23:52:36
试试这个:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value">
<root>
<xsl:for-each select="num">
<xsl:variable name="node" select="current()" />
<xsl:copy-of select="$node/accession" />
</xsl:for-each>
</root>
</xsl:template>
</xsl:transform>注意,我使用的是xsl:transform,而不是xsl:stylesheet。此外,如果您有兼容的处理器,请考虑使用2.0版而不是1.0版,它添加了许多有用的功能。
不过,我仍然不认为您需要一个变量。
https://stackoverflow.com/questions/8563327
复制相似问题