我有两个 xsl 文件,每个文件用于不同类型的机器
- 适用于各种 UNIX flavor 机
- 适用于各种 Windows 机器
我想根据检查这些 xsl 的 osname 来加载另一个 xsl
eg: if osname="Windows" then load windows.xsl else load nix.xsl
因此,为了做到这一点,应该有另一个 xsl 来执行检查。那么现在我如何根据 osname 检查加载这些 windows 和 nix xsl?
有关更多详细信息,我提供了适用于 win 和 nix 机器的 xml
nix 机器 xml
<machine> <system> <osname>Linux</osname> <username>Abhishek</username> </system> </machine>
赢得机器xml
<machine> <system> <osinfo> <osinfo field='OS Name' information='Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition ' /> <osinfo field='OS Version' information='5.2.3790 Service Pack 2 Build 3790 ' /> <osinfo field='OS Manufacturer' information='Microsoft Corporation ' /><br/> </osinfo> <username>Matt</username> </system> </machine>
我还提供模板,
unix xsl template
<xsl:template name="unixsystem" match="machine">
<span style="color:#328aa4"><a name="_systeminfo" href="#_top">System Info</a></span>
<table border="1" >
<tbody>
<tr>
<th align="left">OS Name</th>
<th align="left">User Name</th>
</tr>
<tr>
<td><xsl:value-of select="system/osname"/></td>
<td><xsl:value-of select="system/username"/></td>
</tr>
</tbody>
</table>
</span>
</xsl:template>
Windows xsl template
<xsl:template name="winsystem" match="machine">
<span style="color:#328aa4"><a name="_ospatches" href="#_top">OS Information: </a></span></h2>
<table border="1">
<tbody>
<tr>
<th>Field</th>
<th>Information</th>
</tr>
<xsl:for-each select="osinfo/osinfo">
<tr>
<td valign="top" ><xsl:value-of select="@field"/></td>
<td valign="top" style="width: 2;"><xsl:value-of select="@information"/></td>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
而不是使用 contains
这可以吗?
<xsl:choose>
<xsl:when test="osinfo/osinfo/@information='Windows'">
<xsl:call-template name="winsystem"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="unixsystem"/>
</xsl:otherwise>
</xsl:choose>
最佳答案
因为 xsl:include
或 xsl:import
必须是 xsl:stylesheet
的子级,因此您将无法有条件地管理它们。
如果您将两个样式表都包含在主样式表中,然后根据您的选择调用适当的模板,可能会更好。
示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="win.xsl"/>
<xsl:include href="unix.xsl"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="contains(//osinfo/@information, 'Windows')">
<xsl:call-template name="winsystem"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="unixsystem"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
显然这只是一个建议,它基于在名为 win.xsl 和 unix.xsl 的两个不同样式表上有两个名为 win 和 unix 的模板。
希望这有帮助
看到模板后的扩展答案
我会以不同的方式命名这两个模板并删除match
。我的意思是我会使用:
unix xsl
<xsl:template name="unixsystem">
<!-- your staff -->
</xsl:template>
Windows xsl
<xsl:template name="winsystem">
<!-- your staff -->
</xsl:template>
之后您可以按照上面的指示调用模板。请注意,我已删除match
。这样你应该就可以调用模板了。我没有调查你的 xsl 文件的内容是否正确。我还更正了 xsl 中的错误代码(未封闭的 xsl:choose
)。
关于xml - 如何编辑 xsl 文件,以便它根据某些检查加载不同的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5987861/