如何将 XMLNS 属性从 XSL 模板自动传播到另一个模板 [英] How to automatically propagate the XMLNS attribute from on XSL template to another

查看:25
本文介绍了如何将 XMLNS 属性从 XSL 模板自动传播到另一个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法让 XMLNS 属性从一个 XSL 模板自动传播到另一个模板,这样我就不必每次都重新声明它.

I'm looking for a way to have the XMLNS attribute propagate automatically from one XSL Template to another so I don't have to redeclare it each time.

这是我的情况.我正在尝试使用 XSLT 文件将 XML 文件转换为 XHTML 文件.我的 XML 文件是:

Here's my situation. I'm trying to transform an XML file into an XHTML file using a XSLT file. My XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" encoding="UTF-8" href="car.xslt" version="1.0"?>
<vehicle>
  <car>
    <make>Honda</make>
    <color>blue</color>
  </car>
  <car>
    <make>Saab</make>
    <color>red</color>
  </car>
</vehicle>

我的 XSLT 文件是:

My XSLT file is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
  <table cellpadding="5" border="1">
    <tr><td>make</td><td>color</td></tr>
        <xsl:apply-templates/>
  </table>
</body>
</html>
</xsl:template>

<xsl:template match="car">
    <tr xmlns="http://www.w3.org/1999/xhtml">
      <td><xsl:value-of select="make"/></td><td><xsl:value-of select="color"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

在 XSLT 文件中,我只想说:

In the XSLT file, I would like to just be able to say:

<xsl:template match="car">
    <tr>

无需重新声明 XMLNS.有什么建议吗?

without having to redeclare the XMLNS. Any suggestions?

推荐答案

您可以使 XHTML 成为样式表中的默认命名空间;然后它适用于每个模板:

You can make XHTML the default namespace in the stylesheet; it then applies to every template:

<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" 
  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

这篇关于如何将 XMLNS 属性从 XSL 模板自动传播到另一个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