Reputation: 2576
I have an input xml looking like this:
<vtext>
<myTag>Title</myTag>
</vtext>
<vtext>
<myTag> </myTag>
</vtext>
<vtext>
<myTag> </myTag>
</vtext>
<vtext>
<myTag>Some text here maybe</myTag>
</vtext>
<vtext>
<myTag> </myTag>
</vtext>
<vtext>
<myTag> </myTag>
</vtext>
<vtext>
<myTag> </myTag>
</vtext>
<vtext>
<myTag>Other text...</myTag>
</vtext>
The <vtext>
node always contains a single <myTag>
child node that could be empty. (In this example it is filled in with , but it can also be something like
<myTag\>
And what I'm trying to achieve is to have an output HTML that looks like this:
Title<br>
<br>
Some text here maybe<br>
<br>
Other text...
Basically, I want to replace the multiple empty <myTag>
nodes after each other with ONLY ONE<br>
tag. For this I am using a xsl transformation that needs an extra condition that I can not come up with at the moment (can't figure it out)...
What I have by now is this:
<xsl:for-each select="myTag">
<xsl:choose>
<xsl:when
test="normalize-space(current()) = '' and **SOME CONDITION INVOLVING preceding-sibling MAYBE??**>
<br />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="current()" />
<br />
</xsl:otherwise>
</xsl:choose>
Any ideas on what extra condition needs to go there?
Thanks
Upvotes: 1
Views: 1083
Reputation: 2576
It can also be done like this:
<xsl:template match="vtext">
<xsl:for-each select="myTag">
<xsl:choose>
<!-- If the current 'myTag' is empty and the previously one is not append an empty line -->
<xsl:when
test="string-length(normalize-space(current())) = 0
and not(string-length(normalize-space(../preceding-sibling::vtext[1]/myTag)) = 0)">
<br />
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<!-- If the current 'myTag' is not empty, add it to the existing document -->
<xsl:when test="not(string-length(normalize-space(current())) = 0)">
<xsl:value-of select="current()" />
<br />
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
Upvotes: 0
Reputation: 70618
As myTag
is a child of vtext
you probably want to change your xsl:for-each
to select vtext
elements rather than myTag
. Additionally you can then add a condition to select only the ones where myTag
are non-empty, or where the preceding one is non-empty
<xsl:for-each select="vtext[normalize-space(myTag) or normalize-space(preceding-sibling::vtext[1]/myTag)]">
So, you are capturing both conditions at once.
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:template match="/*">
<xsl:for-each select="vtext[normalize-space(myTag) or normalize-space(preceding-sibling::vtext[1]/myTag)]">
<xsl:value-of select="normalize-space(myTag)" />
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2