Home > Software design >  Refer to current XML document in xslt-1.0
Refer to current XML document in xslt-1.0

Time:05-11

I want to add a root to a XML document with XSLT and append a child node to that. This is difficult for me - because I do not have the name of the XML document when I am doing the XSLT. The XSL will be run on different XMLs.

I've seen people add roots to XML documents like this:

doc.AppendChild(rootNode);

Where 'doc' is their XML document.

However, I do not know what the name of my XML document is, so I cannot refer to 'doc'. Any ideas?

Working in xslt-1.0 btw.

Kind regards

edit:

In more detail, when we get XML documents in, we need to run xslt code on them. Sometimes the XML documents are empty (thus, the root element is empty), and therefore, we need to add a root element with a child.

In order to do this, all I need right now is a way to refer to the XML document inside the xslt code.

CodePudding user response:

From the XPath 1.0 specification:

/ 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.

because a document that does not have a root element is not a well-formed XML document and cannot be processed by XSLT.

  • Related