Select a parallel node in xslt - xslt

Consider following xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<base>
<a>
<b>
<c>Text 1</c>
</b>
</a>
</base>
<base>
<a>
<b>
<c>Text 2</c>
</b>
</a>
</base>
</root>
and this xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root/base[1]" />
</xsl:template>
<xsl:template match="base//*">
<xsl:if test="text()">
<xsl:value-of select="text()" />
<!-- i need Text 2 here -->
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
The original xml is much more nested an i don't know the exact structure. But there is a parallel node with the same structure. If my template is at //root/base[1]/a/b/c I want to reference //root/base[2]/a/b/c
However I only know that I am in some node below //root/base[1] and that there is the same node in //root/base[2].
Is there a possibility to accomplish this goal?

This stylesheet requires no extensions, and does not make any assumption on unique names; the boring part is to pass the correct "twin node" every time you call xsl:apply-templates:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root/base[1]">
<xsl:with-param name="twin" select="root/base[2]"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*">
<xsl:param name="twin"/>
<xsl:if test="text()">
<xsl:value-of select="text()" />
<xsl:value-of select="$twin/text()"/>
</xsl:if>
<xsl:for-each select="*">
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select=".">
<xsl:with-param name="twin" select="$twin/*[$pos]"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>

Solution with using saxon:evaluate extension (http://saxon.sourceforge.net/saxon7.9/extensions.html#evaluate)
You can also use dyn:evaluate if you are using xslt 1.0 processors (http://exslt.org/dyn/index.html):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
xmlns:saxon="http://saxon.sf.net/"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root/base[1]" />
</xsl:template>
<xsl:template match="base//*">
<xsl:if test="text()">
<xsl:variable name="path">
<xsl:call-template name="constructPath">
<xsl:with-param name="node" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="rewritedPath" select="concat('/root/base[2]', substring($path, 11))"/>
<xsl:value-of select="text()" />
<xsl:value-of select="saxon:evaluate($rewritedPath)" />
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<xsl:template name="constructPath">
<xsl:param name="node"/>
<xsl:choose>
<xsl:when test="$node/parent::node()/name()">
<xsl:call-template name="constructPath">
<xsl:with-param name="node" select="$node/parent::node()"/>
</xsl:call-template>
<xsl:value-of select="concat('/', $node/name())"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('/', $node/name())"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
Here we use recursive template "constructPath" to create string and process it in evaluate() function. This will select the exact node as in the parallel branch. Hope this will help.

Try the code below. This code will work only if each base element has children with unique element name.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root/base[1]" />
</xsl:template>
<xsl:template match="base//*">
<xsl:if test="text()">
<xsl:variable name="name" select="name()"/>
<xsl:value-of select="text()" />
<xsl:value-of select="ancestor::base/following-sibling::base[1]//*[name() = $name]" />
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text()" />
However you can use dyn:evaluate from exslt extension or saxon:eval

Related

How we import the value in one xsl into another xsl in XSLT1.0

I have below xsl created Address1.xsl
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0">
<xsl:param name="FCode"/>
<xsl:param name="FName"/>
<xsl:template match="/">
<xsl:call-template name="Addtemplate"/>
</xsl:template>
<xsl:template name=" Addtemplate ">
<xsl:variable name="TownType">
<xsl:if test="$FCode!= ''">
<xsl:value-of select="ABCValue"/>
</xsl:if>
</xsl:variable>
<Add>
<addName>
<xsl:value-of select="$TownName"/>
</addName>
<addType>
<xsl:value-of select="$TownType"/>
</addType>
</Add>
</xsl:template>
</xsl:stylesheet>
In the second xsl Address2.xsl I want to import the value of addName.Tried below xsl but not getting the value.
Address2.xsl
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0">
<xsl:import href=" Address1.xsl "/>
<xsl:template match="/ ">
<xsl:variable name="TempVar">
<xsl:copy>
<xsl:call-template name="Addtemplate">
<xsl:value-of select=' addName'/>**<!-- I want addName value in the Address1.xsl to be mapped to TempVar variable -->**
</xsl:call-template>
</xsl:copy>
</xsl:variable>
</xsl:template>
</xsl:stylesheet>

XSLT - extract all text but last subsection

Looking to parse out a namespace from a full class name in xml.
Data example:
<results>
<test-case name="Co.Module.Class.X">
</results>
End result (going to csv format):
,Co.Module.Class
Stylesheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="text" indent="yes" encoding="ISO-8859-1"/>
<xsl:param name="delim" select="','" />
<xsl:param name="quote" select="'"'" />
<xsl:param name="break" select="'
'" />
<xsl:template match="/">
FullTestName, Namespace
<xsl:apply-templates select="//test-case" />
</xsl:template>
<xsl:template match="test-case">
<xsl:apply-templates />
<xsl:value-of select="#name" />
<xsl:value-of select="$delim" />
<xsl:value-of select="function to go here for nameWithJustNamespace" />
<xsl:value-of select="$break" />
</xsl:template>
I understand the process would need a last index of "." to be called once, yet I'm not finding XSLT to have that function. How to best accomplish this?
To do this in pure XSLT 1.0, you need to call a named recursive template, e.g.:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/results">
<xsl:call-template name="remove-last-token">
<xsl:with-param name="text" select="test-case/#name"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="remove-last-token">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'.'"/>
<xsl:value-of select="substring-before($text, $delimiter)"/>
<xsl:if test="contains(substring-after($text, $delimiter), $delimiter)">
<xsl:value-of select="$delimiter"/>
<xsl:call-template name="remove-last-token">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This pure XSLT 1.0 transformation (shorter, no conditional XSLT operations, single template):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="test-case[contains(#name, '.')]">
<xsl:param name="pDotIndex" select="0"/>
<xsl:variable name="vNextToken"
select="substring-before(substring(#name, $pDotIndex+1), '.')"/>
<xsl:value-of select="concat(substring('.', 2 - ($pDotIndex > 0)),$vNextToken)"/>
<xsl:variable name="vNewDotIndex" select="$pDotIndex+string-length($vNextToken)+1"/>
<xsl:apply-templates
select="self::node()[contains(substring(#name,$vNewDotIndex+1), '.')]">
<xsl:with-param name="pDotIndex" select="$vNewDotIndex"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<results>
<test-case name="Co.Module.Class.X"/>
</results>
produces the wanted, correct result:
Co.Module.Class
Part 2
With a slight modification the following transformation produces the complete CSV:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="test-case[contains(#name, '.')]">
<xsl:param name="pDotIndex" select="0"/>
<xsl:variable name="vNextToken"
select="substring-before(substring(#name, $pDotIndex+1), '.')"/>
<xsl:value-of select="concat(substring(',', 2 - (position() > 1)),
substring('.', 2 - ($pDotIndex > 0)), $vNextToken)"/>
<xsl:variable name="vNewDotIndex" select="$pDotIndex+string-length($vNextToken)+1"/>
<xsl:apply-templates
select="self::node()[contains(substring(#name,$vNewDotIndex+1), '.')]">
<xsl:with-param name="pDotIndex" select="$vNewDotIndex"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
When applied on this XML document:
<results>
<test-case name="Co.Module.Class.X"/>
<test-case name="Co2.Module2.Class2.Y"/>
<test-case name="Co3.Module3.Class3.Z"/>
</results>
the wanted, correct (CSV) result is produced:
Co.Module.Class,Co2.Module2.Class2,Co3.Module3.Class3

Transform the name of a node via a key-value list in XSLT

My Source XML sample looks like this.
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<T>A Book</T>
<A>A Man</A>
<D>Today</D>
</cd>
</catalog>
While 'T' means Title,'A' means Author,'D' means Date.
The output I want to get looks like this:
Title:A Book. Author:A Man. Date:Today
According to Implementing Key Value Concept in XSLT,I find that I can wirite the XSLT like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:codes>
<code key="T" value="Title"/>
<code key="A" value="Author"/>
<code key="D" value="Date"/>
</my:codes>
<xsl:key name="kCodeByName" match="code" use="#key"/>
<xsl:template match="/">
<xsl:for-each select="catalog/cd/*">
<xsl:apply-templates select="."/>:<xsl:value-of select="."/>.
</xsl:for-each>
</xsl:template>
<xsl:template match= "node()[name() = document('')/*/my:codes/*/#key]">
<xsl:variable name="vCur" select="name()"/>
<xsl:for-each select="document('')">
<xsl:value-of select=
"key('kCodeByName', $vCur)/#value"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
But if I want to use
<xsl:apply-templates select="name()"/>:<xsl:value-of select="."/>.
rather than
<xsl:apply-templates select="."/>:<xsl:value-of select="."/>.
What should I change in XSLT?
name() is a function, not a node; you cannot apply templates to it or use it in a match pattern.
Why don't you do simply:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="codes">
<code key="T" value="Title"/>
<code key="A" value="Author"/>
<code key="D" value="Date"/>
</xsl:variable>
<xsl:key name="kCodeByName" match="code" use="#key"/>
<xsl:template match="cd">
<xsl:apply-templates/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="cd/*">
<xsl:variable name="vCur" select="name()"/>
<xsl:for-each select="document('')">
<xsl:value-of select="key('kCodeByName', $vCur)/#value"/>
</xsl:for-each>
<xsl:text>:</xsl:text>
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:text>. </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Edit
If you prefer, you can change the last template to:
<xsl:template match="cd/*">
<xsl:call-template name="lookup">
<xsl:with-param name="key" select="name()"/>
</xsl:call-template>
<xsl:text>:</xsl:text>
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:text>. </xsl:text>
</xsl:if>
</xsl:template>
and add:
<xsl:template name="lookup">
<xsl:param name="key"/>
<xsl:for-each select="document('')">
<xsl:value-of select="key('kCodeByName', $key)/#value"/>
</xsl:for-each>
</xsl:template>

Excluding namespaces from XML

My xsl file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:go="http://www.google.com"
exclude-result-prefixes="go">
<xsl:include href="SomeLibrary.xsl"/>
<xsl:template match="/">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
The SomeLibrary.xsl file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.google.com">
<xsl:template name="SomeTemplate">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
Input xml: just use an empty XML. The result is this:
<?xml version="1.0" encoding="UTF-8"?>
<Blabla xmlns="http://www.google.com">
randomParam
</Blabla>
What I want is to have the "Blabla" node without a namespace. How can I remove it or make sure it doesn't get there, without modifying my "SomeLibrary.xsl"?
In case you don't want to edit the imported stylesheet code, the way to remove the namespace is with a two-pass transformation (which in XSLT 1.0 requires using an xxx:node-set() extension function):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
<xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
</xsl:template>
<xsl:template match="*" mode="pass2">
<xsl:element name="{name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="pass2"/>
</xsl:element>
</xsl:template>
<xsl:template name="SomeTemplate" xmlns="http://www.google.com">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the wanted result (the default namespace removed) is produced:
<Blabla>randomParam</Blabla>
Update:
The OP has indicated in a comment that he is using Xalan 2.07.
Below is almost the same solution, but with namespace and name for the xxx:node-set() function, as used in Xalan:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="x" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vPass1" select="x:nodeset($vrtfPass1)"/>
<xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
</xsl:template>
<xsl:template match="*" mode="pass2">
<xsl:element name="{name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="pass2"/>
</xsl:element>
</xsl:template>
<xsl:template name="SomeTemplate" xmlns="http://www.google.com">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
Explicitly create them with:
<xsl:element name="Blabla" namespace="">...</xsl:element>
but this means fiddling with the SomeLibrary.xsl file.
add exclude-result-prefixes="go" to the stylesheet element.
and, of course, update the namespace declaration from the default one to xmlns:go="http://www.google.com"

XSLT generating attributes if source-Element is in parameterfile

i got an xml-file with some elements. For some of these is an aqvivalent in a parameter xml-file along with some other elements.
I want to add these other elements from parm-file as parameter to output file if element-names are matching. (the Attributes should only be generated if an element "InvoiceHeader" exists in the source-xml.
Here is my code...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:variable name="rpl" select="document('ParamInvoice.xml')"></xsl:variable>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates></xsl:apply-templates>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:if test="$rpl/StoraInvoice/local-name()">
<xsl:call-template name="AttributeErzeugen">
<xsl:with-param name="attr" select="$rpl/StoraInvoice/local-name()"></xsl:with-param>
</xsl:call-template>
</xsl:if>
<xsl:apply-templates></xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template name="AttributeErzeugen">
<xsl:param name="attr"></xsl:param>
<xsl:for-each select="$attr">
<xsl:attribute name="{Attibute/#name}"><xsl:value-of select="."></xsl:value- of></xsl:attribute>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
and here the param-file
<?xml version="1.0" encoding="UTF-8"?>
<StoraInvoice>
<InvoiceHeader>
<Attribute name="Fuehrend">YYY</Attribute>
<Attribute name="Feld">FFFF</Attribute>
<Attribute name="Format">XYZXYZ</Attribute>
</InvoiceHeader>
</StoraInvoice>
Siegfried
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my"
exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<my:rpl>
<StoraInvoice>
<t>
<InvoiceHeader>
<Attribute name="Fuehrend">YYY</Attribute>
<Attribute name="Feld">FFFF</Attribute>
<Attribute name="Format">XYZXYZ</Attribute>
</InvoiceHeader>
</t>
</StoraInvoice>
</my:rpl>
<xsl:variable name="rpl" select="document('')/*/my:rpl"/>
<xsl:template match="*">
<xsl:variable name="vInvoiceElement" select=
"$rpl/StoraInvoice/*[name()=name(current())]"/>
<xsl:copy>
<xsl:if test="$vInvoiceElement">
<xsl:call-template name="AttributeErzeugen">
<xsl:with-param name="pInvoiced" select="$vInvoiceElement"/>
</xsl:call-template>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template name="AttributeErzeugen">
<xsl:param name="pInvoiced"/>
<xsl:for-each select="$pInvoiced/InvoiceHeader/Attribute">
<xsl:attribute name="{#name}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<t/>
produces the wanted, correct result:
<t Fuehrend="YYY" Feld="FFFF" Format="XYZXYZ"/>