xml - 如何编辑 xsl 文件,以便它根据某些检查加载不同的模板?

标签 xml xslt xsl-fo

我有两个 xsl 文件,每个文件用于不同类型的机器

  1. 适用于各种 UNIX flavor 机
  2. 适用于各种 Windows 机器

我想根据检查这些 xsl 的 osname 来加载另一个 xsl

eg: if osname="Windows" then load windows.xsl else load nix.xsl

因此,为了做到这一点,应该有另一个 xsl 来执行检查。那么现在我如何根据 osname 检查加载这些 windowsnix xsl?

有关更多详细信息,我提供了适用于 win 和 nix 机器的 xml

  1. nix 机器 xml

    <machine> <system> <osname>Linux</osname> <username>Abhishek</username> </system> </machine>

  2. 赢得机器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:includexsl: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/

相关文章:

php - 如何使用匹配节点连接两个 XML 文件

xml - GZIP Deflate 与 CURL(PHP) Web 服务问题

xml - 如何使用 XSLT 1.0 查找 XML 节点的存在

javascript - 从 xml 文件获取一些信息的 xslt 和 xpath 在 JavaScript 中不起作用

xslt - 如何在 xsl 中将文本和表格数据对齐到中心

java - 在 Java 中使用 xalan 搜索 XML 文件

java - 第一个官方 Android 教程崩溃

xml - 在 XSLT 中创建父/子层次结构的电子表格 XML

java - 处理 xslt 包含中的非法 URI 字符

xsl-fo - 当表格跨越多页时,如何让 XSL-FO 表格标题在每一页上重复?