apply templates select substring-after with child node() - xslt

Here is my input file
<toc-title>(1) Thsi is <content-style>Short title</content-style>
</toc-title>
I want to output as following:
<toctitle>
<label>(1)</label>
<toctext>Thsi is <content-style>Short title</content-style></toctext>
</toctitle>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:template match="toc-title">
<xsl:copy>
<label>
<xsl:value-of select="substring-before(.,' ')"/>
</label>
<toctext>
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="position()=1">
<xsl:value-of select="substring-after(.,' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</toctext>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This iterates thru all child nodes of <toc-title>, assuming that the first child node is always text.

Related

precalculating node-set via copy-of and accessing ancestors (XSLT 1.0)

I want to precalculate a subtree of nodes in an source XML, and the process them seperately (because I want the subset to be processed in different ways), and access some values from ancestors.
simple example
<numbers count="5">
<number value="1"/>
<number value="2"/>
<number value="3"/>
<number value="4"/>
<number value="5"/>
</numbers>
and lets say I have an xslt (MSXML) to extract the even nodes somehow
<?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"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<evens>
<xsl:for-each select="numbers/number">
<xsl:choose>
<xsl:when test="#value mod 2 = 0">
<even>
<xsl:attribute name="count">
<xsl:value-of select="../#count"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="#value"/>
</xsl:attribute>
</even>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</evens>
</xsl:template>
</xsl:stylesheet>
and we get..
<evens>
<even count="5" value="2" />
<even count="5" value="4" />
</evens>
nice...
but how can I seperate the filtering from the processing so something like...
<?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"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template name="calculateNodes">
<xsl:for-each select="numbers/number">
<xsl:choose>
<xsl:when test="#value mod 2 = 0">
<xsl:copy-of select="."/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="nodes">
<xsl:call-template name="calculateNodes"/>
</xsl:variable>
<evens>
<xsl:for-each select="msxsl:node-set($nodes)/number">
<even>
<xsl:attribute name="count">
<xsl:value-of select="../#count"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="#value"/>
</xsl:attribute>
</even>
</xsl:for-each>
</evens>
</xsl:template>
</xsl:stylesheet>
this gives.
<evens>
<even count="" value="2" />
<even count="" value="4" />
</evens>
so...the ancestors arent copied.
Is there an idiomatic way to get out of this?
A copied node exists on its own, outside of the original tree. In your example, the parent of number is the $nodes variable, which does not have any attributes.
Why don't you do simply:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/numbers">
<xsl:variable name="nodes" select="number[#value mod 2 = 1]"/>
<evens>
<xsl:for-each select="$nodes">
<even count="{../#count}" value="{#value}"/>
</xsl:for-each>
</evens>
</xsl:template>
</xsl:stylesheet>
This way you have a variable containing a reference to the original nodes, instead of a copy. Then you also have access to the original parent. And the content of the variable is a node-set; you don't need to convert it.
this seems to work
<?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"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template name="calculateNodes">
<xsl:for-each select="numbers/number">
<xsl:choose>
<xsl:when test="#value mod 2 = 0">
<numberWrapper>
<xsl:attribute name="count">
<xsl:value-of select="../#count"/>
</xsl:attribute>
<xsl:copy-of select="."/>
</numberWrapper>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="nodes">
<xsl:call-template name="calculateNodes"/>
</xsl:variable>
<evens>
<xsl:for-each select="msxsl:node-set($nodes)/numberWrapper">
<even>
<xsl:attribute name="count">
<xsl:value-of select="#count"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="number/#value"/>
</xsl:attribute>
</even>
</xsl:for-each>
</evens>
</xsl:template>
</xsl:stylesheet>

Flattening with XSLT: I want to move one kind element one level

