2016-04-06 7 views
0

Ich habe eine XML-Datei, während auf der Übereinstimmung der Übereinstimmung (Enthält) auf der Xpath-Variable aufgeteilt werden muss.Split XML mit XSLT zu mehreren Dateien mit Java

`<Apps> 
    <App> 
     <AppID>49O</AppID> 
     <Type>MP Pay</Type> 
    </App> 
    <App> 
     <AppID>715</AppID> 
     <Type>DP Pay</Type> 
    </App> 
    <App> 
     <AppID>716</AppID> 
     <Type>MP Pay</Type> 
    </App> 
    <App> 
     <AppID>725</AppID> 
     <Type>CP Pay</Type> 
    </App> 
    <App> 
     <AppID>728</AppID> 
     <Type>MP Pay</Type> 
    </App> 
    <App> 
     <AppID>728</AppID> 
     <Type>CP Pay</Type> 
    </App> 
</Apps>` 

So Bedingungen für die xml ist 1. Apps/App/Typ/text() aufzuspalten -> Enthält 'MP' 2. Apps/App/Typ/text() -> 'DP' Enthält

im Folgenden sind die erwarteten Ergebnisse:

  1. Alle App-Knoten enthält Typ als 'MP' in einer Datei sein sollte -> MP.xml.

    <Apps> <App> <AppID>49O</AppID> <Type>MP Pay</Type> </App> <App> <AppID>716</AppID> <Type>MP Pay</Type> </App> <App> <AppID>728</AppID> <Type>MP Pay</Type> </App> </Apps>

  2. Alle App-Knoten-Typ als 'DP' enthält, in einer Datei sein sollte -> DP.xml.

    <Apps> <App> <AppID>715</AppID> <Type>DP Pay</Type> </App> </Apps>

  3. Alle App-Knoten nicht gelungen, die über 2 Bedingungen sollten in der Datei entsprechen -> Mismatched.xml.

    <Apps> <App> <AppID>715</AppID> <Type>CP Pay</Type> </App> <App> <AppID>725</AppID> <Type>CP Pay</Type> </App> </Apps>

Java-Code

public class Splitter { 

    public static void transform(String sourcePath, String xsltPath, 
      String resultDir) { 

     TransformerFactory tFactory = TransformerFactory.newInstance(); 
     try { 
      Transformer transformer = tFactory.newTransformer(new StreamSource(
        new File(xsltPath))); 
      transformer.transform(new StreamSource(new File(sourcePath)), 
        new StreamResult(new File(resultDir))); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 

     // Set saxon as transformer. 
     System.setProperty("javax.xml.transform.TransformerFactory", 
       "net.sf.saxon.TransformerFactoryImpl"); 

     String inputFilepath = "resources\\InputFile.xml"; 
     String transformXsltPath = "resources\\Transform.xslt"; 
     String outputDir = "D://Tmp//"; 

     transform(inputFilepath, transformXsltPath, outputDir); 
     System.out.println("Completed"); 
    } 

} 
+0

könnten Sie Ihr Java-Code als auch schreiben? Verwenden Sie Saxon, einfaches Java (mit xslt 2.0)? Danke, Stephan –

+0

Ja benutze Saxon. Java Code in meiner Frage selbst hinzugefügt –

Antwort

0
<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 

    <xsl:template match="Apps"> 
     <xsl:result-document href="MP.xml"> 
     <xsl:copy> 
      <xsl:apply-templates select="App[contains(Type, 'MP')]"/> 
     </xsl:copy> 
     </xsl:result-document> 
     <xsl:result-document href="DP.xml"> 
     <xsl:copy> 
      <xsl:apply-templates select="App[contains(Type, 'DP')]"/> 
     </xsl:copy> 
     </xsl:result-document> 
     <xsl:result-document href="Mismatched.xml"> 
     <xsl:copy> 
      <xsl:apply-templates select="App[not(contains(Type, 'MP') or contains(Type, 'DP'))]"/> 
     </xsl:copy> 
     </xsl:result-document> 
    </xsl:template> 

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

Danke. Perfekt arbeiten! –

0

Hier ist ein Ansatz, der mehr regelbasierte ist:

<xsl:template match="App[starts-with(Type, 'MP')]" mode="group">MP</xsl:template> 

<xsl:template match="App[starts-with(Type, 'DP')]" mode="group">DP</xsl:template> 

<xsl:template match="App" mode="group">Mismatched</xsl:template> 

<xsl:function name="f:key" as="xs:string"> 
    <xsl:param name="e" as="element(App)"/> 
    <xsl:apply-templates select="." mode="group"/> 
</xsl:function> 

<xsl:template match="Apps"> 
    <xsl:for-each-group select="App" group-by="f:key(.)"> 
     <xsl:result-document href="{current-grouping-key()}.xml"> 
     <Apps> 
      <xsl:copy-of select="current-group()"/> 
     </Apps> 
     </xsl:result-document> 
    </xsl:for-each-group> 
</xsl:template>