xslt 2.0 tokenize and group

后端 未结 1 977
别跟我提以往
别跟我提以往 2020-12-22 13:18

I have an text file with following data:

Heros
Firstname Sean
Lastname Connery
DOB 25-08-1930

Films
Dr.No 1962
Goldfinger 1964
Thunerball 1965

Awa         


        
1条回答
  •  滥情空心
    2020-12-22 13:25

    You shouldn't need to group; you could just tokenize (and tokenize and tokenize...).

    Here's an example. It doesn't do anything with the case of the element names. You can either handle those changes during the building of $initData, or you can add additional templates to handle any changes.

    Also, the element names have to be valid QNames. Right now the stylesheet terminates processing with a message, but you can change how that's handled.

    This should at least get you started...

    XSLT 2.0

    
      
      
    
      
      
    
      
        
          
            
            
              
                
                  
                    
                    
                      
                        
                          
                                              
                      
                      
                        Invalid element name: 
                      
                    
                  
                            
              
              
                Invalid element name: 
              
            
          
        
      
    
      
        
          
        
      
    
      
            
      
    
      
    
    
    

    EDIT

    You're passing the text file in as the input of the transform. That's why you had to add the element.

    Since you don't actually have an XML input, you can pass the stylesheet itself in as input. Nothing will get processed because we're only applying-templates to the variable in the template that matches root (/).

    You also need to set the input-uri parameter with transformer.setParameter("input-uri", TXT_PATH);. If your path is absolute, be sure to add the file:/// protocol.

    Example...

    Text File

    Heros
    Firstname Sean
    Lastname Connery
    DOB 25-08-1930
    
    Films
    Dr.No 1962
    Goldfinger 1964
    Thunerball 1965
    
    Award
    name Academy
    time 1
    
    Award
    name BAFTA
    time 2
    
    Award
    name Gloden Globes
    time 3
    

    Java (you'll need to change paths/filenames)

    final String TXT_PATH = "file:///C:/tmp/input.txt";
    final String XSLT_PATH = "C:/tmp/txt2xml.xsl";
    final String XML_PATH = "C:/tmp/test_xml_result.xml";
    
    TransformerFactory tFactory = new net.sf.saxon.TransformerFactoryImpl();
    Transformer transformer = tFactory.newTransformer(new StreamSource(new File(XSLT_PATH)));
    transformer.setParameter("input-uri", TXT_PATH);
    transformer.transform(new StreamSource(new File(XSLT_PATH)),new StreamResult(new File(XML_PATH)));
    

    XSLT 2.0

    Same as above.

    Output

    
       
          Sean
          Connery
          25-08-1930
       
       
          1962
          1964
          1965
       
       
          Academy
          
       
       
          BAFTA
          
       
       
          Gloden Globes
          
       
    
    

    However, since you're using Saxon you could use the s9api and specify an initial template. This is the way I would do it instead of passing the stylesheet as the input to the transform.

    Example...

    Java

    final String TXT_PATH = "file:///C:/tmp/input.txt";
    final String XSLT_PATH = "C:/tmp/txt2xml.xsl";
    final String XML_PATH = "C:/tmp/test_xml_result.xml";
    
    Processor processor = new Processor(false);
    Serializer serializer = processor.newSerializer();
    serializer.setOutputFile(new File(XML_PATH));
    XsltCompiler compiler = processor.newXsltCompiler();
    XsltExecutable executable = compiler.compile(new StreamSource(new File(XSLT_PATH)));
    XsltTransformer transformer = executable.load();
    transformer.setInitialTemplate(new QName("root"));
    transformer.setParameter(new QName("input-uri"), new XdmAtomicValue(TXT_PATH));
    transformer.setDestination(serializer);
    transformer.transform();
    

    XSLT 2.0

    
      
      
    
      
      
    
      
        
          
            
            
              
                
                  
                    
                    
                      
                        
                          
                                              
                      
                      
                        Invalid element name: 
                      
                    
                  
                            
              
              
                Invalid element name: 
              
            
          
        
      
    
      
        
          
        
      
    
      
            
      
    
      
    
    
    

    Input and output would be the same. Let me know if you need me to add the java imports to the example.

    0 讨论(0)
提交回复
热议问题