Jump to content

How to make conditional statements of attributes with templates in XSLT


Recommended Posts

How can I get the value of an attribute and use it in a conditional statement. For example I have <O Option="a" LineNr="1"> and this is element is a child under another parent element that gets grouped with other elements. So the condition I want is: if [value-of Option is null] then [do nothing] else-if [check if the value of the attribute of the element under the next parent element with the same value is empty]. And if there are for example 3 parent elements that contain this element with the Option attribute and if all of the attributes are NOT empty - then group them. I have the grouping done, but it is not connected with this attribute that I want to use now. So I need just the formulation of the conditional statements.

edit: giving input and output desired examples

Input example 1:

<Area><Type>A</Type><Street><Position>5</Position><House><L Option="" LineNr="1">--> some elements</L></House></Street><Street><Position>5</Position><Block><O Option="" LineNr="1">--> some elements</O></Block></Street><Street><Position>6</Position><House><L Option="o" LineNr="1">--> some elements</L></House></Street><Street><Position>6</Position><Block><O Option="" LineNr="1">--> some elements</O></Block></Street></Area>

And desired output:

<Area><Type>A</Type><Street><Position>5</Position><House><L Option="" LineNr="1">--> some elements</L></House><Block><O Option="" LineNr="1">--> some elements</O></Block></Street><Street><Position>6</Position><House><L Option="o" LineNr="1">--> some elements</L></House><Block><O Option="" LineNr="1">--> some elements</O></Block></Street></Area>

But if in the same position number, all the occurrences have a value in the Option attribute of L or O, then I want them arranged under the other occurrence of a position number. Like that:

Input example 2:

<Area><Type>A</Type><Street><Position>5</Position><House><L Option="" LineNr="1">--> some elements</L></House></Street><Street><Position>5</Position><Block><O Option="" LineNr="1">--> some elements</O></Block></Street><Street><Position>6</Position><House><L Option="o" LineNr="1">--> some elements</L></House></Street><Street><Position>6</Position><Block><O Option="a" LineNr="1">--> some elements</O></Block></Street></Area>

Then the output will be:

<Area><Type>A</Type><Street><Position>5</Position><House><L Option="" LineNr="1">--> some elements</L></House><Block><O Option="" LineNr="1">--> some elements</O></Block><House><L Option="o" LineNr="1">--> some elements</L></House><Block><O Option="a" LineNr="1">--> some elements</O></Block></Street></Area>

as now position 6 is gone and its child elements are grouped under the upper occurrence position (which is 5 in this case in the same Type) as all of the attributes Option in the L or O elements had value (which can be either "a" or "o", if that is relevant).

And this is the XSLT which I am using now. It only groups the elements of the different occurrences of the same position numbers under one number:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:strip-space elements="*" /><xsl:output method="xml" indent="yes" /><xsl:key name="streetByPosition" match="Street" use="concat(../Type, '|', Position)" /><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy></xsl:template><!-- for the first Street in each Position --><xsl:template match="Street[generate-id() =generate-id(key('streetByPosition', concat(../Type, '|', Position))[1])]"><Street><!-- copy in the Position element once only --><xsl:apply-templates select="Position" /><!-- copy in all sub-elements except Position from all matching Streets--><xsl:apply-templates select="key('streetByPosition', concat(../Type, '|', Position))/*[not(self::Position)]" /></Street></xsl:template><!-- ignore all other Street elements --><xsl:template match="Street" /></xsl:stylesheet>

I really, really need solution for that and I have no clue how to do it, especially the checks.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...