简体   繁体   English

通过整个节点集比较属性值

[英]Compare attribute values through whole node-set

I have xml data. 我有xml数据。 And parse it using xslt transformation. 并使用xslt转换对其进行解析。 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": 我们应该复制所有树并将标志设置为“ 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 这确实意味着如果您只有一个,则其标志将为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: 当应用于以下XML文档时:

<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: 在此XML文档上应用相同的转换时:

<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. 根本不使用任何明确的条件指令(或变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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