How can I group data after a copy-of merge with XSLT 1.0?

klas mack

I have a xml called paths.xml which can contain 1 to X number of filepaths to XMl-files that I need to merge into one for further processing.

I use the stylesheet below to do this, but now I need to group the data by a attribute in the outputted file in the same conversion. I have looked into muenchian grouping but cant figure out how to implement it in the same stylesheet that does the copying?

Each XML contains only tags for its group. The output I want to achieve is that each XML gets grouped under a new element with the groups attribute-name as its value.

Any ideas?

My stylesheet:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" 
            doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
            doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes" omit-xml-declaration="no" />

            <xsl:template match="/">

        <xsl:copy-of select="document(document('paths.xml')//file/path)/*/node()"/>

    </xsl:template>
</xsl:stylesheet>

Example of XML before merge:

<?xml version="1.0" encoding="UTF-8"?>
<tags generator="xmlgenerator" id="123">
  <tag group="group1">
    <title lang="eng">title1</title>
</tag>
  <tag group="group1">
    <title lang="se">title2</title>
</tag>
  <tag group="group1">
    <title lang="eng">title3</title>
</tag>
</tags>

Wanted output:

<?xml version="1.0" encoding="UTF-8"?>
<tagcollection>
<group1>
  <tag>
    <title lang="rus">title1</title>
</tag>
  <tag>
    <title lang="se">title2</title>
</tag>
</group1>
<group2>
  <tag>
    <title lang="eng">title1</title>
</tag>
  <tag>
    <title lang="se">title2</title>
</tag>
</group2>
</tagcollection>
Martin Honnen

Change

        <xsl:template match="/">

    <xsl:copy-of select="document(document('paths.xml')//file/path)/*/node()"/>

</xsl:template>

to

<xsl:template match="/">
  <tagcollection>
    <xsl:apply-templates select="document(document('paths.xml')//file/path)/*"/>
  </tagcollection>
</xsl:template>

and then add the key for Muenchian grouping

<xsl:key name="group" match="tag" use="@group"/>

and the templates for grouping along the lines of

<xsl:template match="/*">
  <xsl:apply-templates select="tag[generate-id() = generate-id(key('group', @group)[1])]" mode="group"/>
</xsl:template>

<xsl:template match="tag" mode="group">
  <xsl:element name="{@group}">
    <xsl:apply-templates select="key('group', @group)"/>
  </xsl:element>
</xsl:template>

<xsl:template match="tag/@group"/>

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

You haven't shown the exact structure and nesting of the input documents, the above assumes a structure like

<root>
  <tag group="group1">
    <title lang="eng">title1</title>
</tag>
  <tag group="group1">
    <title lang="se">title2</title>
</tag>
  <tag group="group1">
    <title lang="eng">title3</title>
</tag>
</root>

where actually the exact name of the root element does not matter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i merge disk 0 and disk 1 | Windows

From Dev

How can I merge files into 1 file, and how to split them after the merge

From Java

How can I copy multiple cells from one notebook to another at once (the cells should not merge after pasting)

From Dev

Using XSLT how can I copy a specific XML subtree

From Dev

how can i copy file in data directory

From Dev

How do I merge two XML docs with XSLT (can be XSLT 2.0)?

From Dev

Can't open folder after giving it www-data group, how do I become owner?

From Dev

how can I count mysql data with group by

From Dev

How can I plot a "group" data in R?

From Dev

How can I know GROUP BY is missing data?

From Dev

XSLT Group and Merge

From Dev

How could I copy data that contain '\0' character

From Dev

How could I copy data that contain '\0' character

From Dev

How can I query 1=1 or 1=0 in mongo?

From Dev

How can i check a Long data type number a power of 2 or not using a&(a-1)==0 in JAVA

From Dev

How can I initialize 2nd array elements which are 'double data type' as -1 or 0 easily

From Dev

How can I get a Group By to show me a 0 if no rows match?

From Dev

How can I merge 2 zip files into 1?

From Dev

How can i merge 2 sheets into 1 in excel

From Dev

How can I merge 2 zip files into 1?

From Dev

How can I merge consecutive subArrays which have the same data in it?

From Dev

How can I merge two files in Pentaho Data Integration (Kettle)

From Dev

How can I perform a "setdiff" merge using data.table?

From Dev

How can I merge my data set based on the time in MATLAB?

From Dev

How can I merge two files in Pentaho Data Integration (Kettle)

From Dev

How can I merge cells same data in Excel 2010?

From Dev

how can i merge same data in array in ci?

From Dev

How can I group table after inner join?

From Dev

How can I copy 1 specific commit to another branch ?

Related Related

  1. 1

    How can i merge disk 0 and disk 1 | Windows

  2. 2

    How can I merge files into 1 file, and how to split them after the merge

  3. 3

    How can I copy multiple cells from one notebook to another at once (the cells should not merge after pasting)

  4. 4

    Using XSLT how can I copy a specific XML subtree

  5. 5

    how can i copy file in data directory

  6. 6

    How do I merge two XML docs with XSLT (can be XSLT 2.0)?

  7. 7

    Can't open folder after giving it www-data group, how do I become owner?

  8. 8

    how can I count mysql data with group by

  9. 9

    How can I plot a "group" data in R?

  10. 10

    How can I know GROUP BY is missing data?

  11. 11

    XSLT Group and Merge

  12. 12

    How could I copy data that contain '\0' character

  13. 13

    How could I copy data that contain '\0' character

  14. 14

    How can I query 1=1 or 1=0 in mongo?

  15. 15

    How can i check a Long data type number a power of 2 or not using a&(a-1)==0 in JAVA

  16. 16

    How can I initialize 2nd array elements which are 'double data type' as -1 or 0 easily

  17. 17

    How can I get a Group By to show me a 0 if no rows match?

  18. 18

    How can I merge 2 zip files into 1?

  19. 19

    How can i merge 2 sheets into 1 in excel

  20. 20

    How can I merge 2 zip files into 1?

  21. 21

    How can I merge consecutive subArrays which have the same data in it?

  22. 22

    How can I merge two files in Pentaho Data Integration (Kettle)

  23. 23

    How can I perform a "setdiff" merge using data.table?

  24. 24

    How can I merge my data set based on the time in MATLAB?

  25. 25

    How can I merge two files in Pentaho Data Integration (Kettle)

  26. 26

    How can I merge cells same data in Excel 2010?

  27. 27

    how can i merge same data in array in ci?

  28. 28

    How can I group table after inner join?

  29. 29

    How can I copy 1 specific commit to another branch ?

HotTag

Archive