xslt: keeping namespace declaration on root when root element is not known in advance - xslt-2.0

I have xml documents that follow a schema where most of the defined elements are allowed to be the root of a valid instance. I also have several xslt's v2.0 which translate it in various ways (put it into a normal form, a compact form, a different dialect, ...) These xslt's are all based on an identity transform with templates added to make the desired modification. The problem is that there is a proliferation of namespace attributes because there are some elements that come from outside the default namespace.
I have tried the recommended procedures for inserting the namespace on the root element, but I can't seem to get it right. The issues are:
1. the transformation may change the name, and sometimes the content of the root element, so I still need the templates for each of the global elements, and since I don't know which one will be root, I can't just insert namespace elements where needed (I don't know where they will be needed for a particular document.
2. I thought about implementing this as multi-pass, or simply an independent xslt, since I want the same result for several different xslts. In this case, what I would need is an identity transform that takes all the namespaces and prefixes from all elements in the document, and inserts them into the root. This would, I hope, automatically remove the namespace attributes from the children? However, I tried the following
<?xml version="1.0" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template name="start" match="/">
<xsl:copy>
<xsl:for-each select="*">
<xsl:copy>
<xsl:for-each select="descendant::*">
<xsl:call-template name="add-ns">
<xsl:with-param name="ns-namespace">
<xsl:value-of select="namespace-uri()"/>
</xsl:with-param>
<xsl:with-param name="ns-prefix">
<xsl:value-of
select=" prefix-from-QName( QName(namespace-uri(),name()))"/>
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template name="add-ns">
<xsl:param name="ns-prefix" select="'x'"/>
<xsl:param name="ns-namespace" select="'someNamespace'"/>
<xsl:namespace name="{$ns-prefix}" select="$ns-namespace"/>
</xsl:template>
<xsl:template match="node()|#* ">
<xsl:copy>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And this works for all prefixes that appear on elements, but it doesn't catch the prefixes of attributes. Here is a test document:
<RuleML xmlns="http://www.ruleml.org/0.91/xsd">
<Assert textiri="xy>z">
<Importation xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="abc"
textiri="urn:common-logic:demo1"
xlink:href="http://common-logic.org/x>cl/demos.xml"/>
<a:anything xmlns:a="http://anything.org"
xmlns:xlink="http://www.w3.org/1999/xlink"/>
</Assert>
</RuleML>
I want it to produce:
<RuleML xmlns="http://www.ruleml.org/0.91/xsd" xmlns:a="http://anything.org" xmlns:xlink="http://www.w3.org/1999/xlink" >
<Assert textiri="xy>z">
<Importation xml:id="abc"
textiri="urn:common-logic:demo1"
xlink:href="http://common-logic.org/x>cl/demos.xml"/>
<a:anything/>
</Assert>
</RuleML>
but instead I get
<RuleML xmlns="http://www.ruleml.org/0.91/xsd" xmlns:a="http://anything.org">
<Assert textiri="xy>z">
<Importation xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="abc"
textiri="urn:common-logic:demo1"
xlink:href="http://common-logic.org/x>cl/demos.xml"/>
<a:anything xmlns:xlink="http://www.w3.org/1999/xlink"/>
</Assert>
</RuleML>
Tara

Does the following do what you want?
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:template match="#* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="#* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="descendant::*/namespace::*"/>
<xsl:apply-templates select="#* , node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With Saxon 9.3 it seems to do the job on the sample you posted.
I am however not sure what you want to do if there are several elements in different default namespaces or several elements in different namespaces but using the same prefix. For instance with
<root xmlns="http://example.com/ns1">
<foo xmlns="http://example.com/ns2">
<pf:bar xmlns:pf="http://example.com/ns3">
<pf:foobar xmlns:pf="http://example.com/ns4"/>
</pf:bar>
</foo>
</root>
Saxon simply reports the error
Error at xsl:copy-of on line 15 of test2011061801Xsl2.xsl:
XTDE0430: Cannot create two namespace nodes with the same prefix mapped to different URIs
(prefix="", URI=http://example.com/ns2, URI=http://example.com/ns1)
in built-in template rule
[edit]
If you don't want an error to be reported you could try to implement a strategy to pull up namespace nodes as far up as possible but to avoid any collisions. That can be done with for-each-group, as in the following sample XSLT 2.0:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:template match="#* | text() | processing-instruction() | comment()">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:copy copy-namespaces="no">
<xsl:for-each-group select="descendant-or-self::*/namespace::*" group-by="local-name()">
<xsl:copy-of select="."/>
</xsl:for-each-group>
<xsl:apply-templates select="#* , node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With the input being
<root xmlns="http://example.com/ns1">
<foo xmlns="http://example.com/ns2">
<pf:bar xmlns:pf="http://example.com/ns3">
<pf:foobar xmlns:pf="http://example.com/ns4"/>
</pf:bar>
</foo>
</root>
Saxon 9.3 outputs
<?xml version="1.0" encoding="UTF-8"?><root xmlns="http://example.com/ns1" xmlns:pf="http://example.com/ns3">
<foo xmlns="http://example.com/ns2">
<pf:bar>
<pf:foobar xmlns:pf="http://example.com/ns4"/>
</pf:bar>
</foo>
</root>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*:RuleML">
<xsl:copy>
<xsl:for-each select="descendant::node()">
<xsl:choose>
<xsl:when test="self::text()"/>
<xsl:otherwise>
<xsl:for-each select="namespace::node()">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="(node() | #*) except namespace::node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="(node() | #*) except namespace::node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Related

Some help on how groupings actually work would be much appreciated

I'm trying to get my head around how the different grouping techniques work. What causes a group, how are each group defined, and how the key are formed for each group.
If I wanted to use "group-adjacent" to move all following siblings of a specific element name into the first of preceding sibling of a given type. Would this be doable? I know how I can do this with recursive templates, and to some extent with keys in xslt 1.0. But I cannot get the 2.0 groups to work for me.
Lets say that I want to move all fig elements into the first preceding para, given that there are no other kinds of elements in between the fig(s) and the preceding para element, in this simple xml.
<root>
<first_lvl>
<title>First heading</title>
<para>First para under first heading</para>
<para>Second para under first heading</para>
<fig>fig1</fig>
<fig>fig 2</fig>
<table>Table A</table>
<fig>fig 3</fig>
<para>Third para under first heading</para>
<para>Fourth para under first heading</para>
<fig>fig4</fig>
</first_lvl>
</root>
Desired result:
<root>
<first_lvl>
<title>First heading</title>
<para>First para under first heading</para>
<para>Second para under first heading
<fig>fig1</fig>
<fig>fig 2</fig>
</para>
<table>Table A</table>
<fig>fig 3</fig>
<para>Third para under first heading</para>
<para>Fourth para under first heading
<fig>fig4</fig>
</para>
</first_lvl>
</root>
How can I set a grouping up that takes care of every directly following fig element?
This doesn't work:
<xsl:template match=para[following-sibling::*[1][self::fig]]>
<xsl:for-each-group select"folowing-sibling::*" group-adjacent="boolean(self::fig)">
<xsl:apply-templates select="current-group()" mode="move"/>
</xsl:for-each-group>
</xsl:template>
And then I've added atemplate to build content for each fig inside the para, and one to ignore those figs when they appear later on in the processing.
No luck though.
I have no other values to group by, other that the fact that they are fig elements.
What am I missing here?
I would start with a group-starting-with on those para followed by a fig and then inside use group-adjacent to identify only the first group of adjacent figs. With the verbosity of XSLT that looks a bit convoluted but does the job as far as I have understood your requirements:
<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:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes" />
<xsl:template match="*[para and fig]">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:for-each-group select="*" group-starting-with="para[following-sibling::*[1][self::fig]]">
<xsl:choose>
<xsl:when test="self::para[following-sibling::*[1][self::fig]]">
<xsl:variable name="para-head" select="."/>
<xsl:for-each-group select="tail(current-group())" group-adjacent="boolean(self::fig)">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:copy select="$para-head">
<xsl:apply-templates select="node(), current-group()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/6qVRKxh
I have taken the liberty to use XSLT 3 instead of 2 but you simply would have to spell out the identity transformation declared by the xsl:mode on-no-match="shallow-copy" and make sure you use
<xsl:element name="{name($para-head)}" namespace="{namespace-uri($para-head)}">
<xsl:apply-templates select="$para-head/node(), current-group()"/>
</xsl:element>
instead of the XSLT 3 only xsl:copy with a select:
<xsl:copy select="$para-head">
<xsl:apply-templates select="node(), current-group()"/>
</xsl:copy>
and instead of the XPath 3 tail function you use subsequence e.g.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:output method="xml" indent="yes" />
<xsl:template match="*[para and fig]">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:for-each-group select="*" group-starting-with="para[following-sibling::*[1][self::fig]]">
<xsl:choose>
<xsl:when test="self::para[following-sibling::*[1][self::fig]]">
<xsl:variable name="para-head" select="."/>
<xsl:for-each-group select="subsequence(current-group(), 2)" group-adjacent="boolean(self::fig)">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:element name="{name($para-head)}" namespace="{namespace-uri($para-head)}">
<xsl:apply-templates select="$para-head/node(), current-group()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>
http://xsltransform.hikmatu.com/bFDb2BN
On the other hand, I am not sure whether an attempt not using xsl:for-each-group but rather a template matching para[following-sibling::*[1][self::fig]] and then consuming following sibling figs (which can be done easily in XSLT 3 with xsl:iterate) is not more compact:
<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:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="para[following-sibling::*[1][self::fig]]">
<xsl:copy>
<xsl:apply-templates select="#*, node()"/>
<xsl:iterate select="following-sibling::*">
<xsl:choose>
<xsl:when test="self::fig">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:break/>
</xsl:otherwise>
</xsl:choose>
</xsl:iterate>
</xsl:copy>
</xsl:template>
<xsl:template match="fig[preceding-sibling::*[not(self::fig)][1][self::para]]"/>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/6qVRKxh/3

Any limitation with Saxon-EE XSLT v3 Streaming?

I want to apply different tansformations to a big XML document using the Saxon XSLT3 streaming capabilities. The problem that I'm facing is that, if I apply this transformation it does not work:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="ano contextutil" xmlns:ano="java:StreamingGenericProcessor"
xmlns:contextutil="java:GenericAnonymizerContextUtil">
<xsl:mode streamable="yes"/>
<xsl:output method="xml"/>
<xsl:param name="context" as="class:java.lang.Object" xmlns:class="http://saxon.sf.net/java-type"/>
<xsl:template match="internal/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="email/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="address/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="birthday/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="country/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="external/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="name/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="phone/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="city/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="id/text()"><xsl:value-of select="ano:uuid($context, current(), 'ID')"/></xsl:template>
<xsl:template match="." >
<xsl:copy validation="preserve">
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
But with this one it does:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="ano contextutil" xmlns:ano="java:StreamingGenericProcessor"
xmlns:contextutil="java:GenericAnonymizerContextUtil">
<xsl:mode streamable="yes"/>
<xsl:output method="xml"/>
<xsl:param name="context" as="class:java.lang.Object" xmlns:class="http://saxon.sf.net/java-type"/>
<xsl:template match="email/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="address/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="birthday/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="country/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="external/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="name/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="phone/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="city/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="id/text()"><xsl:value-of select="ano:uuid($context, current(), 'ID')"/></xsl:template>
<xsl:template match="." >
<xsl:copy validation="preserve">
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I tested plenty of different scenarios and I concluded that if I have more than 9 "xsl:template" it does not work!
EDIT: it does not work means: on a specific tag named "id" I'm applying a java function. If I have more than 9 "xsl:template", the output is not modified and my java function is not called at all. I have no error message
EDIT2: If I replace the call to the java function with, for instance, "concat(current(), '_ID')", I have the same behaviour so this is not specific to the java function all.
EDIT3:
Here is a sample input data:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<row>
<id>10</id>
<email>fake#fake.com</email>
<address>dsffe</address>
<birthday>10/2018</birthday>
<country>FR</country>
<external>zz</external>
<internal>ww</internal>
<name>Jean</name>
<phone>000000</phone>
<city>Dfegd</city>
</row>
<row>
<id>9</id>
<email>fake#fake2.com</email>
<address>sdfzefzef</address>
<birthday>11/2012</birthday>
<country>GB</country>
<external>xx</external>
<internal>yy</internal>
<name>Jean-Claude</name>
<phone>000000</phone>
<city>dd</city>
</row>
This xsl which always works:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:mode streamable="yes"/>
<xsl:output method="xml"/>
<xsl:template match="email/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="address/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="birthday/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="country/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="external/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="name/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="phone/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="city/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="id/text()"><xsl:value-of select="concat(current(), '_ID')"/></xsl:template>
<xsl:template match="." >
<xsl:copy validation="preserve">
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The problematic one (the same xsl with one more template):
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:mode streamable="yes"/>
<xsl:output method="xml"/>
<xsl:template match="email/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="address/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="birthday/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="country/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="external/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="internal/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="name/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="phone/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="city/text()"><xsl:value-of select="current()"/></xsl:template>
<xsl:template match="id/text()"><xsl:value-of select="concat(current(), '_ID')"/></xsl:template>
<xsl:template match="." >
<xsl:copy validation="preserve">
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I run with the following command line:
java -cp Saxon-EE-9.8.0-14.jar net.sf.saxon.Transform -s:test.xml -xsl:concat_not_working.xsl
The working XSL properly append _ID to the output id tag value whereas the
not working xsl does not do any transformation.
Another information, if I run without the license (so without streaming), both stylesheets work!
I'm using Saxon-EE 9.8.0-14 with a trial license: could it be a non documented trial license limitation ?
Your theory that the failure occurs with 10 or more rules turns out to be spot on. When there are more than 10 rules matching the same node-kind/node-name combination (in this case, all text nodes), Saxon-EE attempts to avoid a linear search of all the rules by looking for criteria that subsets of the rules share in common. In this case it is looking to see whether it can group the rules according to a precondition based on the parent of the text node.
At this stage there is a flaw in the logic; it carefully works out that each rule is in a group of 1 (no two parent conditions are the same), which should mean that it then abandons the optimization attempt. But it doesn't abandon it; it carries on. This shouldn't matter, because the optimization should work correctly even though it was pointless.
The reason the optimization isn't working correctly is because on the streaming path for xsl:apply-templates, the context data for evaluating the rule preconditions isn't being initialized properly, leading the rule matcher to think that the preconditions aren't satisfied.
So you've hit a bug that, as you surmised, applies when you have a set of 10 or more template rules in a streaming mode when the rules all match nodes that have the same node-kind and node-name.
Running unlicensed bypasses the bug for two reasons: it deactivates the optimization of rule chains, and it deactivates streaming.
As a workaround, simply remove the /text() from each of your template rules.
Logged as a bug here: https://saxonica.plan.io/issues/3901
Unless you indicate otherwise, I will submit a new test case based on your test data and stylesheet to the W3C test suite for XSLT 3.0.

Add node into XML if another node doesn't exist

I am new to XSLT and working on transformation from one XML to another and facing below issue
If Book 1 node exist then I should not see Book 2 node into transformed XML. I tried different phrases but doesn't work.
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
<book1>Wise Otherwise</book1>
<book2>Great Expectations</book2>>
</Catalog>
Expected XML
Wise Otherwise
Below is my XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:if test="book1">
<xsl:template match="book2" />
</xsl:if>
<xsl:if test="not(book1)">
<xsl:template match="book2">
<book2>
<xsl:apply-templates />
</book2>
</xsl:template>
</xsl:if>
</xsl:stylesheet>
I even tried xsl:Choose
<xsl:choose>
<xsl:when test="/catalog/book1">
<xsl:template match="book2" />
</xsl:when>
<!-- more xsl:when here, if needed -->
<xsl:otherwise>
<xsl:template match="book2">
<book2>
<xsl:apply-templates />
</book2>
</xsl:template>
</xsl:otherwise>
</xsl:choose>
Book1 and Book2 are mutual exclusion from transformed xml.
Book2 should be in transformed XML if and only if Book1 is not in input XML.
I hope this will give you better picture
Thanks
Use a stylesheet with the identity transformation template
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
and the template <xsl:template match="Catalog[Book-1]/Book-2"/>.
Based on your provided XML sample a complete XSLT stylesheet is
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Catalog[book1]/book2"/>
</xsl:stylesheet>
which transforms
<Catalog>
<book1>Wise Otherwise</book1>
<book2>Great Expectations</book2>
</Catalog>
into
<Catalog>
<book1>Wise Otherwise</book1>
</Catalog>

XSLT set directory where result document ends up

The XSLT below creates result-documents as desired, with one exception: the result document ends up in the directory where the stylesheet was invoked from. I want the result document to be where it was found (i.e. overwrite itself with the transform version).
How can I do that?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xpath-default-namespace="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<xsl:for-each select="collection(iri-to-uri('file:///home/paul/Text/?select=*.xhtml'))">
<xsl:variable name="filename">
<xsl:value-of select="tokenize(document-uri(.), '/')[last()]"/>
</xsl:variable>
<xsl:result-document indent="yes" method="xml" href="{$filename}">
<xsl:apply-templates/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- transform templates removed -->
</xsl:stylesheet>
Try just using href="{document-uri(.)}" to use the full uri as the target rather than doing the tokenize to pull out the last segment.

How do I merge and concatenate the data from each row in two separate source files?

I have two source files which I need to combine on a row by row basis. I am happy reading the files into a variable and I am happy with the logic but the syntax has me stumped. For each row in file 1 I need to loop round each row in file 2 and output the two variables concatenated together:
File 1:
<rows>
<row>1</row>
<row>2</row>
<row>3</row>
<row>4</row>
</rows>
File 2:
<rows>
<row>a</row>
<row>b</row>
</rows>
Required output:
<rows>
<row>1/a</row>
<row>1/b</row>
<row>2/a</row>
<row>2/b</row>
<row>3/a</row>
<row>3/b</row>
<row>4/a</row>
<row>4/b</row>
<rows>
My (poor) attempt at getting the XSLT to work:
<rows>
<xsl:apply-templates select="document('file1.xml')/rows/row" />
</rows>
<xsl:template match="row">
<xsl:apply-templates select="document('file2.xml')/rows/row" />
</xsl:template>
<xsl:template match="row">
<row><xsl:value-of select="???" />/<xsl:value-of select="???" /></row>
</xsl:template>
(These files are simplified versions of what I actually have)
How do I make one template match one 'row' value and the other match another (both source files use the same structure). And how do I set those '???' values?
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vDoc2">
<rows>
<row>a</row>
<row>b</row>
</rows>
</xsl:variable>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<rows>
<xsl:apply-templates/>
</rows>
</xsl:template>
<xsl:template match="row">
<xsl:apply-templates select="$vDoc2/*/row" mode="doc2">
<xsl:with-param name="pValue" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="row" mode="doc2">
<xsl:param name="pValue" />
<row><xsl:sequence select="concat($pValue, '/', .)"/></row>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided first XML document:
<rows>
<row>1</row>
<row>2</row>
<row>3</row>
<row>4</row>
</rows>
the wanted, correct result is produced:
<rows>
<row>1/a</row>
<row>1/b</row>
<row>2/a</row>
<row>2/b</row>
<row>3/a</row>
<row>3/b</row>
<row>4/a</row>
<row>4/b</row>
</rows>

Resources