xml – 在XSLT之后出现未使用的命名空间声明
在搜索网络寻找答案后,提出“几乎”解决方案……我决定将问题简化为一个非常简单的案例.
请考虑以下XML代码段: <me:root xmlns:me="https://stackoverflow.com/xml" xmlns="http://www.w3.org/1999/xhtml"> <me:element> <p>Some HTML code here.</p> </me:element> </me:root> 请注意,p元素是XHTML的命名空间,这是此doc的默认命名空间. 现在考虑以下简单的样式表.我想创建一个XHTML文档,其中包含me:element作为正文. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:me="https://stackoverflow.com/xml" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="me"> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Title</title> </head> <body> <xsl:copy-of select="me:root/me:element/node()"/> </body> </html> </xsl:template> </xsl:stylesheet> 请注意,我包含了排除结果前缀…但是看看我得到了什么: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Title</title> </head> <body> <p xmlns:me="https://stackoverflow.com/xml">Some HTML code here.</p> </body> </html> 而这让我疯狂的是为什么,为什么xmlns:我出现在p元素中? 无论我尝试什么,我都无法上班.我有一种奇怪的感觉,问题出在我的xsl:copy-of语句中. 解决方法
这正是原因所在. 源XML文档包含此片段: <me:element> <p>Some HTML code here.</p> </me:element> 在XPath数据模型中,命名空间节点从子树的根传播到其所有后代.因此,< p> element具有以下命名空间: >“http://www.w3.org/1999/xhtml” 最后两个是reserved namespaces(对于前缀xml:和xmlns),可用于任何命名节点. 报告的问题是由于根据定义< xsl:copy-of>指令将所有节点及其完整子树复制到属于每个节点的所有名称空间. 请记住:指定为exclude-result-prefixes属性值的前缀仅从literal-result元素中排除! 解: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:me="https://stackoverflow.com/xml" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="me"> <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Title</title> </head> <body> <xsl:apply-templates select="me:root/me:element/*"/> </body> </html> </xsl:template> <xsl:template match="*"> <xsl:element name="{name()}"> <xsl:copy-of select="@*"/> <xsl:apply-templates/> </xsl:element> </xsl:template> </xsl:stylesheet> 当此转换应用于提供的XML文档时: <me:root xmlns:me="https://stackoverflow.com/xml" xmlns="http://www.w3.org/1999/xhtml"> <me:element> <p>Some HTML code here.</p> </me:element> </me:root> 产生了想要的正确结果: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Title</title> </head> <body> <p>Some HTML code here.</p> </body> </html> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |