生成有效 XHTML 的 XSLT 转换:命名空间问题 [英] XSLT transformation to produce valid XHTML: a namespace issue

查看:23
本文介绍了生成有效 XHTML 的 XSLT 转换:命名空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过这个资源链接文本,但我仍然无法使用 MSXML 生成有效的 XHTML 文档解析器.

I have seen this resource link text but I still have troubles generating a valid XHTML document using MSXML parser.

我输入了以下文件:

    <?xml version="1.0" encoding="UTF-8" ?> 
- <html xml:lang="it" xmlns="http://www.w3.org/1999/xhtml">
- <head xmlns="">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
   <title>Bla bla bla</title> 
  <link rel="stylesheet" type="text/css" media="screen" href="css/bla.css" /> 
  </head>
- <body xmlns="" style="background-color:#DFDFDF;left-margin:0;margin-top:0">
    ....
    </body>
  </html>

产生讨厌的 xmlns="" 因为我插入了指令:

where the nasty xmlns="" are produced because i have inserted the instruction:

parserInstance.documentElement.setAttribute "xmlns", "http://www.w3.org/1999/xhtml"

在转换前创建输入文档时.

when creating the input document before transformation.

XSLT 导入我用于捕获 html 元素的转换

The XSLT imports the transformation I use for catching the html elements

<xsl:stylesheet 
    version="1.0" 
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="xhtml xsl"
>
<xsl:import href="_lib.xsl"/>
<xsl:import href="_standard.xsl"/>

在 _standard.xsl 中,我有进行相关转换的代码:

and within _standard.xsl I have the code that make the rerlevant transformation:

<xsl:template match="html">
<xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates select="head" />       
    <xsl:apply-templates select="body" />       
</xsl:copy>

我在 html 标记中仍然没有所需的 xmlns:

Still I do not have the required xmlns in the html markup:

<html xml:lang="it">

而且我仍然在 html 代码中散布着令人讨厌的 xmlns:

AND I still have the nasty xmlns intersparsed in html code:

<h1 xmlns="http://www.w3.org/1999/xhtml">BLA BLA</h1>

我哪里出错了?

请注意:我必须使用拉模型进行转换,我不能使用身份转换

Please, NOTE: i MUST use a pull model for transformation, I cannot use an identity transform

推荐答案

使用 DOM 模型(MSXML 使用),元素或属性节点的命名空间在创建时就确定了,之后您无法更改它并尝试像您通过设置名称为xmlns"的属性来更改名称空间不会更改节点的名称空间.

With the DOM model (MSXML uses) the namespace of an element or attribute node is determined when it is created, you can't change it afterwards and attempts like yours to change namespaces by setting an attribute with name "xmlns" do not change the namespace of nodes.

所以听起来好像您有一个输入文档,其中的元素没有名称空间,并且想要使用 XSLT 将其转换为包含 XHTML 名称空间中的元素的文档.在这种情况下,您不能使用 xsl:copy 那样在没有命名空间的情况下获得结果元素.相反,您需要例如

So it sounds as if you have an input document with elements in no namespace and want to use XSLT to transform it into one with elements in the XHTML namespace. In that case you can't use xsl:copy as that way you get result elements in no namespace. Instead you need e.g.

<xsl:template match="html">
  <html xmlns="http://www.w3.org/1999/xhtml">
    ...
  </html>
</xsl:template>

或者当然,如果您想要所有结果元素的命名空间,您可以将 xmlns="http://www.w3.org/1999/xhtml" 放在样式表或样式表模块的 xsl:stylesheet 元素上您想要创建 XHTML 元素.

or of course as you want that namespace for all result elements you could put xmlns="http://www.w3.org/1999/xhtml" on the xsl:stylesheet element of the stylesheet or stylesheet module(s) that you want to create XHTML elements.

这篇关于生成有效 XHTML 的 XSLT 转换:命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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