XSLT:使用节点集会导致在第二次传递时选择节点时出现问题
我正在尝试使用XSLT进行多重传递。因此,首先我声明一个带有第一次传递结果的变量,然后为第二次传递应用模板,如下面的代码所示。在这种情况下,第二次传递并不像我预期的那样有效 作为一个简单的例子,我想为第一遍中的每个XSLT:使用节点集会导致在第二次传递时选择节点时出现问题,xslt,Xslt,我正在尝试使用XSLT进行多重传递。因此,首先我声明一个带有第一次传递结果的变量,然后为第二次传递应用模板,如下面的代码所示。在这种情况下,第二次传递并不像我预期的那样有效 作为一个简单的例子,我想为第一遍中的每个添加一个标记。在本例中,输入XML可以是任何内容 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr
添加一个
标记。在本例中,输入XML可以是任何内容
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*" mode="secondPass">
<xsl:for-each select="Node">
<NewNode/>
</xsl:for-each>
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="firstPass">
<Data>
<Node/>
<Node/>
</Data>
</xsl:variable>
<xsl:apply-templates mode="secondPass"
select="exslt:node-set($firstPass)/*"/>
</xsl:template>
</xsl:stylesheet>
在这种情况下,for-each不起作用,就像xml中没有
这样的东西一样。其他选择也找不到相应的节点,for each只是一个示例。我尝试使用另一个匹配“Node”而不是“Node()|@”的模板,但我们从未像缺少标记一样输入它:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Node" mode="secondPass">
<NewNode/>
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*" mode="secondPass">
<xsl:copy>
<xsl:apply-templates select="Node" mode="secondPass"/>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="firstPass">
<Data>
<Node/>
<Node/>
</Data>
</xsl:variable>
<xsl:apply-templates mode="secondPass"
select="exslt:node-set($firstPass)/*"/>
</xsl:template>
</xsl:stylesheet>
这两种代码的结果是:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Node />
<Node />
</Data>
当我期待:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<NewNode />
<NewNode />
<Node />
<Node />
</Data>
及
作为输入,您可以使用:
<?xml version="1.0" encoding="utf-8"?>
<Body>
</Body>
当你说你的代码不工作时,你没有解释你想要的结果和得到的结果,但是你的方法中的一个问题是你的样式表有
xmlns=”http://www.w3.org/1999/xhtml“
这样就可以将结果元素(包括第一次传递的元素)放入该命名空间,但是,您的匹配模式或路径表达式没有考虑名称空间。我在你的代码片段中没有看到任何XHTML,所以我想知道你为什么使用这个名称空间。如果您的实际元素名是XHTML元素的名称,这意味着您需要该名称空间,那么您需要确保您的模板在XHTML名称空间上与例如
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:exslt="http://exslt.org/common"
exclude-result-prefixes="exslt xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="xhtml:Node" mode="secondPass">
<NewNode/>
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*" mode="secondPass">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="firstPass">
<Data>
<Node/>
<Node/>
</Data>
</xsl:variable>
<xsl:apply-templates mode="secondPass"
select="exslt:node-set($firstPass)/*"/>
</xsl:template>
</xsl:stylesheet>
另一种选择是,如果临时元素不应该是XHTML元素,并且希望在不使用前缀的情况下选择或匹配它们,则需要使用
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Node" mode="secondPass">
<NewNode/>
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*" mode="secondPass">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="firstPass" xmlns="">
<Data>
<Node/>
<Node/>
</Data>
</xsl:variable>
<xsl:apply-templates mode="secondPass"
select="exslt:node-set($firstPass)/*"/>
</xsl:template>
</xsl:stylesheet>
当然,如果您的所有元素都不是XHTML元素,那么我只需删除
xmlns=”http://www.w3.org/1999/xhtml样式表中的“
。”不能很好地描述问题。请发布一个完整的示例(可以通过复制和粘贴复制),并显示预期的输出。为什么有xmlns=”http://www.w3.org/1999/xhtml“
在样式表中,如果您想要的结果不使用任何名称空间?谢谢,就是这样,当我删除名称空间时,它开始工作了!
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Node" mode="secondPass">
<NewNode/>
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*" mode="secondPass">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="secondPass"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="firstPass" xmlns="">
<Data>
<Node/>
<Node/>
</Data>
</xsl:variable>
<xsl:apply-templates mode="secondPass"
select="exslt:node-set($firstPass)/*"/>
</xsl:template>
</xsl:stylesheet>