简体   繁体   English

参考 xslt-1.0 中的当前 XML 文档

[英]Refer to current XML document in xslt-1.0

I want to add a root to a XML document with XSLT and append a child node to that.我想用 XSLT 向 XML 文档添加一个根,并在其中附加一个子节点。 This is difficult for me - because I do not have the name of the XML document when I am doing the XSLT.这对我来说很难——因为我在执行 XSLT 时没有 XML 文档的名称。 The XSL will be run on different XMLs. XSL 将在不同的 XML 上运行。

I've seen people add roots to XML documents like this:我见过人们像这样向 XML 文档添加根:

doc.AppendChild(rootNode);

Where 'doc' is their XML document.其中“doc”是他们的 XML 文档。

However, I do not know what the name of my XML document is, so I cannot refer to 'doc'.但是,我不知道我的 XML 文档的名称是什么,所以我不能引用“doc”。 Any ideas?有任何想法吗?

Working in xslt-1.0 btw.顺便说一句,在 xslt-1.0 中工作。

Kind regards亲切的问候

edit:编辑:

In more detail, when we get XML documents in, we need to run xslt code on them.更详细地说,当我们获取 XML 文档时,我们需要在它们上运行 xslt 代码。 Sometimes the XML documents are empty (thus, the root element is empty), and therefore, we need to add a root element with a child.有时 XML 文档是空的(因此,根元素是空的),因此我们需要添加一个带有子元素的根元素。

In order to do this, all I need right now is a way to refer to the XML document inside the xslt code.为了做到这一点,我现在只需要一种在 xslt 代码中引用 XML 文档的方法。

From the XPath 1.0 specification :XPath 1.0 规范

/ selects the document root (which is always the parent of the document element) /选择文档根(始终是文档元素的父级)

However, that's not going to help you in the situation you describe:但是,在您描述的情况下,这对您没有帮助:

Sometimes the XML documents are empty (thus, they do not have a root element), and therefore, we need to add a root element to it.有时 XML 文档是空的(因此,它们没有根元素),因此我们需要为其添加根元素。

because a document that does not have a root element is not a well-formed XML document and cannot be processed by XSLT.因为没有根元素的文档不是格式良好的 XML 文档,不能被 XSLT 处理。

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

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