How Can I compare ip address with reference of family and name. using XSLT file. In Details, I have one xml file which has list of operatorstation nodes with each operator stati
I think I have finally understood this question.
The following stylesheet will group the OperatorStation
elements by the Family
and Name
elements located inside Nodes/DataNodeBase
. Note that there is another Name
element that is a direct child of OperatorStation
- this is potentially confusing.
For each group, the first IPAddress
element is listed and the others are compared to it. The names of these result elements are taken from the values of the (top) Name
element (just to add some more confusion...).
Pay special attention to the handling of the namespace declared in the source XML document.
Styleheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://ACORD.org/Standards/Life/2"
exclude-result-prefixes="ns">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="kOperatorStation" match="ns:OperatorStation" use="concat(.//ns:Family, '#', .//ns:DataNodeBase/ns:Name)"/>
<xsl:template match="/">
<OperatorStationCollection>
<xsl:for-each select="ns:OperatorStationCollection/ns:OperatorStation[generate-id() = generate-id(key('kOperatorStation',concat(.//ns:Family, '#', .//ns:DataNodeBase/ns:Name))[1])]" >
<Result>
<Family><xsl:value-of select=".//ns:Family"/></Family>
<AdaptorName><xsl:value-of select=".//ns:DataNodeBase/ns:Name"/></AdaptorName>
<xsl:variable name="ip" select=".//ns:IPAddress" />
<xsl:for-each select="key('kOperatorStation', concat(.//ns:Family, '#', .//ns:DataNodeBase/ns:Name))" >
<xsl:element name="{ns:Name}">
<xsl:choose>
<xsl:when test="position()=1">
<xsl:value-of select="$ip"/>
</xsl:when>
<xsl:when test=".//ns:IPAddress=$ip">
<xsl:text>Equal</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>UnEqual</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:for-each>
</Result>
</xsl:for-each>
</OperatorStationCollection>
</xsl:template>
</xsl:stylesheet>
Source XML:
<?xml version="1.0" encoding="utf-8"?>
<OperatorStationCollection xmlns="http://ACORD.org/Standards/Life/2">
<OperatorStation>
<Name>OS01</Name>
<Nodes>
<DataNodeBase >
<Family>NetworkSettings</Family>
<Name>Internet</Name>
<IPAddress>111.22.22.3</IPAddress>
</DataNodeBase>
</Nodes>
</OperatorStation>
<OperatorStation>
<Name>OS02</Name>
<Nodes>
<DataNodeBase>
<Family>NetworkSettings</Family>
<Name>Internet</Name>
<IPAddress>111.22.22.3</IPAddress>
</DataNodeBase>
</Nodes>
</OperatorStation>
<OperatorStation>
<Name>OS03</Name>
<Nodes>
<DataNodeBase>
<Family>NetworkSettings</Family>
<Name>Internet</Name>
<IPAddress>111.22.22.4</IPAddress>
</DataNodeBase>
</Nodes>
</OperatorStation>
<OperatorStation>
<Name>OS04</Name>
<Nodes>
<DataNodeBase>
<Family>NetworkSettings</Family>
<Name>Internet</Name>
<IPAddress>111.22.22.4</IPAddress>
</DataNodeBase>
</Nodes>
</OperatorStation>
</OperatorStationCollection>
Result:
<?xml version="1.0" encoding="utf-8"?>
<OperatorStationCollection>
<Result>
<Family>NetworkSettings</Family>
<AdaptorName>Internet</AdaptorName>
<OS01>111.22.22.3</OS01>
<OS02>Equal</OS02>
<OS03>UnEqual</OS03>
<OS04>UnEqual</OS04>
</Result>
</OperatorStationCollection>