简体   繁体   English

xslt1.0 / xml-为什么在添加名称空间信息时xsl文件将停止呈现xml

[英]xslt1.0/xml - why will xsl file stop rendering xml when namespace information is added

I've got an xsl file that has been working fine. 我有一个运行正常的xsl文件。 The templates are all in place and everything shows on the page when the root node is the following: 当根节点为以下节点时,所有模板均已就位,并且所有内容均显示在页面上:

<Document>
  <...>
</Document>

However, if namespace information is added to the xml document like so: 但是,如果像这样将名称空间信息添加到xml文档中,则:

<Document  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi_schemaLocation="urn:hl7-org:v3 CDA_SDTC.xsd" xmlns="urn:hl7-org:v3" xmlns:cda="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc">
  <...>
</Document>

My stylesheet hasn't changed and it was working for the first example: 我的样式表没有改变,它适用于第一个示例:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ms="urn:schemas-microsoft-com:xslt"
  xmlns="http://www.w3.org/1999/xhtml">

  <xsl:template match="/">
  </xsl:template>
</xsl:stylesheet>

There's nothing being read from the xml document at all on the web output. Web输出上根本没有从xml文档读取任何内容。 What would be the cause for that? 是什么原因造成的?

I made the following change to the XSLT file: 我对XSLT文件进行了以下更改:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hl7-org:v3 CDA_SDTC.xsd" 
xmlns="urn:hl7-org:v3" 
xmlns:cda="urn:hl7-org:v3" 
xmlns:sdtc="urn:hl7-org:sdtc"
>

but that throws an error on the xsi:schemaLocation because of the space between v3 and CDA. 但是由于v3和CDA之间的空间,在xsi:schemaLocation上引发了错误。 If I take out that one line like so: 如果我这样取出那一行:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:hl7-org:v3" 
xmlns:cda="urn:hl7-org:v3" 
xmlns:sdtc="urn:hl7-org:sdtc"
>

Then I still get nothing rendered out of the xml document onto the page. 然后,我仍然无法从xml文档中渲染任何内容到页面上。 I know I'm missing something but I'm not familiar enough with xsl to know what it is. 我知道我缺少某些东西,但是我对xsl不够了解,因此很难知道它是什么。

Thanks for any help. 谢谢你的帮助。

In XPath 1.0 expressions, unprefixed names always refer to nodes that are not in a namespace. 在XPath 1.0表达式中,无前缀名称始终引用不在名称空间中的节点。 If you want to refer to nodes that are in a namespace then you need to bind the relevant namespace URI to a prefix in the stylesheet and use that prefix in your XPaths. 如果要引用命名空间中的节点,则需要将相关的命名空间URI绑定到样式表中的前缀,并在XPath中使用该前缀。

In your example the document 在您的示例中,文档

<Document>
  <!--...-->
</Document>

has a root element with the local name Document and no namespace, so it can be matched by an XPath expression of /Document . 有一个具有本地名称Document且没有名称空间的根元素,因此可以通过/Document的XPath表达式进行匹配。 But the document 但是文件

<Document  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi_schemaLocation="urn:hl7-org:v3 CDA_SDTC.xsd" xmlns="urn:hl7-org:v3" xmlns:cda="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc">
  <!--...-->
</Document>

has a root element with local name Document in the urn:hl7-org:v3 namespace, which is not matched by the path /Document . urn:hl7-org:v3命名空间中具有本地名称为Document的根元素,该路径与/Document路径匹配。 In order to match that you would need to add 为了匹配,您需要添加

xmlns:hl7="urn:hl7-org:v3"

to the <xsl:stylesheet> tag, and then use XPath expressions like /hl7:Document . <xsl:stylesheet>标记,然后使用XPath表达式,例如/hl7:Document Since the default namespace xmlns="..." applies to descendant elements as well, you'll probably find that you have to add this prefix in many places, eg /Document/a/b would probably need to change to /hl7:Document/hl7:a/hl7:b . 由于默认名称空间xmlns="..."也适用于后代元素,因此您可能会发现必须在许多位置添加此前缀,例如/Document/a/b可能需要更改为/hl7:Document/hl7:a/hl7:b In XSLT 2.0 you can use xpath-default-namespace but this option is not available in 1.0. 在XSLT 2.0中,可以使用xpath-default-namespace但此选项在1.0中不可用。

The fact that the element name doesn't have a prefix in the original XML is irrelevant - what matters to XPath is the local name and the namespace URI. 元素名称在原始XML中没有前缀这一事实无关紧要-XPath重要的是本地名称和名称空间URI。

Your input XML no contains a default namespace xmlns="urn:hl7-org:v3" . 您的输入XML no包含默认名称空间xmlns="urn:hl7-org:v3" This means your XSLT also needs to know this namespace. 这意味着您的XSLT也需要知道此命名空间。

The XSLT processor matches on the namespace URI. XSLT处理器在名称空间URI上进行匹配。 So in your XSLT you can use a different prefix, for example putting xmlns:pref="urn:hl7-org:v3" in your XSLT. 因此,在XSLT中,您可以使用其他前缀,例如在XSLT中放置xmlns:pref="urn:hl7-org:v3"

You could than select elements in your XSLT with use of that prefix: <xsl:value-of select="/pref:rootElement/pref:childElement" /> 然后,您可以使用该前缀在XSLT中选择元素: <xsl:value-of select="/pref:rootElement/pref:childElement" />

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

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