i am new to XSLT ,can any one please suggest to me how to compare two elements coming from xml as string their values are:
<OU_NAME>Vision Operations</OU_NAME> --XML code
<OU_ADDR1>90 Fifth Avenue</OU_ADDR1> --XML code
<xsl:choose>
<xsl:when test="OU_NAME='OU_ADDR1'"> --comparing two elements coming from XML
<!--remove if adrees already contain operating unit name
<xsl:value-of select="OU_NAME"/> <fo:block/>-->
<xsl:if test="OU_ADDR1 !='' ">
<xsl:value-of select="OU_ADDR1"/>
<fo:block/>
</xsl:if>
<xsl:if test="LE_ADDR2 !='' ">
<xsl:value-of select="OU_ADDR2"/>
<fo:block/>
</xsl:if>
<xsl:if test="LE_ADDR3 !='' ">
<xsl:value-of select="OU_ADDR3"/>
<fo:block/>
</xsl:if>
<xsl:if test="OU_TOWN_CITY !=''">
<xsl:value-of select="OU_TOWN_CITY"/>,
<fo:leader leader-pattern="space" leader-length="2.0pt"/>
</xsl:if>
<xsl:value-of select="OU_REGION2"/>
<fo:leader leader-pattern="space" leader-length="3.0pt"/>
<xsl:value-of select="OU_POSTALCODE"/>
<fo:block/>
<xsl:value-of select="OU_COUNTRY"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="OU_NAME"/>
<fo:block/>
<xsl:if test="OU_ADDR1 !='' ">
<xsl:value-of select="OU_ADDR1"/>
<fo:block/>
</xsl:if>
<xsl:if test="LE_ADDR2 !='' ">
<xsl:value-of select="OU_ADDR2"/>
<fo:block/>
</xsl:if>
<xsl:if test="LE_ADDR3 !='' ">
<xsl:value-of select="OU_ADDR3"/>
<fo:block/>
</xsl:if>
<xsl:if test="OU_TOWN_CITY !=''">
<xsl:value-of select="OU_TOWN_CITY"/>,
<fo:leader leader-pattern="space" leader-length="2.0pt"/>
</xsl:if>
<xsl:value-of select="OU_REGION2"/>
<fo:leader leader-pattern="space" leader-length="3.0pt"/>
<xsl:value-of select="OU_POSTALCODE"/>
<fo:block/>
<xsl:value-of select="OU_COUNTRY"/>
</xsl:otherwise>
</xsl:choose>
First of all, the provided long code:
is equivalent to this, much shorter code:
Now, to your question:
In Xpath 1.0 strings can be compared only for equality (or inequality), using the operator
=
and the functionnot()
together with the operator=
.evaluates to
true()
exactly when the string$str1
is equal to the string$str2
.evaluates to
true()
exactly when the string$str1
is not equal to the string$str2
.There is also the
!=
operator. It generally should be avoided because it has anomalous behavior whenever one of its operands is a node-set.Now, the rules for comparing two element nodes are similar:
evaluates to
true()
exactly when the string value of$el1
is equal to the string value of$el2
.evaluates to
true()
exactly when the string value of$el1
is not equal to the string value of$el2
.However, if one of the operands of
=
is a node-set, thenevaluates to
true()
exactly when there is at least one node in the node-set$ns1
, whose string value is equal to the string$str
evaluates to
true()
exactly when there is at least one node in the node-set$ns1
, whose string value is equal to the string value of some node from$ns2
Therefore, the expression:
evaluates to
true()
only when there is at least one element child of the current node that is namedOU_NAME
and whose string value is the string 'OU_ADDR1'.This is obviously not what you want!
Most probably you want:
This expression evaluates to
true
exactly there is at least oneOU_NAME
child of the current node and oneOU_ADDR1
child of the current node with the same string value.Finally, in XPath 2.0, strings can be compared also using the value comparison operators
lt
,le
,eq
,gt
,ge
and the inherited from XPath 1.0 general comparison operator=
.Trying to evaluate a value comparison operator when one or both of its arguments is a sequence of more than one item results in error.