Choose distinctive values ​​in XSLT relative to each element

I am trying to get a list of attribute values ​​for the children of an element, but I want the values ​​to appear only once.

For example, I have the following XML

<root>
    <sec>
        <nom-title>
            <nom-chapter>
                <nom-article><data att="1.1"/></nom-article>
                <nom-article>
                    <nom-item><data att="1.1"/></nom-item>
                    <nom-item><data att="1.2"/></nom-item>
                </nom-article>
             </nom-chapter>
             <nom-chapter>
                <nom-article><data att="2.1"/></nom-article>
                <nom-article><data att="1.1"/></nom-article>
             </nom-chapter>
         </nom-title>
         <nom-title>
             <nom-chapter>
                 <nom-article><data att="1.1"/></nom-article>
             </nom-chapter>
         </nom-title>
     </sec>
 </root>

      

And I want to get a result like this:

<root>
    <nom-title>
        <att>1.1</att>
        <att>1.2</att>
        <att>2.1</att>
        <nom-chapter>
            <att>1.1</att>
            <att>1.2</att>
            <nom-article>
                <att>1.1</att>
            </nom-article>
            <nom-article>
                <att>1.1</att>
                <att>1.2</att>
                <nom-item><att>1.1</att></nom-item>
                <nom-item><att>1.2</att></nom-item>
            </nom-article>
         </nom-chapter>
    </nom-title>
    <nom-title>
         <att>1.1</att>
         <nom-chapter>
             <att>1.1</att>
             <nom-article>
                 <att>1.1</att>
             </nom-article>
         </nom-chapter>
     </nom-title>
</root>

      

I tried to use the xsl: key element, but it only returns a value for one element. In this example, it only returns 1.1 for the first title, not the second. The xsl I used:

 <xsl:key name="allAtt"
    match="//*[starts-with(name(.),'nom-')]/data"
    use="@att"/>
 <xsl:template match="nom-title|nom-chapter|nom-article|nom-item">
     <xsl:element name="name(.)">
         <xsl:apply-templates select=".//*[starts-with(name(.),'nom-')]/data
     </xsl:element>
 </xsl:template>        
 <xsl:template match="data">
      <xsl:variable name="att" select="@att"/>
      <xsl:if test="generate-id(.)=generate-id(key('allAtt',$att)[1]">
           <xsl:element name="att"><xsl:value-of select="$att"></xsl:element>
      </xsl:if>
 </xsl:template>

      

+3


source to share


1 answer


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:key name="kData-nom-article" match="data" use=
 "concat(generate-id(ancestor::nom-article[1]),
         '+', @att)"/>
 <xsl:key name="kData-nom-chapter" match="data" use=
 "concat(generate-id(ancestor::nom-chapter[1]),
         '+', @att)"/>
 <xsl:key name="kData-nom-title" match="data" use=
 "concat(generate-id(ancestor::nom-title[1]),
         '+', @att)"/>

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

 <xsl:template match="sec"><xsl:apply-templates/></xsl:template>

 <xsl:template match="nom-title|nom-article|nom-chapter">
  <xsl:copy>
    <xsl:apply-templates mode="list" select=
     ".//data[generate-id()
             =
              generate-id(key(concat('kData-', name(current())),
                              concat(generate-id(current()),
                                     '+', @att
                                    )
                              )
                                [1]
                          )
             ]"/>
    <xsl:apply-templates/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="data" mode="list">
  <att><xsl:value-of select="@att"/></att>
 </xsl:template>

 <xsl:template match="non-item/data">
  <att><xsl:value-of select="@att"/></att>
 </xsl:template>

 <xsl:template match="*[not(self::nom-item)]/data"/>
</xsl:stylesheet>

      

when applied to the provided XML document :

<root>
    <sec>
        <nom-title>
            <nom-chapter>
                <nom-article>
                    <data att="1.1"/>
                </nom-article>
                <nom-article>
                    <nom-item>
                        <data att="1.1"/>
                    </nom-item>
                    <nom-item>
                        <data att="1.2"/>
                    </nom-item>
                </nom-article>
            </nom-chapter>
            <nom-chapter>
                <nom-article>
                    <data att="2.1"/>
                </nom-article>
                <nom-article>
                    <data att="1.1"/>
                </nom-article>
            </nom-chapter>
        </nom-title>
        <nom-title>
            <nom-chapter>
                <nom-article>
                    <data att="1.1"/>
                </nom-article>
            </nom-chapter>
        </nom-title>
    </sec>
</root>

      



produces the desired, correct result:

<root>
   <nom-title>
      <att>1.1</att>
      <att>1.2</att>
      <att>2.1</att>
      <nom-chapter>
         <att>1.1</att>
         <att>1.2</att>
         <nom-article>
            <att>1.1</att>
         </nom-article>
         <nom-article>
            <att>1.1</att>
            <att>1.2</att>
            <nom-item>
               <data att="1.1"/>
            </nom-item>
            <nom-item>
               <data att="1.2"/>
            </nom-item>
         </nom-article>
      </nom-chapter>
      <nom-chapter>
         <att>2.1</att>
         <att>1.1</att>
         <nom-article>
            <att>2.1</att>
         </nom-article>
         <nom-article>
            <att>1.1</att>
         </nom-article>
      </nom-chapter>
   </nom-title>
   <nom-title>
      <att>1.1</att>
      <nom-chapter>
         <att>1.1</att>
         <nom-article>
            <att>1.1</att>
         </nom-article>
      </nom-chapter>
   </nom-title>
</root>

      

Explanation . Expressing three different Muenchian groups as one, by dynamically constructing a key name for the actual grouping to be performed.

Remember . The key name is a string and, when needed (as in this case), the name can be dynamically constructed or passed as a parameter.

+2


source







All Articles