XSL outputting more than expected - xslt

I'm fairly new to XSLT and XPath and have been banging my head against the wall for a while on this problem.
I have the following XML:
<reply>
<multi-results>
<multi-item>
<name>node1</name>
<information>
<block>
<slot>A</slot>
<state>Online</state>
<colour>purple</colour>
</block>
<block>
<slot>B</slot>
<state>Online</state>
<colour>yellow</colour>
</block>
<block>
<slot>C</slot>
<state>Online</state>
<colour>red</colour>
</block>
<block>
<slot>D</slot>
<state>Online</state>
<colour>blue</colour>
</block>
</information>
</multi-item>
</multi-results>
<address>
<label>this is an arbitrary bit of text included for this example</label>
</address>
</reply>
There are a variable number of "block" entries per file.
I want to "CSV" the data, and I'm using the following XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="*/text()[normalize-space()]">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<xsl:template match="*/text()[not(normalize-space())]" />
<xsl:template match="block">
<xsl:value-of select="slot"/>
<xsl:text>|</xsl:text>
<xsl:value-of select="state"/>
<xsl:text>|</xsl:text>
<xsl:value-of select="colour"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
node1A|Online|purple
B|Online|yellow
C|Online|red
D|Online|blue
this is an arbitrary bit of text included for this example
However, the output includes both the "name" and the "label"...
I want only what I'm explicitly asking for in the XSL:
A|Online|purple
B|Online|yellow
C|Online|red
D|Online|blue
I don't understand why. Can someone explain please?
Also, there may be multiple "name" elements, each with its own number of "block" elements.
Many thanks in advance

The elements outside the <block> are being processed using the default template rules. To prevent this you need to add
<xsl:template match="/">
<xsl:apply-templates select="block"/>
</xsl:template>
Then you don't need the template rules that match text nodes, because you never apply-templates to text nodes.

Just remove xsl:value-of from your first xsl:template. You get "name" and "label" contents because of it: it takes any text node and outputs its content. Moreover you don't need checking conditions on text nodes, leave one xsl:template for them with empty body:
<xsl:template match="*/text()"/>

Related

XSL predicate increase for each iteration of a for-each

