Reporting duplicate or non-duplicate nodes using XSL 1.0

两盒软妹~` 提交于 2019-12-11 22:11:26

问题


Using XSLT 1.0 I need to find if there are duplicate nodes or are there single nodes from the to and from categories.

I have found this XSL comparison of nodes that is very similar, but couldn't get it to work either.

XML:

    <acl>
      <enabled>true</enabled>
      <from>
        <rule>
          <name>1.1.1.1</name>
          <pattern>1.1.1.1</pattern>
          <allow/>
          <log>true</log>
          <alarm>0</alarm>
        </rule>
        <rule>
          <name>1.1.1.3</name>
          <pattern>1.1.1.3</pattern>
          <allow/>
          <log>false</log>
          <alarm>0</alarm>
        </rule>
        <rule>
          <name>1.1.1.4</name>
          <pattern>1.1.1.4</pattern>
          <deny/>
          <log>true</log>
          <alarm>0</alarm>
        </rule>
        <fallthrough>
          <allow/>
          <log>true</log>
          <alarm>0</alarm>
        </fallthrough>
      </from>
      <to>
        <rule>
          <name>1.1.1.2</name>
          <pattern>1.1.1.2</pattern>
          <allow/>
          <log>true</log>
          <alarm>0</alarm>
        </rule>
        <rule>
          <name>1.1.1.3</name>
          <pattern>1.1.1.3</pattern>
          <allow/>
          <log>false</log>
          <alarm>0</alarm>
        </rule>
        <rule>
          <name>1.1.1.4</name>
          <pattern>1.1.1.4</pattern>
          <deny/>
          <log>true</log>
          <alarm>0</alarm>
        </rule>
        <fallthrough>
          <allow/>
          <log>false</log>
          <alarm>0</alarm>
        </fallthrough>
      </to>
    </acl>

Desired Result:

1.1.1.1 would display "from" (it came from the from section)

1.1.1.3 would display "both" (it exists from both the from and to section)

1.1.1.2 would display "to" (it came from the to section)

I have already figured out how to write the 1.1.1.4 as "none" since as long as it shows <deny/>, I can print it as "none". But I have no idea how to test if they exist in both sections of the XPath or just one section.

What I have started on but non working.

XSL: (non working)

    <xsl:apply-templates select="acl">
        <xsl:if test="from/rule/name = to/rule/name">
            <xsl:text>both</xsl:text>
        </xsl:if>
    </xsl:apply-templates>

I realize that there are other posts out there that will find copy xml and match it and remove if two nodes are exactly the same. But I'm not trying to remove it, rather trying to report if there are duplicate nodes and where they came from.


回答1:


Would something like this work for you?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="from" match="from/rule" use="name" />
<xsl:key name="to" match="to/rule" use="name" />

<xsl:template match="/acl">
<result>
    <group desc="from">
        <xsl:apply-templates select="from/rule[not(key('to', name))]"/>
    </group>
    <group desc="to">
        <xsl:apply-templates select="to/rule[not(key('from', name))]"/>
    </group>
    <group desc="both">
        <xsl:apply-templates select="from/rule[key('to', name)]"/>
    </group>
</result>
</xsl:template>

<xsl:template match="rule">
    <xsl:copy-of select="." />
</xsl:template>

</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="utf-8"?>
<result>
<group desc="from">
<rule>
          <name>1.1.1.1</name>
          <pattern>1.1.1.1</pattern>
          <allow/>
          <log>true</log>
          <alarm>0</alarm>
        </rule>
</group>
<group desc="to">
<rule>
          <name>1.1.1.2</name>
          <pattern>1.1.1.2</pattern>
          <allow/>
          <log>true</log>
          <alarm>0</alarm>
        </rule>
</group>
<group desc="both">
<rule>
          <name>1.1.1.3</name>
          <pattern>1.1.1.3</pattern>
          <allow/>
          <log>false</log>
          <alarm>0</alarm>
        </rule>
<rule>
          <name>1.1.1.4</name>
          <pattern>1.1.1.4</pattern>
          <deny/>
          <log>true</log>
          <alarm>0</alarm>
        </rule>
</group>
</result>

EDIT

Essentially the same thing, with HTML output:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="from" match="from/rule" use="name" />
<xsl:key name="to" match="to/rule" use="name" />

<xsl:template match="/acl">
    <h3>From</h3>
    <xsl:apply-templates select="from/rule[not(key('to', name))]"/>
    <h3>To</h3>
    <xsl:apply-templates select="to/rule[not(key('from', name))]"/>
    <h3>Both</h3>
    <xsl:apply-templates select="from/rule[key('to', name)]"/>
</xsl:template>

<xsl:template match="rule">
    <p><xsl:value-of select="name" /></p>
</xsl:template>

</xsl:stylesheet>

Result:

<h3>From</h3>
<p>1.1.1.1</p>
<h3>To</h3>
<p>1.1.1.2</p>
<h3>Both</h3>
<p>1.1.1.3</p>
<p>1.1.1.4</p>

Rendered as:

From

1.1.1.1

To

1.1.1.2

Both

1.1.1.3

1.1.1.4




回答2:


Here's another option that copies the document as is and only adds an attribute to the rules in situ:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="from" match="from/rule" use="name" />
<xsl:key name="to" match="to/rule" use="name" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="rule">
    <xsl:copy>
        <xsl:attribute name="group">
            <xsl:choose>
                <xsl:when test="parent::from and not(key('to', name))">
                    <xsl:text>from</xsl:text>
                </xsl:when>
                <xsl:when test="parent::to and not(key('from', name))">
                    <xsl:text>to</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>both</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="utf-8"?>
<acl>
  <enabled>true</enabled>
  <from>
    <rule group="from">
      <name>1.1.1.1</name>
      <pattern>1.1.1.1</pattern>
      <allow/>
      <log>true</log>
      <alarm>0</alarm>
    </rule>
    <rule group="both">
      <name>1.1.1.3</name>
      <pattern>1.1.1.3</pattern>
      <allow/>
      <log>false</log>
      <alarm>0</alarm>
    </rule>
    <rule group="both">
      <name>1.1.1.4</name>
      <pattern>1.1.1.4</pattern>
      <deny/>
      <log>true</log>
      <alarm>0</alarm>
    </rule>
    <fallthrough>
      <allow/>
      <log>true</log>
      <alarm>0</alarm>
    </fallthrough>
  </from>
  <to>
    <rule group="to">
      <name>1.1.1.2</name>
      <pattern>1.1.1.2</pattern>
      <allow/>
      <log>true</log>
      <alarm>0</alarm>
    </rule>
    <rule group="both">
      <name>1.1.1.3</name>
      <pattern>1.1.1.3</pattern>
      <allow/>
      <log>false</log>
      <alarm>0</alarm>
    </rule>
    <rule group="both">
      <name>1.1.1.4</name>
      <pattern>1.1.1.4</pattern>
      <deny/>
      <log>true</log>
      <alarm>0</alarm>
    </rule>
    <fallthrough>
      <allow/>
      <log>false</log>
      <alarm>0</alarm>
    </fallthrough>
  </to>
</acl>


来源:https://stackoverflow.com/questions/21710665/reporting-duplicate-or-non-duplicate-nodes-using-xsl-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!