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 station has its unique information. So By considering first operatorstation node as a master comparing all remaining slave operatorStation information. First need to compare IPAddress by grouping Family & Name which gives result like shown below. So I am looking for starnsform script XSLT which give me result shown below.
Please check xml file I am using as input, xml file as I am expecting output result and XSLT file I am working on it.
Please ask question if anyone still not understand....
Input xml file
<?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>
Expected OUTPUT:
<?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>
Script I am trying to add:
<?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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kOperatorStation" match="OperatorStation" use="concat(Family,'#',Name)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:for-each select="OperatorStation[generate-id() = generate-id(key('kOperatorStation',concat(Family,'#',Name))[1])]" >
<xsl:copy>
<xsl:variable name="group" select="key('kOperatorStation',concat(current()/Family,'#',current()/Name))" />
<xsl:for-each select= "$group" >
<xsl:if test="position() = 1">
<xsl:apply-templates select="*[local-name() != 'IPAddress']" />
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
c# code for transform:
XmlReader objXmlReader = XmlReader.Create(@"C:\XMLFile1.xml");
/*Transform and add arguments*/
XmlReader xsltTransformReader= XmlReader.Create(@"C:\XSLTFile1.xslt");
XslCompiledTransform objXslTransform = new XslCompiledTransform();
objXslTransform.Load(xsltTransformReader);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
/*Do transformation*/
StringBuilder output = new StringBuilder();
using (XmlWriter xw = XmlWriter.Create(new StringWriter(output), settings))
{
objXslTransform.Transform(objXmlReader, xw);
string result = output.ToString();
}
I think I have finally understood this question.
The following stylesheet will group the
OperatorStation
elements by theFamily
andName
elements located insideNodes/DataNodeBase
. Note that there is anotherName
element that is a direct child ofOperatorStation
- 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:
Source XML:
Result: