如何使用默认命名空间编写XSL?
假设我有一个如下所示的 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Project xmlns="http://My/Project.xsd">
<Thing Name="test"/>
</Project>
我的 XSLT 是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns="http://My/Project.xsd">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="Thing">
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
输出为 [NewLine][Tab][NewLine]
,它与 XML 文件的间距匹配。
如果我将 XSLT 更改为:(添加前缀)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:N="http://My/Project.xsd">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="N:Thing">
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
输出为 [NewLine][Tab]test[NewLine]
,它再次匹配 XML 文件的间距,但包含“Name “ 属性。
我的预期输出只是测试
。没有新行,没有制表符 - 它根本不应该遵循 XML 文件的格式。
我想在不使用前缀的情况下编写 XML 和 XSLT。我怎样才能使这个输出达到我的预期?
Say I have an XML file which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Project xmlns="http://My/Project.xsd">
<Thing Name="test"/>
</Project>
And my XSLT is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns="http://My/Project.xsd">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="Thing">
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
The output is [NewLine][Tab][NewLine]
which matches the spacing of the XML file.
If I change my XSLT to be: (added a prefix)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:N="http://My/Project.xsd">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="N:Thing">
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
The output is [NewLine][Tab]test[NewLine]
which again matches the spacing of the XML file but includes the value of the "Name" attribute.
My expected output is simply test
. No new lines, no tabs - it should not follow the format of the XML file at all.
I want to write the XML and XSLT without using prefixes. How can I make this output what I'm expecting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个问题 - 第一是您不想指定名称空间前缀,第二是您不想源文档中的空格影响您的输出。让我们分别讨论它们。
使用命名空间前缀:
简短的回答是否定的 - 如果不使用前缀指定此类命名空间,则无法编写与特定命名空间内的元素相匹配的 XSL 模板。在您的第一个 XSLT 中,您可以阅读模板定义,例如“我想选择名为 Thing 的节点,它没有任何命名空间”,而您真正想说的是 >“我想选择名为 Thing 的节点,它具有命名空间 http://My/Project.xsd< /a>”。这就是 XPath 1.0 规范的工作方式(更多详细信息请参见此 文章)。
摆脱间距:
在样式表开头使用
指令指定您不希望在输出文档中保留所有源元素中的空格。如果您想保留其中一些,也可以使用
。There are two issues here - first is that you don't want to specify the namespace prefix and second is that you don't want to have spaces from source document to affect your output. Let's discuss them separately.
Using namespace prefix:
The short answer is no - you can not write XSL template that matches elements within particular namespace without specifying such namespace using prefix. In your first XSLT you could read template definition like "I want to select node named Thing which doesn't have any namespace" while what you really want to say is "I want to select node named Thing which has namespace http://My/Project.xsd". This is the way XPath 1.0 specification works (more details in this article).
Getting rid of spacing:
Use
<xsl:strip-space elements="*"/>
instruction at the beginning of stylesheet to specify that you don't want spaces from all source elements to be preserved in output document. If you want to preserve some of them use<xsl:preserve-spaces elements="myNode">
as well.