分组和身份转换

时间:2018-08-31 05:13:03

标签: xslt-2.0 xslt-grouping

如何同时应用身份转换和分组

Following code snippets to convert docx file to pdf

Maven Dependencies:

`<dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j</artifactId>
            <version>6.0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-compress</artifactId>
                </exclusion>
            </exclusions>
        </dependency>`
    <br><br><b>Java code to convert docx to pdf:</b> 

`ByteArrayOutputStream bos = new ByteArrayOutputStream();
                System.out.println("Start : "+new Date());
                WordprocessingMLPackage wordprocessingMLPackage = WordprocessingMLPackage.load(bis);
                Docx4J.toPDF(wordprocessingMLPackage, bos);
                System.out.println("End : "+new Date());
`

我想要输出为

  <items>
        <user>
          <id>8788989</id>
         <firstname>test</firstname>
         <lastname>user</lastname>
        </user>
        <info>test xml</info>
        <fromdate><fromdate>
        <todate></todate>

        <item id="123" name="Java">
            <price>1</price>
            <description></description> 
        </item>

        <item id="123" name="Java and XML">
            <price>2</price>
            <description></description> 
        </item>

        <item id="234" name="python">
            <price>3</price>
            <description></description> 
        </item>

        <item id="234" name="scala">
            <price>3</price>
            <description></description> 
        </item>

      </items>

对项目/ @ id进行分组

1 个答案:

答案 0 :(得分:1)

您可以像这样分组:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes"/>

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

    <xsl:template match="items">
        <items>
            <xsl:apply-templates select="* except item"/>
            <xsl:for-each-group select="item" group-by="@id">
                <group>
                    <xsl:apply-templates select="../item[@id = current()/@id]"/>
                </group>
            </xsl:for-each-group>
        </items>
    </xsl:template>

</xsl:stylesheet>

更新后的答案:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes"/>

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

    <xsl:template match="items">
        <items>
            <xsl:apply-templates select="* except item"/>
            <xsl:for-each-group select="item" group-by="@id">
                <group>
                    <xsl:apply-templates select="current-group()"/>
                </group>
            </xsl:for-each-group>
        </items>
    </xsl:template>

</xsl:stylesheet>