DTD validation before XSLT transformation - xslt

I have to write an XSLT to convert a XML file into another XML file. The original input file has to valid with respect to a DTD. Is there a way in which XSLT can validate the input xml file against the DTD before converting it ? Also reject the input file if it does not conform to the DTD.
Also , I will run the XSLT in a tool like Oxygen XML Editor.
Thanks in advance.
JK

You have to do this through your XSLT processor. I don't think there is a way to do this in XSLT itself.
In oXygen, you can go to "XML / XSLT-FO-XQuery / XSLT / Saxon / Saxon-HE/PE/EE" in Preferences and turn DTD validation on or off (there is a combo box).

Not with builtin XSLT-1.0 functions (which cannot detect non-well-formedness in a clean manner either). Your environment may be able to provide this through other tools such as DOM Builders.

This depends entirely on the tool you are using. In this case, this is a question about the Oxygen editor. There's nothing in the specification for XSLT that requires a processor to perform DTD validation when reading XML.

Related

Execute XSLT on an XML-Schema rather than an XML document

I know that the input of an XSLT processor is a source XML document that will be transformed into a target XML document.
In my case, I haven't a source XML document but I have a source XML-SCHEMA and I want to know through the XSLT document information about the mappings between the source XML-SCHEMA and the target one.
Thus, I have the idea of executing or maybe parsing the XSLT on the source XML-SCHEMA in order to get this information.
I'm really confused about the difference between execute and parse an XSLT document.
I think that to execute an XSLT document, an XSLT processor firstly parses it to transform it to another internal representation.
What is this internal representation??
I really need your help
I'm really confused about the difference between execute and parse an
XSLT document.
To parse a language, including XML, is to analyse it into parts.
To execute a language, including XSLT, is to perform the instructions specified by the language.
It does not make sense to talk about executing XML because XML itself specifies no instruction.
It does make sense to talk about validating XML against the grammar given by its XSD.
I think that to execute an XSLT document, an XSLT processor firstly
parses it to transform it to another internal representation. What is
this internal representation??
You do not need to know an XSLT processor's internal representation because that is an implementation detail.
You do need to know that an XSLT processor parses an input XML document, executes an XSLT transformation, and generates output. (It also parses the XML that represents the XSLT, but that's beside the point.)
The input XML document typically is not an XSD. You might want to consider the XSD associated with the input XML document in order to anticipate allowed input variations. In rare circumstances you might want to transform the XSD itself in some way, and you can do so since an XSD is represented in XML. In other rare circumstances, you might want to transform XSLT itself in some way, and you can do so since XSLT is also represented in XML. But, normally, the input to an XSLT transformation is a mundane XML document instance, not an XSD, and not other XSLT.

How to use XML wrapped in CDATA inside another XML for XSL transformation?

An XML document contains another XML element, which is wrapped in CDATA.
How can the wrapped XML be used for XSL and XSL-FO transformation (version 1)?
If you are willing to take multiple transformation steps then it is possible. Output the relevant section with disable-output-escaping to turn the escaped XML into valid XML. Process it in a subsequent step.
It does require the escaped XML to be well-formed. And some parsers require the intermediate result to be serialized (to disk or else) first to make sure the escaped XML is properly unescaped before it enters the subsequent transformations.
This is not possible with standard XSLT 1.0 or 2.0, in a single transformation.
It can be done using Saxon 9 Professional Edition or Enterprise Edition. These products have a saxon:parse() extension function. Or use the XPath 3.0 parse-xml() function, which is also supported by recent versions of Saxon PE/EE.
As #grtjn points out, it is possible to do it with a two-pass process. Stylesheet 1 turns the CDATA-wrapped text into parseable XML (using <xsl:value-of select="whatever" disable-output-escaping="yes"/>). Stylesheet 2 then processes the serialized result produced by stylesheet 1.

Is there a tool to check if an XSLT stylesheet follows coding standards?

How can we check whether an XSLT stylesheet is following all the coding standards? Is there a tool where we can specify our own rules and find out if the stylesheet conforms?
First of all, there is nothing like
"all coding standards"
...
This said, take a look at the XSLT Lint, developed by Mukul Ghandi, and published Dec. 2008:
http://lists.xml.org/archives/xml-dev/200812/msg00178.html
There is also one published recently by Andriy Gerasika:
http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/201102/msg00103.html
In case you are interested in functional programming with XSLT, take a look at FXSL.
Finally, if by "all coding standards" you mean "style", you may look at my answers in the xslt tag of SO, to learn a little bit more about "push style" and programming without explicit logical instructions.
Well, if the stylesheet works, it should be valid, right?
Other than that, i think all the big XML IDEs like Altova's XMLSpy provide some sort of schema validation, if that's what you're looking for.
XSL stylesheets is a XML language. So, validation tools available for XML still applies here. So, DTD or XML namespaces can be used to define the rules to check. Link to the location where the DTD/ns reside in the XSL sheet. Then tools like Xerces can be used for validating the document.
If you are using ANT, xmlvalidate task will do this automatically by invoking Xerces SAXParser.
<target name="validate" if="perform-validation-dtd">
<xmlvalidate file="${input-xml}"
classname="org.apache.xerces.parsers.SAXParser"/>
</target>
xmllint program validates an XML file against its DTD (Document Type Definition) and reports on any differences. Read more here
Well, an XSLT stylesheet is XML itself of course; you could easily write an XSLT that looks for patterns.
For example:
<xsl:template match="xsl:for-each">
<xsl:text>Inappropriate use of xsl:for-each; should be using templates instead</xsl:text>
</xsl:template>
if your policy includes not using xsl:for-each.
Or, you could write a schema that expands on the xslt one.

Is there a free C++ xsl-fo to PDF engine?

Is there an xsl-fo to PDF engine written in C++ that can be used in QT?
The engines I have come across so far are in java.
You should use QXmlQuery: link
From qt docs:
Running an XSLT stylesheet is like running an XQuery, except that when you construct your QXmlQuery, you must pass QXmlQuery::XSLT20 to tell QXmlQuery to interpret whatever it gets from setQuery() as an XSLT stylesheet instead of as an XQuery. You must also set the input document by calling setFocus().
Code:
QXmlQuery query(QXmlQuery::XSLT20);
query.setFocus(QUrl("myInput.xml"));
query.setQuery(QUrl("myStylesheet.xsl"));
query.evaluateTo(out);
where "out" is pdf file. and xsl file define how to trasform your xml file to pdf.

How to create XSD document from an XSLT?

I have an XSLT, now i need to create a new XSD document from that xslt.
Please any one help.
As far as I understand you already have this flow
input XML --> XSLT --> output XML
Now its not clear if you want to creat the XSD for the input or output xml.
There are many tools which will help you in generating XSD schema from a given XML.
Here is one of those you can try online: click here
You can of course google for more or if you want to download a desktop version.