XSLT not working now I added a namespace - xslt

I just added a namespace to my XML document and now my XSLT stylesheet does not work. What do I have to change in XSLT to support namespaces? (I have not yet changed the XSLT file since adding namespaces)

You have to add a xmlns:namespacehere attribute and use the namespace in the appropriate matches and selections.

Related

XSLT namespaces URL

What does a namespace do in XSLT when a url is provided such as:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
does this attempt to make a connection to the internet?
No; it just so happens that the specification for XML Namespaces (see W3C XSL Namespace specifications) are URI's.
They work in exactly the same way that namespaces in other languages do; they help uniquely identify things with the same names but in different contexts.
You can prove that no attempt is made to retrieve the resource by using a HTTP Monitor on your machine while loading or using the XSL Transformation - this answer has many good suggestions.
No.
Whatever the namespace is in a xsd, an xslt or any other xml file, there is no internet request.
The namespace is used to qualified your xml element.
When you conduct an XSLT transformation, the XSLT engine validates the XSLT file. It performs many checks, such as the root element being named stylesheet, etc. The engine must also be able to discern literal result elements (like <table>) from XSLT-specific elements (like <xsl:stylesheet>).
An element is recognized as XSLT-specific when it resides in the XSLT namespace. The value of the URI you posted (http://www.w3.org/1999/XSL/Transform) is simply a convention that makes it clear we're talking about XSLT. The prefix being defined (xsl) is the prefix used in the XSLT file to qualify the XSLT elements. You can use another prefix if you choose, provided you map it to the XSLT namespace.
Note that it's actually just a URI (an identifier), not a URL (a locator). There is no HTTP request to locate anything, it just identifies an abstract concept (in this case "XSLT").

Editing an html file like xml file

I need to convert HTML file to iXBRL format. iXBRL format is basically html with some embedded nodes or some information of html file wrapped under ixbrl tags. For this, I will need to SEARCH and REMOVE some nodes from HTML file and WRAP some nodes under iXBRL tags.
I'm not able to use XML DOM as it shoots an exception on content type. .Net htmldocument class doesn't support removing the nodes and replacing them, neither I could see the Save option.
I tried using HTML Agility Pack but It can't find the nodes because of namespaces in the node name and it doesn't have any option to specify namespaces (line namespace manager in .net).
Can I specify namespace in XPATH expression? How?
Can anyone help me in Editing HTML (or XHTML) files using .net or any free library.
If you want to use XPATH with namespaces you just need to prefix the nodes with the right namespace.
If your Namespace looks like this:
xmlns:xbrli="http://www.xbrl.org/2003/instance"
And your Elements are like this:
<root>
<xbrli:elementname></xbrli:elementname>
<root>
Then you can select them in XPATH like:
//xbrli:elementname

How to use an expression in xsl:include element in an XSLT processing

I need to include an XSLT that exists in 2 variants, depending on a param value.
However, it's seems to be not possible to write an expression in the href attribute of the xsl:include element. My last trial looks like that:
< xsl:param name="ml-fmt" select="mono"/>
...
< xsl:include href="{$ml-fmt}/format.xsl"/>
The XSLT engine used is Saxon 9.2.0.6
Have anybody an idea about how I could do something close to that ?
As Dimitre has said you can't do it, but you can generate the XSLT file from scratch or slightly modify an existing XSLT file by inserting the node in the code preparing the transformation.
You can't.
If you know all possible xslt stylesheet modules to be included, you could use the xsl:use-when attribute in order to selectively include only some of them. However, xsl:use-when has its own limitations. To quote the XSLT 2.0 Spec:
"Any element in the XSLT namespace may have a use-when attribute whose value is an XPath expression that can be evaluated statically".
There is a way to achieve dynamic inclusion, but it requires some non-XSLT initialization:
The code (think C# or Java or ... your programming language) that invokes the transformation, can edit the DOM of the loaded (as XML) XSLT stylesheet and can set the value of the href attribute of any <xsl:import> element to the desired URL.

How to deal with presence or not of xml namespaces using xslt

I have some XML/TEI documents, and i'm writing an XSLT 2.0 to extract their content.
Almost all TEI documents has no namespace, but one has the default namespace (xmlns="http://www.tei-c.org/ns/1.0").
So all documents has the same aspect, with unqulified tags like <TEI> or <teiHeader>, but if I try to extract the content, all works with "non-namespaced-documents", but nothing (of course) is extracted from the namespaced-document.
So i used the attribute xpath-default-namespace="http://www.tei-c.org/ns/1.0" and now (of course) the only document working is the namespaced one.
I can't edit documents at all, so what I'm asking is if there's a way to change dynamically the xpath-default-namespace in order to make work xpaths like //teiHeader both with namespaced and non-namespaced documents
If you are using XSLT 2.0, then you do have the option for a wildcard match for the namespace in a node test.
e.g. //*:teiHeader
http://www.w3.org/TR/xpath20/#node-tests
A node test can also have the form
*:NCName. In this case, the node test is true for any node of the principal
node kind of the step axis whose local
name matches the given NCName,
regardless of its namespace or lack of
a namespace.
This is functionally equivalent to Dimitre Novatchev's example, but a little shorter/easier to type.
However, this will only work in XSLT/XPATH 2.0.
There isn't really a clean way to do precisely what you are asking. However, there are workarounds available. You could use a two stage process whereby you strip the namespace from the document if it's present and then pass it through the same templates for all content.
There is a good example (in XSLT 1) of doing this in the DocBook XSLT. Take a look at html/docbook.xsl and common/stripns.xsl
Basically, you would need to assign the result of stripping the namespace to a variable and then call your existing templates (for the non namespaced) content but select the variable.
It is ugly, but this gives you what you want:
//*[name()='teiHeader']
If you use this style for all location steps in any XPath expression, the XPath expressions will select elements only by name, regardless whether or not the elements belong to any namespace.

Problem with XSLT where source xml document uses default namespace

Have a source xml document that uses namespace containing prefixes and a default namespace. When I transform it using a XSLT doc, the resulting translated xml document is incorrect, that is, element data from the source xml document is missing.
When I remove the "default namespace" from the source xml document, the transformation works as expected.
Question: is there a way to resolve problem without the need to edit out the default namespace from the source xml document? That is, add the solution to the XSLT document.
XML Document:
<MyElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.zolldata.com/UDX">
where the problem default namespace is xmlns="http://www.zolldata.com/UDX"
Specify same default namespace in XSLT document if your selectors do not use prefixes, or use prefixes for all selectors in the XSLT, but don't forget to bind them to the default namespace of your XML document.