tesmc
asked on
XSL: How do I use "and" operator to compare a position and if node exist
How do I use "and" operator to compare a position and if node exist
For example I have following input
<Compl>
<person Type="ONE">
<Name>
<Fname>SMITH</Fname>
<Lname>JOHN</Lname>
</Name>
<misc Type="TWO>
<Fname>SMITH</Fname>
<Lname>JAkE</Lname>
</misc>
</person>
</Compl>
And I want output to be ONE/TWO
I have following code
<xsl:for-each select="Compl/person">
<xsl:value-of select="./@Type"/>
<xsl:if test="position() != last() and misc">
<xsl:value-of select="'/'"/>
</xsl:if>
</xsl:for-each>
For example I have following input
<Compl>
<person Type="ONE">
<Name>
<Fname>SMITH</Fname>
<Lname>JOHN</Lname>
</Name>
<misc Type="TWO>
<Fname>SMITH</Fname>
<Lname>JAkE</Lname>
</misc>
</person>
</Compl>
And I want output to be ONE/TWO
I have following code
<xsl:for-each select="Compl/person">
<xsl:value-of select="./@Type"/>
<xsl:if test="position() != last() and misc">
<xsl:value-of select="'/'"/>
</xsl:if>
</xsl:for-each>
That is exacxtly how you need to use "and"
But I think you want more than that
you need a
<xsl:value-of select="misc/@Type"/>
too
for the "Two"
But I think you want more than that
you need a
<xsl:value-of select="misc/@Type"/>
too
for the "Two"
Maybe you were thinking in terms of
is making the Type attributes the selected node-set
<xsl:for-each select="Compl/person/@Type | Compl/person/misc/@Type">
<xsl:if test="not(position() = 1)">
<xsl:text>/</xsl:text>
</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
is making the Type attributes the selected node-set
ASKER
i need the position() in case there are more <person> then i want to seperate the @type with "/"
ASKER
in this example, i'd want CCC/AAA/III/AAA
<person Type="CCC">
<Name>
<Fname>TEMP</Fname>
<Lname>ANYTHING</Lname>
</Name>
</person>
<person Type="AAA">
<Name>
<Fname>TEMP</Fname>
<Lname>ANYTHING</Lname>
</Name>
<misc Type="III">
<Fname>TEMP</Fname>
<Lname>ANYTHING</Lname>
</misc>
</person>
<person Type="AAA">
<Name>
<Fname>TEMP</Fname>
<Lname>ANYTHING</Lname>
</Name>
</person>
<person Type="CCC">
<Name>
<Fname>TEMP</Fname>
<Lname>ANYTHING</Lname>
</Name>
</person>
<person Type="AAA">
<Name>
<Fname>TEMP</Fname>
<Lname>ANYTHING</Lname>
</Name>
<misc Type="III">
<Fname>TEMP</Fname>
<Lname>ANYTHING</Lname>
</misc>
</person>
<person Type="AAA">
<Name>
<Fname>TEMP</Fname>
<Lname>ANYTHING</Lname>
</Name>
</person>
OK, but you have the "two" in the misc/@Type inside Person,
that is what caused my comment
So you will need a "/" between Types inside the same person and between persons,
hence my suggestion to collect the nodest of wanted @Type attributes
that is what caused my comment
So you will need a "/" between Types inside the same person and between persons,
hence my suggestion to collect the nodest of wanted @Type attributes
OK, I did not see your example before posting.
But the approach with the @Type nodeset will give you that
But the approach with the @Type nodeset will give you that
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx. that worked.
welcome
Here's an example with the following improvements:
1) Use templates instead of for-each
2) Use <xsl:text> instead of <xsl:value-of select="'/'"/>
I'm not sure, what's your goal besides printing out ONE/TWO, so I've removed that "position() != last()" test.
Open in new window