XSLT 节点值比较 [英] XSLT node value comparision

查看:20
本文介绍了XSLT 节点值比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将 IP 地址与家庭和姓名的参考进行比较.使用 XSLT 文件.在详细信息中,我有一个 xml 文件,其中包含操作员站节点列表,每个操作员站都有其独特的信息.因此,通过将第一个操作员站节点视为主站,比较所有剩余的从站操作员站信息.首先需要通过分组Family & 来比较IPAddress给出如下所示结果的名称.所以我正在寻找 starnsform 脚本 XSLT,它给了我如下所示的结果.请检查我用作输入的 xml 文件、我期待输出结果的 xml 文件和我正在处理的 XSLT 文件.
如果还有不懂的请追问....输入xml文件

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>

预期输出:

<?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>

我要添加的脚本:

<?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#代码:

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.

以下样式表将按位于 Nodes/DataNodeBase 内的 FamilyName 元素对 OperatorStation 元素进行分组.请注意,还有另一个 Name 元素是 OperatorStation 的直接子元素 - 这可能会造成混淆.

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.

对于每个组,列出第一个 IPAddress 元素,并将其他元素与其进行比较.这些结果元素的名称取自(顶部)Name 元素的值(只是为了增加一些混淆......).

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...).

要特别注意对源 XML 文档中声明的命名空间的处理.

Pay special attention to the handling of the namespace declared in the source XML document.

样式表:

<?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>

源 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>

结果:

<?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>

这篇关于XSLT 节点值比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