简体   繁体   English

条目XML标记导致XSL值不起作用

[英]Entry XML tag is causing the XSL value-of to not work

I have a (simplified) XML file I'm trying to transform: 我正在尝试转换一个(简化的)XML文件:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns="http://www.symplectic.co.uk/vivo/" xmlns:api="http://www.symplectic.co.uk/publications/api">
<tag>Hello, Hello.</tag>
</entry>

Wrapping my content in the "entry" tag seems to cause problems with this statement in my XSLT. 将我的内容包装在“ entry”标签中似乎会导致XSLT中此语句出现问题。

<xsl:value-of select="$xmlfile/entry/tag"/>

Where the $xmlFile is the fn:document output of the above XML. 其中$ xmlFile是上述XML的fn:document输出。 I have found that when removing the entry tags from the XML and simply modifying my XSLT to: 我发现,当从XML中删除条目标签并简单地将XSLT修改为:

<xsl:value-of select="$xmlfile/tag"/>

I get the content (Hello,Hello) I'm expecting in my output. 我得到了输出中期望的内容(Hello,Hello)。 I have verified that doing a copy-of $xmlfile returns the full XML file. 我已验证执行$ xmlfile的副本会返回完整的XML文件。 I'm sure there is something I'm not doing right syntactically as I'm new to XSL. 我敢肯定,由于我是XSL的新手,所以我在语法上没有做正确的事情。 Any advice would be appreciated. 任何意见,将不胜感激。

Thanks! 谢谢!

XPath and XSLT are namespace-aware. XPath和XSLT支持名称空间。 To specify a namespaced node in a path, you should give the node name a prefix that you have bound to the correct namespace. 要在路径中指定一个带名称空间的节点,您应该给该节点名称一个前缀,该前缀已绑定到正确的名称空间。

Using wildcards, and then adding a predicate to test only the localname, is possible but generally the Wrong Answer. 可以使用通配符,然后添加谓词以仅测试本地名称,但这通常是错误答案。 Namespaces are a meaningful part of the name. 命名空间是名称的重要组成部分。 Use them properly. 正确使用它们。

The issue is indeed with the entry element... 问题的确与入口元素有关...

<entry xmlns="http://www.symplectic.co.uk/vivo/"

The xmlns attribute here is actually a namespace declaration. 此处的xmlns属性实际上是名称空间声明。 Or rather, because it does declare a prefix (like xmlns:api does) it is the default namespace. 或者更确切地说,因为它确实声明了前缀(如xmlns:api一样),所以它是默认的名称空间。 This means the entry element and its child tag element belong to the namespace. 这意味着entry元素及其子标记元素属于名称空间。

Now, your xslt is doing this... 现在,您的xslt正在执行此操作...

<xsl:value-of select="$xmlfile/entry/tag"/>

This is looking for entry and tag elements that belong to NO namespace. 这是在寻找属于NO名称空间的entrytag元素。 These are different to elements with the same name that do belong to a namespace. 这些与属于名称空间的具有相同名称的元素不同。

The solution is to declare the namespace, with a prefix, in the XSLT, like so 解决方案是在XSLT中用前缀声明名称空间,例如

<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:vivo="http://www.symplectic.co.uk/vivo/">

Then change your xslt expression to look like this 然后更改您的xslt表达式,如下所示

<xsl:value-of select="$xmlfile/vivo:entry/vivo:tag"/>

Note the actual prefix is no important, but the namespace url must match. 请注意,实际前缀并不重要,但是名称空间url必须匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM