XPath 中源文件引用中的 XSLT 命名空间
我花了几个小时来解决我的问题,直到我意识到只要 XML 中定义了命名空间,XSL 文件中的 XPath 就与我的源文档不匹配:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<clonkDoc xmlns="https://clonkspot.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://clonkspot.org ../../../clonk.xsd">
<func>
<some-more-elements/>
</func>
<author>Author</author><date>lazy date</date>
</clonkDoc>
xmlns="https://clokspot.org"据我所知,需要
才能使用 XSD 引用该文档。
我的 XSL 文件如下所示(此处未显示“head”和“nav”):
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:clonk="https://clonkspot.org">
<xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.01//EN"
doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
<xsl:template match="/clonk:clonkDoc/clonk:func">
<html>
<xsl:call-template name="head"/>
<body>
<xsl:call-template name="nav"/>
<h1>
</h1>
</body>
</html>
</xsl:template>
结果如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:clonk="https://clonkspot.org">
<head>...
</head>
<body>
<h1></h1>
</body>
</html>
Authorlazy date
我有两个问题:
- 我在目标 html 文档中没有“clonk:”元素,但在
-元素定义了命名空间。
- 我想将所有 XPath 中的“clone:”保存在 XSL 中。
如何告诉 XSLT 处理器将 解释为源 XML 文档的默认命名空间(其中
xmlns="https:// clonkspot.org”
)?
非常感谢!
i have spent hours with my problem until i realized that the XPath in the XSL file does not match my source document as long as there is a namespace defined in the XML:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<clonkDoc xmlns="https://clonkspot.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://clonkspot.org ../../../clonk.xsd">
<func>
<some-more-elements/>
</func>
<author>Author</author><date>lazy date</date>
</clonkDoc>
The xmlns="https://clonkspot.org"
is needed as far as I know that the document can be referenced with an XSD.
My XSL file looks as following ("head" and "nav" not shown here):
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:clonk="https://clonkspot.org">
<xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.01//EN"
doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
<xsl:template match="/clonk:clonkDoc/clonk:func">
<html>
<xsl:call-template name="head"/>
<body>
<xsl:call-template name="nav"/>
<h1>
</h1>
</body>
</html>
</xsl:template>
The result looks like:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:clonk="https://clonkspot.org">
<head>...
</head>
<body>
<h1></h1>
</body>
</html>
Authorlazy date
I've two problems with it:
- I have no "clonk:" elements in the target html document but in the
<html>
-element the namespace is defined. - I would like to save the "clonk:" in all XPath in the XSL.
How do I tell the XSLT processor to interpret <template match="/clonkDoc/func">
to the default namespace of the source XML document (where xmlns="https://clonkspot.org"
)?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要从输出中排除
xmlns:clonk="https://clonkspot.org"
声明,请将其添加到xsl:stylesheet
开始标记中。我不明白你的第二个问题。如果您询问如何定义一个默认名称空间,该名称空间将用于样式表中任何 XPath 表达式中包含的任何无前缀元素名称,那么答案是您需要 XSLT 2.0:
https://www.w3.org/TR/xslt20/#unprefixed-qnames< /a>
To exclude the
xmlns:clonk="https://clonkspot.org"
declaration from the output, add this to thexsl:stylesheet
start-tag.I did not understand your 2nd question. If you are asking how to define a default namespace that will be used for any unprefixed element name included in any XPath expression in the stylesheet, then the answer is that you need XSLT 2.0 for that:
https://www.w3.org/TR/xslt20/#unprefixed-qnames