Reputation: 69
I try to retrieve a list of the attribute values of the children of an element but I want that the values only appears once.
For instance, 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 a result like that:
<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've tried to use the xsl:key element but it only returns the value for one element. In the example, it only returns 1.1 for the first title but not the second. The xsl I've 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>
Upvotes: 3
Views: 158
Reputation: 243459
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 on 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 wanted, 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 groupings as one, by dynamically constructing the name of the key for the actual grouping to be performed.
Remember: The key name is a string and when necessary (as in this case), the name can be dynamically constructed, or passed as a parameter.
Upvotes: 2