I have a XML file where elements B are inside elements A and I want to move them up. From:
<?xml version="1.0" encoding="utf-8"?>
<root>
<A>
<C>Text</C>
Text again
More text
<D>Other text</D>
<B>Text again</B>
<C>No</C>
<D>May be</D>
<B>What?</B>
</A>
<A>
Something
<B>Nothing</B>
<D>Again</D>
<B>Content</B>
End
</A>
</root>
I would like to have:
<?xml version="1.0" encoding="utf-8"?>
<root>
<A>
<C>Text</C>
Text again
More text
<D>Other text</D>
</A>
<B>Text again</B>
<A>
<C>No</C>
<D>May be</D>
</A>
<B>What?</B>
<A>
Something
</A>
<B>Nothing</B>
<A>
<D>Again</D>
</A>
<B>Content</B>
<A>
End
</A>
</root>
The closest XSLT program I have is this:
<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:output method="xml" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="A">
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="name()='B'">
<xsl:apply-templates select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="A">
<xsl:apply-templates select="."/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
It has two problems: it ignores text nodes (this is probably just a matter of adding |text() to the select="*") but, more important, it creates a element for each node while I would like them to stay together under one . For instance, the above stylesheet makes:
<A><C>No</C></A>
<A><D>May be</D></A>
where I want:
<A><C>No</C>
<D>May be</D></A>
In my XML files, are always direct children of , and there is no or nesting.
The main use case is producing HTML where UL and OL cannot be inside a P.
This question is related but not identical to xslt flattening out child elements in a DocBook para element (and may be also to Flatten xml hierarchy using XSLT
)
As I said in the comment to your question, this is not about moving elements up in hierarchy. It is about grouping nodes, and creating a new parent A element for each group determined by the dividing B element.
In XSLT 1.0 this can be achieved using a so-called sibling recursion:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates select="A"/>
</xsl:copy>
</xsl:template>
<xsl:template match="A">
<xsl:copy>
<xsl:apply-templates select="node()[1][not(self::B)]" mode="sibling"/>
</xsl:copy>
<xsl:apply-templates select="B[1]" mode="sibling"/>
</xsl:template>
<xsl:template match="node()" mode="sibling">
<xsl:copy-of select="." />
<xsl:apply-templates select="following-sibling::node()[1][not(self::B)]" mode="sibling"/>
</xsl:template>
<xsl:template match="B" mode="sibling">
<xsl:copy-of select="." />
<xsl:if test="following-sibling::node()[normalize-space()]">
<A>
<xsl:apply-templates select="following-sibling::node()[1][not(self::B)]" mode="sibling"/>
</A>
<xsl:apply-templates select="following-sibling::B[1]" mode="sibling"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
An XSLT-1.0 solution - which is quite ugly - is the following. The output is as desired, but only for this simple MCVE. A general solution would be far more complicated as #michael.hor257k mentioned in the comments. Without more data it is unlikely to create a better solution in XSLT-1.0. Solutions for XSLT-2.0 and above may simplify this.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="A">
<xsl:if test="normalize-space(text()[1])">
<A>
<xsl:copy-of select="text()[1]" />
</A>
</xsl:if>
<xsl:if test="preceding::*">
<xsl:copy-of select="B[1]" />
</xsl:if>
<A>
<xsl:copy-of select="C[1] | C[1]/following-sibling::text()[1] | D[1]" />
</A>
<xsl:if test="not(preceding::*)">
<xsl:copy-of select="B[1]" />
</xsl:if>
<A>
<xsl:copy-of select="C[2] | C[2]/following-sibling::text()[1]" />
<xsl:if test="D[2]">
<xsl:copy-of select="D[2]" />
</xsl:if>
</A>
<xsl:copy-of select="B[2]" />
<xsl:if test="normalize-space(text()[last()])">
<A>
<xsl:copy-of select="text()[last()]" />
</A>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Concerning the situation of
<A><C>No</C></A>
<A><D>May be</D></A>
It is handled appropriately in the above code. So its output is
<A>
<C>No</C>
<D>May be</D>
</A>
Easy in XSLT 2 or 3 with group-adjacent=". instance of element(B)" or group-adjacent="boolean(self::B)", here is an XSLT 3 example (XSLT 3 is supported by Saxon 9.8 or 9.9 on Java and .NET (https://sourceforge.net/projects/saxon/files/Saxon-HE/) and by Altova since 2017 releases):
<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 indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="A">
<xsl:for-each-group select="node()" group-adjacent=". instance of element(B)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:apply-templates select="current-group()"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy select="..">
<xsl:apply-templates select="current-group()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/gWmuiKv
In XSLT 2 you need to spell out the <xsl:mode on-no-match="shallow-copy"/> as the identity transformation template and use xsl:element instead xsl:copy:
<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 indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="A">
<xsl:for-each-group select="node()" group-adjacent=". instance of element(B)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:apply-templates select="current-group()"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{name(..)}" namespace="{namespace-uri(..)}">
<xsl:apply-templates select="current-group()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
</xsl:transform>
http://xsltransform.hikmatu.com/pPqsHT2

get value of variable to another template

There are references I found in the internet about the passing of variable to other template. I tried to follow all the references but, I can't get the value that I need to populate. I have this xml file:
<Item>
<Test>
<ID>123345677</ID>
</Test>
<DisplayID>99884534</DisplayID>
</Item>
I need to populate MsgId element if the DisplayID is not null, else get value from the ID. My XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="ID">
<xsl:variable name="IDV" select="substring(.,0,35)"/>
<xsl:apply-templates select="DisplayID">
<xsl:with-param name="IDP" select="$IDV"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="DisplayID">
<xsl:param name="IDP"/>
<xsl:element name="MsgId">
<xsl:choose>
<xsl:when test=".!='' or ./*">
<xsl:value-of select="substring(.,0,35)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($IDP,0,35)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
The condition if DisplayID is not null is working, however, if I remove the value of DisplayID, there's no value getting from the ID. I don't know if I doing it correctly.
Your feedback is highly appreciated.
Please try this,
Demo for references : http://xsltransform.net/ejivdHb/16
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://locomotive/bypass/docx" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Item">
<xsl:element name="MsgId">
<xsl:choose>
<xsl:when test="DisplayID !='' ">
<xsl:value-of select="substring(DisplayID , 0 ,35)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(Test/ID,0,35)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Since this is tagged XSLT 2.0, the match="Item" template from #TechBreak can be replaced by
<xsl:template match="Item">
<MsgId>
<xsl:value-of select="substring(
if (DisplayId != '')
then DisplayID
else Test/ID, 1 ,35)"/>
</MsgId>
</xsl:template>
(Note character counting starts from 1)

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>

XSL transformation failed : Expected QName

when I am trying to transform an XMl using below stylesheet, the transformation is failing with error Expected QName. What exactly is missing in the styelesheet, pasted below?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output method="xml" indent="yes" version="1.0"/>
<xsl:variable name="origo-svc-augmented" select="'Y'"/>
<xsl:variable name="origo-svc-ns" select="'http://www.origoservices.com'"/>
<xsl:template match="node()">
<xsl:variable name="namespace" select="namespace-uri(.)"/>
<xsl:choose>
<!--xsl:when test="$namespace = ''
or ($origo-svc-augmented='Y' and $namespace=$origo-svc-ns)"-->
<xsl:when test="$origo-svc-augmented = 'N'">
<xsl:element name="{local-name()}">
<xsl:copy-of select="namespace::*[not(namespace-uri()=$origo-svc-ns)]"/>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{local-name()}" namespace="{$namespace}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
UPDATE: After applying the suggestion from the answer provided, the stylesheet is working fine, updated stylesheet is as below
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output method="xml" indent="yes" version="1.0"/>
<xsl:variable name="origo-svc-augmented" select="'Y'"/>
<xsl:variable name="origo-svc-ns" select="'http://www.origoservices.com'"/>
<xsl:template match="*">
<xsl:variable name="namespace" select="namespace-uri(.)"/>
<xsl:choose>
<!--xsl:when test="$namespace = ''
or ($origo-svc-augmented='Y' and $namespace=$origo-svc-ns)"-->
<xsl:when test="$origo-svc-augmented = 'Y'">
<xsl:element name="{local-name()}">
<xsl:copy-of select="namespace::*[not(namespace-uri()=$origo-svc-ns)]"/>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{local-name()}" namespace="{$namespace}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="#*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I haven't tried it, but I think it may be because you're matching on node() (i.e. element, text node, processing instruction comment), but then trying to create an element with the name of that possibly non-element node. QName refers to a valid XML name, and I presume a text node, for example, wouldn't have one (when you call local-name()). You could change your template match to be *, and add another template for the other types.