简体   繁体   中英

XSL issue : making multiple XMLs using one XML with the help of XSLT2.0

i have the following input XML:

    <?xml version="1.0" encoding="UTF-8"?>
<class xmlns:xfa="soommmeee-tteexxxxtttttt">
    <students>
        <id>A</id>
        <name>jscvjab</name>
        <description>zcm,ab</description>
        <student>
            <id>1</id>
            <refObjectId>m9</refObjectId>
            <subject>
                <id>25</id>
                <name>science</name>
                <friend>popeye</friend>
                <firstname>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>kullu</fragId>
                            <znjdvgsc>nSVCm</znjdvgsc>
                            <zmdcb>zmbvc</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>2</id>
            <refObjectId>m5</refObjectId>
            <subject>
                <id>92</id>
                <name>commerce</name>
                <friend>brutus</friend>
                <firstname>
                    <textContent>
                        <Id>m9</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Contents</body>
                        </text>
                        <Key>Slide</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Jerry</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>zxZXVj</fragId>
                            <znjdvgsc>kjsdgfjkvnwsa</znjdvgsc>
                            <zmdcb>kadbvscjkna</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
    <students>
        <id>B</id>
        <name>jzvxcbhjnba</name>
        <description>mjZSVxcj</description>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <friend>hercules</friend>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <friend>aladin</friend>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <friend>aladin</friend>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>4</id>
            <refObjectId>m3</refObjectId>
            <subject>
                <id>75</id>
                <name>PCM</name>
                <friend>hercules</friend>
                <firstname>
                    <textContent>
                        <Id>j4423</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>dfzxsdbjchA</body>
                        </text>
                        <Key>zdbgcfjkbna</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>zdjbhfjkcda</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>sdjkhzjk</body>
                        </text>
                        <Key>mzsdjkbjk</Key>
                    </textContent>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>sdbnfn</fragId>
                            <znjdvgsc>masdjkljk</znjdvgsc>
                            <zmdcb>msjkldbhfjkn</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
    <faculty>
        <configuration>
            <Type>Output</Type>
            <Key>Font</Key>
            <Value>10pt</Value>
        </configuration>
        <configuration>
            <Type>Out</Type>
            <Key>Text</Key>
            <Value>127,127,127</Value>
        </configuration>
        <configuration>
            <Type>put</Type>
            <Key>Slide</Key>
            <Value>18pt</Value>
        </configuration>
        <configuration>
            <Type>utp</Type>
            <Key>Slide</Key>
            <Value>127</Value>
        </configuration>
    </faculty>
    <Info />
    <Dean>
        <name>zcdfjkaqbkd</name>
    </Dean>    
</class>

what i want with my xsl is that it shoould get devided into multiple XMLs using XSLT2.0 and each xml should consist of one 'student' each including rest of the nodes and should have name according to the friend. say the first xsl should be 1. popeye:

<?xml version="1.0" encoding="UTF-8"?>
<class xmlns:xfa="soommmeee-tteexxxxtttttt">
    <students>
        <id>A</id>
        <name>jscvjab</name>
        <description>zcm,ab</description>
        <student>
            <id>1</id>
            <refObjectId>m9</refObjectId>
            <subject>
                <id>25</id>
                <name>science</name>
                <friend>popeye</friend>
                <firstname>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>kullu</fragId>
                            <znjdvgsc>nSVCm</znjdvgsc>
                            <zmdcb>zmbvc</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
<faculty>
        <configuration>
            <Type>Output</Type>
            <Key>Font</Key>
            <Value>10pt</Value>
        </configuration>
        <configuration>
            <Type>Out</Type>
            <Key>Text</Key>
            <Value>127,127,127</Value>
        </configuration>
        <configuration>
            <Type>put</Type>
            <Key>Slide</Key>
            <Value>18pt</Value>
        </configuration>
        <configuration>
            <Type>utp</Type>
            <Key>Slide</Key>
            <Value>127</Value>
        </configuration>
    </faculty>
    <Info />
    <Dean>
        <name>zcdfjkaqbkd</name>
    </Dean>  
</class>

and following by 2. brutus and 3. hercules and so on........ please help in this issue.....i am unable to make any of the difference in this thing....thanks

This will get you started.

<?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:template match="/">
        <xsl:apply-templates/>
        <xsl:message select="'Done splitting the XML file.'"/>
    </xsl:template>

    <xsl:template match="student">                    
        <xsl:variable 
            name="filename" 
            select="concat(count(preceding::student)+1,'.xml')"/>

        <xsl:message select="concat('Writing ',$filename)"/>

        <xsl:result-document
            method="xml"
            omit-xml-declaration="no"
            encoding="UTF-8"
            indent="yes"
            href="{$filename}">            
            <xsl:copy-of select="."/>            
        </xsl:result-document>                
    </xsl:template>

    <xsl:template match="text()"/>

</xsl:stylesheet>

What you may choose to work on is establishing the file name. Each file name must absolutely be unique. If there is a duplicate, such as 2 students named Brutus it will crash the transform. I couldn't find a unique element in your xml so I used a student count as an alternative to make numbered files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM