Xml XSLT:在变量内模拟模板匹配
我想知道如何根据下面的代码模拟变量内的模板马赫数。 我希望将此XSLT代码的输出存储在一个变量中,稍后在脚本中使用:Xml XSLT:在变量内模拟模板匹配,xml,xslt,Xml,Xslt,我想知道如何根据下面的代码模拟变量内的模板马赫数。 我希望将此XSLT代码的输出存储在一个变量中,稍后在脚本中使用: 我现在可以通过使用两个XSLT脚本来实现这一点,但我只想使用一个脚本 XSLT脚本应提供如下相同的输出: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <
我现在可以通过使用两个XSLT脚本来实现这一点,但我只想使用一个脚本
XSLT脚本应提供如下相同的输出:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="InputNew">
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:variable>
<xsl:template match="/">
<xsl:copy-of select="$InputNew"/>
</xsl:template>
</xsl:stylesheet>
我知道我不能在变量中使用模板匹配,我只能使用调用模板,但我无法为此编写正确的XSLT代码。
编辑:这是工作脚本:
如果您使用的是Saxon,您应该能够使用
exslt:node-set()
函数执行以下操作:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*" mode="stripNs">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@* | node()" mode="stripNs"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*" mode="stripNs">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<!-- Apply the stripNs templates and capture the result in a variable -->
<xsl:variable name="noNamespaces">
<xsl:apply-templates select="*" mode="stripNs" />
</xsl:variable>
<!-- Now use the variable -->
<xsl:apply-templates select="exslt:node-set($noNamespaces)/*" />
</xsl:template>
</xsl:stylesheet>
您使用的是什么XSLT处理器?在一些XSLT处理器中,可以捕获变量中的XML树,然后使用
node-set()
函数与该变量的内容交互。您好,感谢您的快速反馈!我用的是萨克森。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsl soap xsi">
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match ="*" mode="stripNs">
<xsl:element name ="{local-name()}" >
<xsl:apply-templates select ="@* | node()" mode="stripNs"/>
</xsl:element>
</xsl:template>
<xsl:variable name="noNamespaces">
<xsl:apply-templates select="soap:Envelope/soap:Body/*" mode="stripNs" />
</xsl:variable>
<xsl:template match="/">
<!-- Now I can do what I want with the variable $noNamespaces -->
<xsl:copy-of select="$noNamespaces"/>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*" mode="stripNs">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@* | node()" mode="stripNs"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*" mode="stripNs">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<!-- Apply the stripNs templates and capture the result in a variable -->
<xsl:variable name="noNamespaces">
<xsl:apply-templates select="*" mode="stripNs" />
</xsl:variable>
<!-- Now use the variable -->
<xsl:apply-templates select="exslt:node-set($noNamespaces)/*" />
</xsl:template>
</xsl:stylesheet>
<!-- Now use the variable -->
<xsl:apply-templates select="$noNamespaces/*" />