A transform (http://stackoverflow.com/questions/12862902/how-to-do-xslt-muenchian-grouping-with-some-null-attributes/12871809#12871809) groups based on an attribute (@group) and that attribute being null. Initially this was for just concat-ing strings but I now need to extend it so it uses the string as a file location and concats the docs if they are grouped – if not grouped it should use the string as a file it gets but doesn’t need to join to anything else. The Transform:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="modules" match="module[@group]" use="concat(generate-id(..), '|', @group)"/>
<xsl:template match="root">
<AllSections>
<xsl:apply-templates />
</AllSections>
</xsl:template>
<!-- NON GROUPED PART -->
<xsl:template match="module[not(@group)]">
<page>
<content>
<xsl:value-of select="comp"/>
</content>
</page>
</xsl:template>
<!--GROUPED PART -->
<xsl:template match="module[@group][generate-id() = generate-id(key('modules', concat(generate-id(..), '|', @group))[1])]">
<xsl:variable name="modules" select="key('modules', concat(generate-id(..), '|', @group))"/>
<page>
<content>
<xsl:apply-templates select="$modules/comp/text()"/>
</content>
<count>
<xsl:value-of select="count($modules)" />
</count>
</page>
</xsl:template>
<xsl:template match="module"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
And Sample Input:
<root>
<section>
<subsection>
<module>
<comp>111</comp>
</module>
<module group='group01'>
<comp>222</comp>
</module>
<module group='group01'>
<comp>333</comp>
</module>
<module>
<comp>444</comp>
</module>
<module>
<comp>555</comp>
</module>
</subsection>
</section>
<section>
<subsection>
<module group ="group02">
<comp>666</comp>
</module>
<module group ="group02">
<comp>777</comp>
</module>
<module>
<comp>888</comp>
</module>
<module group ="group03">
<comp>999</comp>
</module>
<module group ="group03">
<comp>101010</comp>
</module>
</subsection>
<subsection>
<module group ="group04">
<comp>11111</comp>
</module>
<module group ="group04">
<comp>121212</comp>
</module>
<module group ="group05">
<comp>131313</comp>
</module>
<module group ="group05">
<comp>141414</comp>
</module>
<module group ="group06">
<comp>151515</comp>
</module>
<module group ="group06">
<comp>161616</comp>
</module>
<module>
<comp>171717</comp>
</module>
</subsection>
The transform at the moment concats the comp strings if they are of the same group… doing this for the non-grouped parts so the string is used as a file location:
<!-- NON GROUPED PART -->
<xsl:template match="module[not(@group)]">
<page>
<content>
<xsl:variable name="var">
<xsl:value-of select="comp"/>
</xsl:variable>
<xsl:copy-of select="document(concat('../myfile/', string($var)))"/>
</content>
</page>
</xsl:template>
At the moment the GROUPED PART will output:
…
<page>
<content>strgin1string2</content>
<count>2</count>
</page>
…
I need:
…
<page>
<content>
TEXT FROM FILE CALLED string1
TEXT FROM FILE CALLED string2
</content>
<count>2</count>
</page>
…
Thanks!
Assuming you simply want to pull in complete XML documents you can change your code to
[edit] I think my suggestion above works if the
compelements contain the complete URL. Your comment however suggests you want to concatenate a constant with eachcomp, in that case with XSLT 1.0 you needWith XSLT 2.0 you could write a compact line like
<xsl:copy-of select="document($modules/comp/(concat('../line/', .))"/>but with XSLT 1.0 the for-each should do.