Quick Question: Is there a way to increment the predicate of an XPATH, by using a variable, like itereating through an array in C? For example /XPATH/element[i]
I am trying to use an XSL to access data from an XML using XPATHS. The XML is the output of a database where the parent node is the table name and its children are the columns. The XSL must be able to convert the text value of the children into attributes with the column name of the element of the table name.
The problem I am trying to solve is that each table can have multiple rows which is outputted to the XML as sibling nodes with the same names. There could be infinite rows in any table so I am trying to use a for-each with the XPATH of the table name to process each row. This works but when I try to use the document function with the XPATH with a predicate to the first XPATH and then the next XPATH and then the next, I do not know how to do it. I can only access the first XPATH. I want a way to be able to access the next XPATH on each iteration of the for-each. Is there anything which can increment each loop and that the predicate and use to point to the next XPATH?
The XML code below is a sample which I am using for testing, it is called DB.xml:
<?xml version="1.0"?>
<dataset>
<rtbp>
<cfmtype>dog</cfmtype>
<cfmid>1</cfmid>
</rtbp>
<rtbp>
<cfmtype>cat</cfmtype>
<cfmid>2</cfmid>
</rtbp>
<FunctionSet>
<FUNCTIONSET__IDENTIFIER>1</FUNCTIONSET__IDENTIFIER>
<RTBP__CFMID>1</RTBP__CFMID>
</FunctionSet>
<FunctionSet>
<FUNCTIONSET__IDENTIFIER>2</FUNCTIONSET__IDENTIFIER>
<RTBP__CFMID>2</RTBP__CFMID>
</FunctionSet>
</dataset>
Below is the XSL I am using:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="dataset/rtbp">
<xsl:element name="RTBP">
<xsl:attribute name="CFMtype">
<xsl:value-of select="document('DB.xml')/dataset/rtbp[1]/cfmtype" />
</xsl:attribute>
<xsl:attribute name="CFMid">
<xsl:value-of select="document('DB.xml')/dataset/rtbp[1]/cfmid" />
</xsl:attribute>
<xsl:text>
</xsl:text>
<xsl:for-each select="/dataset/FunctionSet">
<xsl:element name="FunctionSet">
<xsl:attribute name="RTBP__CFMid">
<xsl:value-of select="document('DB.xml')/dataset/FunctionSet[1]/FUNCTIONSET__IDENTIFIER" />
</xsl:attribute>
<xsl:attribute name="RTBP_FunctionSet">
<xsl:value-of select="document('DB.xml')/dataset/FunctionSet[1]/RTBP__CFMID" />
</xsl:attribute>
</xsl:element>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:element>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The predicates are set to 1 at the moment but I wish it to be a variable which iterates on each loop so the XPATH changes to the next occurence of the table name.
The expected result is below:
<?xml version="1.0"?>
<RTBP CFMtype="dog" CFMid="1">
<FunctionSet RTBP__CFMid="1" RTBP_FunctionSet="1"/>
</RTBP>
<RTBP CFMtype="cat" CFMid="2">
<FunctionSet RTBP__CFMid="2" RTBP_FunctionSet="2"/>
</RTBP>
As you may be able to tell the second table (FunctionSet) is a child of the first (RTBP) hence the for-each inside the for-each. I need a method that will put the first row of the FunctionSet into the First row of the RTBP and likewise for the second rows.
I am new to XML, XSL and Posting questions.
The purpose is to re-create a hierarchical XML from a flat XML
exported from a database using DBunit. The association could be done
by cmfid
You should definitely use a key based on matching the cfmid value - especially if you are expecting a large number of rows. Try:
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:key name="func" match="FunctionSet" use="RTBP__CFMID" />
<xsl:template match="/">
<root>
<xsl:for-each select="dataset/rtbp">
<RTBP CFMtype="{cfmtype}" CFMid="{cfmid}">
<xsl:for-each select="key('func', cfmid)">
<FunctionSet RTBP__CFMid="{RTBP__CFMID}" RTBP_FunctionSet="{FUNCTIONSET__IDENTIFIER}"/>
</xsl:for-each>
</RTBP>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
When the above is applied to the following test input:
<?xml version="1.0"?>
<dataset>
<rtbp>
<cfmtype>dog</cfmtype>
<cfmid>124</cfmid>
</rtbp>
<rtbp>
<cfmtype>cat</cfmtype>
<cfmid>256</cfmid>
</rtbp>
<FunctionSet>
<FUNCTIONSET__IDENTIFIER>Canine</FUNCTIONSET__IDENTIFIER>
<RTBP__CFMID>124</RTBP__CFMID>
</FunctionSet>
<FunctionSet>
<FUNCTIONSET__IDENTIFIER>Feline</FUNCTIONSET__IDENTIFIER>
<RTBP__CFMID>256</RTBP__CFMID>
</FunctionSet>
<FunctionSet>
<FUNCTIONSET__IDENTIFIER>Hound</FUNCTIONSET__IDENTIFIER>
<RTBP__CFMID>124</RTBP__CFMID>
</FunctionSet>
</dataset>
the result is:
<?xml version="1.0" encoding="utf-8"?>
<root>
<RTBP CFMtype="dog" CFMid="124">
<FunctionSet RTBP__CFMid="124" RTBP_FunctionSet="Canine"/>
<FunctionSet RTBP__CFMid="124" RTBP_FunctionSet="Hound"/>
</RTBP>
<RTBP CFMtype="cat" CFMid="256">
<FunctionSet RTBP__CFMid="256" RTBP_FunctionSet="Feline"/>
</RTBP>
</root>
Note that your requested output format needlessly duplicates the cfmid value in both parent and child.
I think you're looking for something like (updated after quetion update) :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="rtbp">
<xsl:copy>
<xsl:for-each select="*">
<xsl:attribute name="{local-name()}" select="."/>
</xsl:for-each>
<xsl:apply-templates
select="//FunctionSet[RTBP__CFMID = current()/cfmid]"
mode="insertFunctionSet"/>
</xsl:copy>
</xsl:template>
<xsl:template match="FunctionSet"/>
<xsl:template match="FunctionSet" mode="insertFunctionSet">
<xsl:copy>
<xsl:for-each select="*">
<xsl:attribute name="{local-name()}" select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The idea, here, is to handle differently the element FunctionSet in the context of rtbp element.
It should not be part of the output when you recursively loop over the whole tree (that's the goal of the <xsl:template match="FunctionSet"/>).
But it should be handled inside the rtbp element and so we apply the templates on the relevant FunctionSet in a specific mode at this point. That's the goal of the <xsl:template match="FunctionSet" mode="insertFunctionSet">...</xsl:template>
With your input:
<?xml version="1.0"?>
<dataset>
<rtbp>
<cfmtype>dog</cfmtype>
<cfmid>1</cfmid>
</rtbp>
<rtbp>
<cfmtype>cat</cfmtype>
<cfmid>2</cfmid>
</rtbp>
<FunctionSet>
<FUNCTIONSET__IDENTIFIER>1</FUNCTIONSET__IDENTIFIER>
<RTBP__CFMID>1</RTBP__CFMID>
</FunctionSet>
<FunctionSet>
<FUNCTIONSET__IDENTIFIER>2</FUNCTIONSET__IDENTIFIER>
<RTBP__CFMID>2</RTBP__CFMID>
</FunctionSet>
</dataset>
The result is:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<rtbp cfmtype="dog" cfmid="1">
<FunctionSet FUNCTIONSET__IDENTIFIER="1" RTBP__CFMID="1"/>
</rtbp>
<rtbp cfmtype="cat" cfmid="2">
<FunctionSet FUNCTIONSET__IDENTIFIER="2" RTBP__CFMID="2"/>
</rtbp>
</dataset>
For anyone who had as little knowledge as me when I posted this question and whom wish to find out the same infomation here is my solution to the question. Short answer to the quick question 'can you increment a variable'. No! But you can set a variable and move the position with the following snippet:
<xsl:for-each select="/dataset/rtbp">
<xsl:variable name="i" select="position()" />
</xsl:for-each>
This snippet loops through rtbp tables in the source XML and moves the position one position more each loop. This creates an object which you can use inside a XPath to test for a condition of each occurence of the Xpath with the same URI path. Such as:
<xsl:for-each select="/dataset/rtbp">
<xsl:variable name="i" select="position()" />
<xsl:if test="/dataset/FunctionSet[$i]/cfmid = /dataset/rtbp[$n]/cfmid">
<!--code if condition is true-->
</xsl:for-each>
The [$variable name] is how you direct an XPath to the occurence of the element name. So when i = 1 it looks for the first occurence of the element name in the XPath and then when i = 2 it looks for the second occurence of the element name in the XPath.
The Key function is a good tool to search for a key condition inside a template. However I can only use 1 key function per a template. if you wish to have a multi condition test you have to use a choose when statement with multiple if statements being anded with eeach other. for example:
This is a snippet from my advanced code which has multiple for-each loops inside each other and choose when statements to decide if a XML element is a child of the parent via its Identifiers which are child elements of the parent elements in the example XML in my question.
With the position function and the XPath predicate entry combined with choose when statements with ands you can build up a complex XSL which can re-create a flat XML table list of a database into a hierarchical XML form.
Vincent's Key function answer worked for the small complexity of this question but this answer includes an answer about the XPath predicates so I think it is more relevant of an answer to the question. Please look at Vincent's answer and consider using Key Functions for your solution because it is very useful

XSLT - Getting data from a similarly named sibling using data from the first

I am an XSLT newb whose learning by doing. I've managed to do some easier choose when, for each loops but don't understand what is required to do my next aim.
Here is a sample of the input xml.
What I am looking to do is where the of is Board, look at the value of (in the example below its B). Since B means bottom I then want to look at the sibling component where the value of is EdgeBottom, and return the value of from this sibling component.
Note that could be TBLR or any combination of those options, and I want to pull the Material details from each corresponding Component.
I will be outputting into a table but once I get the idea of how it can be done I can muck about this with. Please excuse any terminology errors above, and the lack of any non-working code examples. Many thanks.
<Report schema="1.0">
<Item id="74" name="cabinet">
<VSection id="0" vsection="main">
<HSection id="3">
<Component id="2" idfull="07400302">
<DisplayName>EdgeBottom</DisplayName>
<Category>Edging</Category>
<Brand>Edging</Brand>
<Color>Edging</Color>
<Material>0.4mm Edging</Material>
</Component>
<Component id="1" idfull="07400301">
<DisplayName>Board</DisplayName>
<Category>Carcass</Category>
<Brand>Laminate</Brand>
<Color>White</Color>
<Material>16White</Material>
<Edging>B</Edging>
<IsEdgedOnTop>No</IsEdgedOnTop>
<IsEdgedOnRight>No</IsEdgedOnRight>
<IsEdgedOnBottom>Yes</IsEdgedOnBottom>
<IsEdgedOnLeft>No</IsEdgedOnLeft>
<EdgeMatTop>0.4mm</EdgeMatTop>
<EdgeMatRight>0.4mm</EdgeMatRight>
<EdgeMatBottom>0.4mm</EdgeMatBottom>
<EdgeMatLeft>0.4mm</EdgeMatLeft>
</Component>
</HSection>
</VSection>
</Item>
<DocumentProperties>
</DocumentProperties>
</Report>
Assuming you were positioned on a 'Board' component....
<xsl:template match="Component[DisplayName='Board']">
Then you could use the preceding-sibling axis to get the Component element using a series of xsl:if conditions, like so:
<xsl:if test="contains(Edging, 'B')">
<xsl:apply-templates select="preceding-sibling::Component[DisplayName='EdgeBottom']"/>
</xsl:if>
<xsl:if test="contains(Edging, 'T')">
<xsl:apply-templates select="preceding-sibling::Component[DisplayName='EdgeTop']"/>
</xsl:if>
Then you could just have a template where you output the value for the 'Edging' component. For example
<xsl:template match="Component[Category='Edging']">
<edging>
<xsl:value-of select="Material" />
</edging>
</xsl:template>
If you wanted to simplify things, the xsl:if conditions could be combined into one single statement. If the possible values of the edging are indeed "EdgeTop", "EdgeBottom", "EdgeLeft" and "EdgeRight" then the fifth character of each of these are obviously "T", "B", "L" and "R" and so can be checked directly against the current component, like so...
<xsl:apply-templates
select="preceding-sibling::Component
[contains(current()/Edging, substring(DisplayName, 5, 1))]"/>
Here is a sample XSLT which demonstrates this in action...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//Component[DisplayName='Board']" />
</xsl:template>
<xsl:template match="Component[DisplayName='Board']">
<xsl:apply-templates select="preceding-sibling::Component[contains(current()/Edging, substring(DisplayName, 5, 1))]"/>
</xsl:template>
<xsl:template match="Component[Category='Edging']">
<edging>
<xsl:value-of select="Material" />
</edging>
</xsl:template>
</xsl:stylesheet>
Note that if the Component element could be a following sibling, not just a preceding one, then try this expression instead:
<xsl:apply-templates
select="../Component
[Category='Edging']
[contains(current()/Edging, substring(DisplayName, 5, 1))]"/>

Using XSLT 2.0 to parse the values of multiple attributes into an array-like structure

I'd like to be able to select all the attributes of a certain type in a document (for example, //#arch) and then take that node set and parse the values out into second node set. When I say "parse", in specific I mean I want to turn a node set like this:
arch="value1;value2;value3"
arch="value1:value4"
into a node set like this:
arch="value1"
arch="value2"
arch="value3"
arch="value1"
arch="value4"
or something like that; I want to get the individual values out of the attributes and into their own node.
If I can get it to that state, I've got plenty of methods for sorting and duplicate removal, after which I'd be using the finished node set for a publishing task.
I'm not so much looking for an tidy answer here as an approach. I know that XSLT cannot do dynamic arrays, but that's not the same as not being able to do something like dynamic arrays or something that mimics the important part of the functionality.
One thought that has occurred to me is that I could count the nodes in the first node set, and the number of delimiters, calculate the number of entries that the second node set would need and create it (somehow), and use the substring functions to parse out the first node set into the second node set.
There's usually a way working around XSLT's issues; has anyone worked their way around this one before?
Thanks for any help,
Jeff.
I think what you're looking for is a sequence. A sequence can be either nodes or atomic values (see http://www.w3.org/TR/xslt20/#constructing-sequences).
Here's an example showing the construction of a sequence and then iterating over it. The sequence is the atomic values from #arch, but it could also be nodes.
XML Input
<doc>
<foo arch="value1;value2;value3"/>
<foo arch="value1:value4"/>
</doc>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="archSequence" as="item()*">
<xsl:for-each select="//#arch">
<xsl:for-each select="tokenize(.,'[;:]')">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/*">
<sequence>
<xsl:for-each select="$archSequence">
<item><xsl:value-of select="."/></item>
</xsl:for-each>
</sequence>
</xsl:template>
</xsl:stylesheet>
XML Output
<sequence>
<item>value1</item>
<item>value2</item>
<item>value3</item>
<item>value1</item>
<item>value4</item>
</sequence>
Example of a sequence of elements (same output):
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="archSequence" as="element()*">
<xsl:for-each select="//#arch">
<xsl:for-each select="tokenize(.,'[;:]')">
<item><xsl:value-of select="."/></item>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/*">
<sequence>
<xsl:for-each select="$archSequence">
<xsl:copy-of select="."/>
</xsl:for-each>
</sequence>
</xsl:template>
</xsl:stylesheet>
You can use the tokenize function in a for expression to get a sequence of the separate values, then create an attribute node for each one. However, since XSLT doesn't let you create a bare attribute node with no element parent, you'll have to use a trick like this:
<xsl:variable name="archElements">
<xsl:for-each select="for $attr in $initialNodeSet
return tokenize($attr, '[:;]')">
<dummy arch="{.}" />
</xsl:for-each>
</xsl:variable>
and then $archElements/dummy/#arch should be the set of separated arch attribute nodes that you require.
Complete example:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes" />
<xsl:template match="/">
<xsl:variable name="inputData">
<a arch="value1;value2;value3" />
<a arch="value1:value4" />
</xsl:variable>
<!-- create an example node set containing the two arch attribute nodes -->
<xsl:variable name="initialNodeSet" select="$inputData/a/#arch" />
<!-- tokenize and generate one arch attribute node for each value -->
<xsl:variable name="archElements">
<xsl:for-each select="for $attr in $initialNodeSet
return tokenize($attr, '[:;]')">
<dummy arch="{.}" />
</xsl:for-each>
</xsl:variable>
<!-- output to verify -->
<r>
<xsl:for-each select="$archElements/dummy/#arch">
<c><xsl:copy-of select="."/></c>
</xsl:for-each>
</r>
</xsl:template>
</xsl:stylesheet>
When run over any input document (the content is ignored) this produces
<?xml version="1.0" encoding="UTF-8"?>
<r>
<c arch="value1"/>
<c arch="value2"/>
<c arch="value3"/>
<c arch="value1"/>
<c arch="value4"/>
</r>

Xslt - How do you check for a grandchild node with a certain path name. (xpath 1.0)

What I want to do is given an element as context, I want to determine if it has a child with a given name and determine if that child has a node with a given name so I can do operations with it. It is important that I do this in XPath 1.0 syntax.
The code that I've gotten so far is this.
<xsl:for-each select="child::*">
<xsl:if test="contains(name(), 'description')">
<xsl:for-each select="child::*">
<xsl:if test="contains(name(), 'text')">
<xsl:value-of select="node()"/>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
It works, but it's big and ugly and I know that there's a way to condense it. The for-eachs there are unnecessary, since I'm only expecting one child node to be named description, and for it to only have one text node.
I feel like this solution should work
<xsl:for-each select="./description/text">
..
</xsl:for-each>
But it isn't, and I'm not really good enough with XPath Syntax to know why.
The reason I'm asking is because though I've found answers that detect whether a child node has a name, and I've found answers that can get to that child node's context, I haven't found an answer that combines the two, though maybe I just haven't been searching hard enough, in which case I apologize.
Edit: Woops, sorry yeah I forgot to mention that the contains() part of the code was also just a hack because I wasn't sure how to compare their values with equality.
Also as long as the answer is there, <xsl:for-each select="description/text"> does not work either.
A sample of the XML in question is this
<leaf>
<description>
<text> Various Words
</text>
</description>
</leaf>
where the context is the leaf and I am trying to get to the text node.
Edit: The Second Coming:
The problem for me was that my XSLT file was using a default namespace (in my case named a). If I had added that then Borodin's answer would have been correct.
To be specific, this is the code which ended up working for me in the end, in case anyone wants to know.
<xsl:for-each select="a:description/a:text>
<xsl:value-of select="node()"/>
</xsl:for-each>
Thanks Guys ^-^
Do you really want to check whether the element names contain those strings? Or, as your narrative says, do you want elements with that exact name?
To do something like what you have already written, use
<xsl:for-each select="*[contains(name(), 'description')]/*[contains(name(), 'text')]">
<xsl:value-of select="node()"/>
</xsl:for-each>
But if you know the complete names it is a lot neater:
<xsl:for-each select="description/text">
<xsl:value-of select="node()"/>
</xsl:for-each>
If that doesn't work then we need to see more of your source XML and your transform.
Update
If I use this XML
<leaf>
<description>
<text>Various Words</text>
</description>
<description>
<text>More Words</text>
</description>
<description>
<text>Other Words</text>
</description>
</leaf>
and apply this stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/leaf">
<xsl:for-each select="description/text">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
the output is the expected Various WordsMore WordsOther Words. I don't know how to help you unless you describe your situation better, except to say that transforms should be written with another template rather than for-each wherever possible. Like this variation which produces the same output as above.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/leaf">
<xsl:apply-templates select="description/text"/>
</xsl:template>
<xsl:template match="text">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>

Transform tag only if other kind of tag will follow

I'd like to amend an existing xsl file so that the tag <Empty> in the source document is only transformed if there will follow another sibling which is not this tag. A kind of truncate of these tags to the end.
Currently I have:
...
<xsl:template match="Expression">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="Empty">
<xsl:value-of select="."/>
</xsl:template>
Can this be achieved with xslt?
Sample input
<root>
<dummy1>Test1</dummy1>
<Empty>Empty1</Empty>
<Empty>Empty2</Empty>
<dummy2>Test2</dummy2>
<Empty>Empty3</Empty>
<Empty>Empty4</Empty>
<Empty>Empty5</Empty>
</root>
desired output:
Test1Empty1Empty2Test2
Please post a small input sample and the corresponding result you want to create with XSLT. There is a sibling axis in XPath so doing e.g.
<xsl:template match="Empty[following-sibling::*[1][not(self::Empty)]]">
<!-- now transform here as needed -->
</xsl:template>
matches any Empty elements followed by another element that is not an Empty element but I am not sure that is all you need and what you want to do inside of the template.
Assumed Input XML as:
<?xml version="1.0" encoding="utf-8"?>
<root>
<dummy1>test</dummy1>
<Empty>do-not-copy</Empty>
<Empty>copy-this1</Empty>
<dummy2>test</dummy2>
<Empty>do-not-copy-this2</Empty>
</root>
In the above XML .. 2nd element <Empty>do-not-copy</Empty> has immediate next sibling as <Empty>copy-this1</Empty> so it should not be selected. where as 3rd element is followed-by <dummy2/> so it should be copied ..
and 5th element is <Empty>copy-this2</Empty> it's not followed by any tag .. so it should be dropped as well :)
this is the XSL code for that:
<xsl:template match="Empty[following-sibling::*[1][name()!='Empty']]">
<xsl:value-of select="."/>
</xsl:template>
I am adding up a new answer ..
If this is your XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<dummy>test</dummy>
<Empty>copy-this</Empty>
<Empty>copy-this1</Empty>
<dummy>test</dummy>
<Empty>copy-this</Empty>
<Empty>copy-this1</Empty>
<dummy>test</dummy>
<Empty>donot-copy-this2</Empty>
<Empty>donot-copy-this2</Empty>
</root>
XSLT: sample code..
<xsl:template match="dummy">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="Empty[following-sibling::*[name()!='Empty']]">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
Output:
testcopy-thiscopy-this1testcopy-thiscopy-this1test
I modified the proposed solutions to the following:
<xsl:template match="Empty[following-sibling::*[not(self::Empty)]]">
<xsl:value-of select="."/>
</xsl:template>
which seems to work.