I have a XML document which root node is sv:node and I am trying to define a template who match this root node.
When my root node has no prefix it usually works but in this case an exception is thrown.
<xsl:template name="/sv:node" />
I am using JAVA with the Apache XALAN XSLT processor which raises a RuntimeException
Caused by: java.lang.RuntimeException: Le préfixe doit se convertir en espace de noms : /sv
at org.apache.xml.utils.QName.<init>(QName.java:450)
at org.apache.xalan.processor.XSLTAttributeDef.processQNAME(XSLTAttributeDef.java:937)
I apologize about the localized message of the RuntimeException, basically it is complaining about the prefix with is not mapped to a namespace.
So the question is, how can I match this root node?
Edit : Here is my stylesheet tag
Here is my stylesheet tag :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:zip="http://apache.org/cocoon/zip-archive/1.0"
xmlns:sv="http://www.jcp.org/jcr/sv/1.0">
I suspect you really meant
<xsl:template match="/sv:node" />
(with match rather than name). Using <xsl:template name="..."> declares a named template which can be called by <xsl:call-template>, not a matching template for <xsl:apply-templates>, and the name of a named template must be a QName. The exception is telling you that it's trying to treat the three characters "forward-slash, s, v" as the prefix part of the QName and finding that this prefix is not mapped to a URI.
It was a silly mistake, I was using
<xsl:template name="/sv:node" />
instead of
<xsl:template match="/sv:node" />
Related
While doing XSLT transformation, I have a requirement to call Datasource inside xsl. I am using ora-ext to achieve it. But I am getting Cannot find a matching 5-argument function named and 4-argument matching function on use of the query-database function of ORAEXT.
Full stacktrace:
Static error at char 1 near {...t_code','jdbc/JDBCDataSourc...} in expression in xsl:value-of/#select on line 1 column 1575
XPST0017: Cannot find a matching 5-argument function named
{http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc}lookup-table().For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS
This is my XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
xmlns:sql="http://ns.saxonica.com/sql"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:java="http://saxon.sf.net/java-type"
xmlns:saxon="http://saxon.sf.net/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ora="http://schemas.oracle.com/xpath/extension"
exclude-result-prefixes="java saxon xsd xsi xsl sql"
extension-element-prefixes="saxon sql"
>
<xsl:template match="Order">
<Parameter>
<xsl:attribute name="name">ACT_CODE</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="oraext:lookup-table('ACT_CODE_MTDT', 'prdt_id','1159', 'act_code','jdbc/JDBCDataSource')"/>
</xsl:attribute>
</Parameter>
</xsl:template>
</xsl:stylesheet>
Kindly suggest, if I am missing on something. Suggest me if any jar apart from ojdbc6.jar is required.
Also, I'm not able access this URL.
Is that the namespace got changed, or there something missing in my code.
It looks to me as if you are running Saxon as your XSLT processor (we can tell from the form of the error messages) but you are trying to call an extension function that has been defined by Oracle as an vendor extension for their own XSLT processor. That ain't gonna work.
I'd like to have unclosed html tag as a result of xslt. I'll add closing tag later in xslt. How can I achieve this? This one doesn't compile:
<xsl:when test="$href">
<xsl:text><a href='{$href}'></xsl:text>
</xsl:when>
Thanx
This is the kind of thing that you probably should want to avoid at all costs. I do not know your requirements but you perhaps want a link or a span tag based on something.
In these instances you can use something like this
<xsl:apply-templates select="tag"/>
then 2 templates ie
<xsl:template match="tag">
<span>hello king dave</span>
</xsl:template>
<xsl:template match="tag[#href]">
link text....
</xsl:template>
It's hard to give a definite answer without a better idea of the precise use case, but it's worth noting that you can use match and name on the same <xsl:template>. For example, if you want to produce some particular output for all <tag> elements, but also wrap this output in an <a> tag in certain cases, then you could use an idiom like
<xsl:template match="tag[#href]">
<xsl:call-template name="tagbody" />
</xsl:template>
<xsl:template match="tag" name="tagbody">
Tag content was "<xsl:value-of select="."/>"
</xsl:template>
The idea here is that tag elements with an href will match the first template, which does some additional processing before and after calling the general tag template. Tags without an href will just hit the normal template without the wrapping logic. I.e. for an input like
<root>
<tag>foo</tag>
<tag href="#">bar</tag>
</root>
you would get an output like
Tag content was "foo"
Tag content was "bar"
I had the same problem before and was only able to solve it by copying the entire <a href='{$href}'>...</a> for each when branch.
Maybe you could try setting the doctype of your XSL to some loose XML standard, but afaik XSLT is pretty strict.
Edit: apparently you can set the doctype with a <xsl:output> tag.
Found solution on the net:
<xsl:text disable-output-escaping="yes"><![CDATA[<a href=']]></xsl:text>
<xsl:value-of select="href"/>
<xsl:text disable-output-escaping="yes"><![CDATA['>]]></xsl:text>
How would I write an xpath query to match the <subitem> element in the XML snippet below?
Specifically I want to match any element that has a "name" attribute that matches the value of the "name" attribute of the root. There may be an arbitrary number of ancestors between <root> and <subitem>.
<root name="xyz">
<anything>
<subitem name="xyz" />
</anything>
</root>
My goal is to write a PMD rule for finding cases of Logger.getLogger() that use a different class than the class the statement is in.
Use this XPath expression:
//*[#name = /*/#name]
This selects any element (including the top element named root), whose name attribute has the same string value as that of the name attribute of the top element. When this XPath expression is evaluated against the provided XML document, two elements -- root and subitem are selected.
I think that only the second element of these two is wanted. For this, use the following XPath expression:
//*/*[#name = /*/#name]
XSLT - based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="//*/*[#name = /*/#name]"/>
========
<xsl:copy-of select="//*[#name = /*/#name]"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<root name="xyz">
<anything>
<subitem name="xyz" />
</anything>
</root>
the two XPath expressions are evaluated and the selected nodes are copied to the output:
<subitem name="xyz"/>
========
<root name="xyz">
<anything>
<subitem name="xyz"/>
</anything>
</root>
<subitem name="xyz"/>
//*[#name=(ancestor::*/#name)]
This should do the trick. I would read it as "Find all nodes that have an ancestor with the same name."
EDIT (plus a second one, it still selected too much)
After the edit of the question:
//*[#name=(/#name)]
This reads as "Find all nodes that have the same name as the root node." Note that the root node might not be what you think it to be (and you want to match against the topmost node named root, not root), and you might need to implement it as
//*[#name=(/root/#name)] or //*[#name=(/*/#name)]
This selects two elements, your subitem and root which is not wanted, you only want to select nodes that are not topmost (are descendants of root). Credits to Dimitre Novatchev for this.
//*/*[#name=(/*/#name)]
I have a simply XSLT 1.0 stylesheet, that turns XML documents in XHTML. I really want to be able to "include" the content of an XML file in another when needed. AFAIK it is simply not possible in XSLT 1.0, so I decided to move my processing to a simple Java app that would pre-process the XML, executing the "includes" recursively, and passing it to the default JDK XSLT processor. I have a XML schema that my documents must conform to.
The most used element is called "text", and can have an "id" and/or a "class" attribute, which gets used for XHTML styling with CSS. This element gets turned into "p", "div", or "span" depending on the context.
What I would like to add, is the ability to define "unknown" elements in my input files, and have them transformed in a "text" element for further processing. If the "unknown" element's name start with a capital letter, then it becomes a "text", with "id" set to original name. Otherwise a "text" with "class" set to original name. Everything else in the unknown element should be kept as-is, and then it should be processed by XSLT as if it was originally in the input file. In other words, I would like to transform all unknown elements to for a valid XML document, and then process it with my stylesheet.
Can this be done in XSLT, possibly in a pre-processing "stylesheet", or should I do that as pre-processing in Java? Performance here is not important. I would prefer a XSLT solution, but not if it's much more complicated then doing it in Java.
Well, since no one answered, I just tried it. While is is easier to do it in Java, it has one major drawback: since the code need to know the valid elements so that it recognize the unknown ones, you end up having to hardcode that in your code and have to recompile it if the XSLT template changes.
So, I tried in XSLT and it also works. Let's say you have:
<xsl:template match="text">
*processing*
<xsl:call-template name="id_and_class"/>
*processing*
</xsl:template>
where the template named id_and_class copies your id and classes attribute in the generated element, and you want unknown elements to be mapped to "text" elements, then you can do this:
<xsl:template match="text">
<xsl:call-template name="text_processing"/>
</xsl:template>
<xsl:template name="text_processing">
*processing*
<xsl:call-template name="text_id_and_class"/>
*processing*
</xsl:template>
...
<xsl:template name="text_id_and_class">
<xsl:choose>
<!-- If name() is not "text", then we have an unknown element. -->
<xsl:when test="name()!='text'">
<!-- Processing of ID and class omitted ... -->
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="id_and_class"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
...
<!-- MUST BE LAST : Process unknown elements like a "text" element. -->
<xsl:template match="*">
<xsl:call-template name="text_processing"/>
</xsl:template>
If yon process the content of one specific element with a named template, then you can check in that template if the name matches, and use that for your special processing. Then you just have to put a <xsl:template match="*"> at the end of your stylesheet and call the named template from there.
I'm trying to validate an XML file, but I get the following error:
Can not find declaration of element
'xsl:stylesheet'.
This is the XML:
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt' exclude-result-prefixes='msxsl' xmlns:ns='http://www.ibm.com/wsla'>
<xsl:strip-space elements='*'/>
<xsl:output method='xml' indent='yes'/>
<xsl:template match='#* | node()'>
<xsl:copy>
<xsl:apply-templates select='#* | node()'/>
</xsl:copy>
</xsl:template>
<xsl:template match="/ns:SLA/ns:ServiceDefinition/ns:WSDLSOAPOperation/ns:SLAParameter/#name[.='TotalMemoryConsumption']">
<xsl:attribute name='{name()}'>
<xsl:text>MemConsumption</xsl:text>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Where is the mistake?
EDIT: I want to parse this XML in Java with SAX, but I get the following error:
Element type "xsl:template" must be followed by either attribute specifications, ">" or "/>".
How to get rid of it?
Assuming you are actually trying to validate your XSL as an XML document, it looks like that website requires you to point to a schema or DTD in order to validate the XML against it. You can get a non-normative schema here: http://www.w3.org/TR/xslt20/#schema-for-xslt. Here's instructions on how to reference a schema from an XML file: http://www.ibm.com/developerworks/xml/library/x-tipsch.html
You could also check "Well-Formedness only," and check the document for well-formedness, if not actually validity.
Generally, any XSL engine will report any errors in your XSL document, so you don't need to validate it separately.
Your XSL is OK, don't worry. Just that there is no DTD/XSD for XSLs 1.0. no one bothers checking XSLT stylesheets (1.0) for validity. "Wellformedness" is enough.