简体   繁体   中英

Compare attribute values through whole node-set

I have xml data. And parse it using xslt transformation. What I need is to find out if all child elements of particular element have same nested-element's values. Look:

if we have same values in :

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>1</compare>
   </child>
</parent>

we should copy all tree and set flag to "1":

<parent>
   ...
   </child>
   <flag>1</flag>
</parent>

If we have diffirent values:

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>2</compare>
   </child>
</parent>

we should copy all tree and set flag to "":

<parent>
   ...
   </child>
   <flag/>
</parent>

How about comparing if anything is different from the first?

<xsl:template match="/parent">
  <parent>
    <xsl:copy-of select="*"/>
    <flag>
      <xsl:if test="not(child[1]/compare != child/compare)">1</xsl:if>
    </flag>
  </parent>
</xsl:template>

this does mean if you only have one it will have a flag of 1

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes"/>

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

    <xsl:template match="parent">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
            <flag>
                <xsl:variable name="comp" select="child[1]/compare"/>
                <!-- add flag value only if child/compare are the same -->
                <xsl:value-of select="child[1]/compare[
                    count(current()/child)
                    = 
                    count(current()/child[ compare=$comp ]
                    )]"/>
            </flag>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

This transformation :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="/*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
       <flag><xsl:value-of select=
       "substring('1', 2 - not(child[compare != current()/child/compare]))"/></flag>
     </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

when applied on the following XML document:

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>1</compare>
   </child>
</parent>

produces the wanted, correct result:

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>1</compare>
   </child>
   <flag>1</flag>
</parent>

When the same transformation is applied on this XML document:

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>2</compare>
   </child>
</parent>

again the wanted, correct result is produced:

<parent>
   <child>
      <compare>1</compare>
   </child>
   <child>
      <compare>2</compare>
   </child>
   <flag/>
</parent>

Do note :

  1. Using and overriding the identity rule.

  2. No explicit conditional instructions (or variables) are used at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM