I have an input XML like
<values xsi:type="xsd:string"><Test objectgroupNr="001"/><bezeichnung>A&B </bezeichnung></values>
which has HTML encoded characters that I want to transform to "plain" XML encoding:
<values xsi:type="xsd:string">
<Test objectgroupNr="001"/>
<bezeichnung>A&B</bezeichnung>
</values>
I could change some characters with
<xsl:character-map name="fischer">
<xsl:output-character character="<" string="<"/>
<xsl:output-character character=">" string=">"/>
</xsl:character-map>
<xsl:output method="xml" use-character-maps="fischer"/>
But it does not seem to be a good idea to type in all possible special characters like Ä, Ü, ß, é and so on...
Can this be done in an easy way with XSLT? The transformation takes place in the environment of Sonic ESB using Saxon 8.9.
According to http://www.saxonica.com/documentation8.9/extensions/functions/parse.html the extension function is supported so you should be able to use e.g.
<xsl:template match="values">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:copy-of select="saxon:parse(concat('<root>', ., '</root>'))/*/node()"/>
</xsl:copy>
</xsl:template>
where you put xmlns:saxon="http://saxon.sf.net/" as a namespace declaration into the stylesheet.
Related
I have a transform.xsl file with will process a input.xml. But there is also an additional config.xml file which will define additional clauses. For e.g. this is the content of the config.xml.
<Location >
<DisplayName>
<Attribute1>ABC</Attribute1>
<Attribute2>XYZ</Attribute2>
<action>concat($Attribute1,$Attribute2)</action>
</DisplayName>
</Location >
So when transform.xsl will encounter the DisplayName variable within the input.xml, then it will form the value with the RESULT of the action expression defined in the config.xml file. transform.xml will call the config.xml just to get the result. (The action can be modified by the end user and hence these are placed outside the xsl file, within the config.xml).
We are using saxon xml processor version 9 and xslt 2.0. So we need to use saxon:evaluate(). I tried to find more examples of saxon:evaluate(), but couldn't find it more. Can anyone show me some examples of how to use it?
Thanks in advance.
***** This is an edited query to highlight the need of saxon:evaluate *****
Here is an example to use an XSLT 3 processor supporting xsl:evaluate (https://www.w3.org/TR/xslt-30/#dynamic-xpath) (i.e. Saxon 9.8 or later with the commercial PE or EE editions or Altova 2017 or later) to process your "config" file:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="config-url" as="xs:string">test2018121301.xml</xsl:param>
<xsl:param name="config-doc" select="doc($config-url)"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:key name="element" match="*" use="node-name()"/>
<xsl:function name="mf:config-evaluation" as="item()*">
<xsl:param name="config-doc" as="document-node()"/>
<xsl:param name="element-name" as="xs:QName"/>
<xsl:variable name="display" select="key('element', $element-name, $config-doc)/DisplayName"/>
<xsl:evaluate xpath="$display/regex" with-params="map:merge($display!(* except regex)!map { QName('', local-name()) : string() })"/>
</xsl:function>
<xsl:template match="*[key('element', node-name(), $config-doc)]">
<xsl:copy>
<xsl:value-of select="mf:config-evaluation($config-doc, node-name()), ."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
So with a config.xml
<Location >
<DisplayName>
<Attribute1>ABC</Attribute1>
<Attribute2>XYZ</Attribute2>
<regex>concat($Attribute1,$Attribute2)</regex>
</DisplayName>
</Location >
this would transform an input sample with e.g.
<Root>
<Items>
<Item>
<Data>data 1</Data>
<Location>location 1</Location>
</Item>
<Item>
<Data>data 2</Data>
<Location>location 2</Location>
</Item>
</Items>
</Root>
into
<Root>
<Items>
<Item>
<Data>data 1</Data>
<Location>ABCXYZ location 1</Location>
</Item>
<Item>
<Data>data 2</Data>
<Location>ABCXYZ location 2</Location>
</Item>
</Items>
</Root>
That gives you a great flexibility to allow XPath expressions in the configuration files but as pointed out in https://www.w3.org/TR/xslt-30/#evaluate-effect, also is a security problem: "Stylesheet authors need to be aware of the security risks associated with the use of xsl:evaluate. The instruction should not be used to execute code from an untrusted source.".
As for using the saxon:evaluate function supported in older versions of Saxon not supporting the XSLT 3 xsl:evaluate instruction, a simple example is
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
exclude-result-prefixes="#all"
version="2.0">
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="example">
<xsl:copy>
<xsl:value-of select="saxon:evaluate(#expression, #foo, #bar)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
which transforms the input
<root>
<example expression="concat($p1, $p2)" foo="This is " bar="an example."/>
<example expression="replace(., $p1, $p2)" foo="\p{L}" bar="X">This is example 2.</example>
</root>
into the result
<root>
<example>This is an example.</example>
<example>XXXX XX XXXXXXX 2.</example>
</root>
Try checking the xsl-attribute tag along with the xsl-value-of tag. If I get what you're asking for, you could probably read the config.xml using the transform.xsl (or a second xsl for an intermediate file) to set the text inside the regex tag to correspond to the value of an tag attribute within the xsl.
https://www.w3schools.com/xml/ref_xsl_el_attribute.asp
Also, check this tutorial for regex in XSLT 2, it may help:
https://www.xml.com/pub/a/2003/06/04/tr.html
Using xslt version 3.0 (saxon):
I have something like the following
<root>
<template ID='1'>
<params>
<a>1</a>
<b>1</b>
</params>
</template>
<document1 templateID='1'>
<params>
<b>4</b>
<c>5</c>
</params>
</document1>
</root>
Basicly I need to convert into something like
<root>
<document1 templateID='1'>
<params>
<a>1</a>
<b>4</b>
<c>5</c>
</params>
</document1>
</root>
In the example parameter a is inherited from the template while parameter b is overwritten by the document itself and parameter c is not known or set in the template. It is akin to inheritance or how css work. I hope you get the idea. Before starting the task I thought this should not be too difficult (and still hoping Im just overlooking something).
I have tried something with concat'ing the two nodeset (using nodeset1 , nodeset2 to preserve the order) and using a preceding-sibling name based 'select'/'filtering' - but this strategy seems not to work as it seems they are not actual siblings. Could this be done with a clever group-by ? Can it be done at all ? (I think it can)
I am using xslt version 3.0 (saxon)
I think you want to group or merge, merging in XSLT 3 would be
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:key name="template-by-id" match="template" use="#ID"/>
<xsl:template match="template"/>
<xsl:template match="*[#templateID]/params">
<xsl:copy>
<xsl:merge>
<xsl:merge-source name="template" select="key('template-by-id', ../#templateID)/params/*">
<xsl:merge-key select="string(node-name())"/>
</xsl:merge-source>
<xsl:merge-source name="doc" select="*">
<xsl:merge-key select="string(node-name())"/>
</xsl:merge-source>
<xsl:merge-action>
<xsl:copy-of select="(current-merge-group('doc'), current-merge-group('template'))[1]"/>
</xsl:merge-action>
</xsl:merge>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/jyH9rN8/
grouping would be
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:key name="template-by-id" match="template" use="#ID"/>
<xsl:template match="template"/>
<xsl:template match="*[#templateID]/params">
<xsl:copy>
<xsl:for-each-group select="key('template-by-id', ../#templateID)/params/*, *" group-by="node-name()">
<xsl:copy-of select="head((current-group()[2], .))"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/jyH9rN8/1
I think, as xsl:merge requires input to be sorted on any merge key or to sort the input first, the grouping above is easier and more reliable, unless your params child elements are really named with sorted letters or words from the alphabet.
I need to convert 11/14/2016 - This is String Data Type. I need to convert it to 2016-11-14 using XSLT 2.0. Please let me know how to proceed? I don't want to use subString method. I tried format-date() function from xslt 2.0 but not successful.
You could do something like this. You can use the tokenize function instead, if you do not want to use substring.
Assuming an input XML like:
<string>11/14/2016</string>
When run against a style sheet below:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="target_string">
<xsl:value-of select="tokenize(string, '/')[last()]"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="tokenize(string, '/')[1]"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="tokenize(string, '/')[2]"/>
</xsl:variable>
<date1>
<xsl:value-of select="xs:date($target_string)"/>
</date1>
<date2>
<xsl:value-of select="format-date(xs:date($target_string), '[MNn] [D], [Y]')"/>
</date2>
</xsl:template>
</xsl:stylesheet>
produces:
<date1>2016-11-14</date1><date2>November 14, 2016</date2>
Sorry to say that XSLT is following ISO 8601 which should be yyyy-mm-dd, so subString will be your only choice.
Reference:
https://www.w3.org/TR/xmlschema-2/#isoformats
If I look at the xslt specs it seems a character map applies to the whole document, bit is it also possible to use it on a given node, or within a template ?
Example : I have a node containing look up values, but they might contain characters that don't play well with regular expressions when using it in another template. For now I use a replace functionwhich works well,, but after a few characters that becomes pretty hard to read or maintain. So if I have something like this :
<xsl:variable name="myLookup" select="
replace(
replace(
replace(
replace(
string-join(/*/lookup/*, '|'),
'\[','\\['),
'\]','\\]'),
'\(','\\('),
'\)','\\)')
"/>
is there a way to achieve something like below fictitious example ?
<xsl:character-map name="escapechar">
<xsl:output-character character="[" string="\[" />
<xsl:output-character character="]" string="\]" />
<xsl:output-character character="(" string="\(" />
<xsl:output-character character=")" string="\)" />
</xsl:character-map>
<xsl:variable name="myLookup" select="string-join(/*/lookup/*, '|')" use-character-map="escapechar"/>
I know this is not working at all, it is just to make my request a bit visual.
Any idea ?
I think character maps in XSLT 2.0 are a serialization feature to be applied when a result tree is serialized to a file or stream so I don't see how you could apply one to a certain string or certain node during a transformation.
As for escaping meta characters of regular expression patterns, maybe http://www.xsltfunctions.com/xsl/functx_escape-for-regex.html helps.
Character maps is only a serialization feature, which means that it is only executed when the final output of a transformation is produced. However, you can significantly simplify your current code.
Just use:
replace($pStr, '(\[|\]|\(|\))','\\$1')
Here is a complete example:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:value-of select="my:escape(.)"/>
</xsl:template>
<xsl:function name="my:escape" as="xs:string">
<xsl:param name="pStr" as="xs:string"/>
<xsl:value-of select="replace($pStr, '(\[|\]|\(|\))','\\$1')"/>
</xsl:function>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<t>([a-z]*)</t>
the wanted, correct result is produced:
\(\[a-z\]*\)
I have the following basic xml which I to parse to give the NAME only if none of the DB values = DB1.
<rnas>
<rna ID="1">
<NAME>Segment 6</NAME>
<XREF>
<ID>AF389120</ID>
<DB>DB1</DB>
</XREF>
<XREF>
<ID>ABCDE</ID>
<DB>DB2</DB>
</XREF>
</rna>
<rna ID="10">
<NAME>Segment 3</NAME>
<XREF>
<ID>12345</ID>
<DB>DB2</DB>
</XREF>
<XREF>
<ID>66789</ID>
<DB>DB3</DB>
</XREF>
</rna>
</rnas>
The expected output would be:
<rnas>
<rna ID="10">
<NAME>Segment 3</NAME>
</rna>
<rnas>
I am still a relative newbie and have tried a variety of approaches using XSLT 2.0 but so far have not been able to get anything to work properly. Any help would be much appreciated.
This will do what you want
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="rna[.//DB/text()='DB1']"/>
<xsl:template match="XREF"/>
</xsl:stylesheet>
It's an Identity Transform along with two empty templates. The first matches any rna that contains a DB with the text value DB1, and suppresses it. The second matches all XREF elements, which you do not want to output.