XSLT: Reading a param that's an xml document passed as a string

后端 未结 2 461
生来不讨喜
生来不讨喜 2020-12-04 00:15

I have ran into an issue with another engineer. Being a new engineer, I have to figure out the solution to this, but I haven\'t been able to. Any help would be appreciated,

2条回答
  •  渐次进展
    2020-12-04 00:53

    It's not possible to parse a string as XML, as @michael.hor257k correctly observed in the accepted answer, but there is a way to treat your string as a node-set by loading it as an embedded document. That is possible using the data URI scheme with the document() function. XSLT 1.0 Specification warns, however, that this is implementation dependent (the processor is not required to support any uri scheme). I tested this with XSLT 1.0 processors such as Xalan and Saxon 6 and it worked.

    The solution is to append your string to the data URI scheme data:text/xml separated by a comma. You can then pass this string to your document() function and it will parse it as an XML file:

    document(concat('data:text/xml,',$servers))
    

    You can then apply any templates to the node-set.

    Here is a stylesheet with a $servers parameter which should receive your string containing XML data. It will parse the string, transform the nodes in templates, and generate a XML output with some data:

    
        
        
    
        
            
        
    
        
            
                
            
        
    
        
        
            
        
    
        
        
            
        
    
    
    

    If you run this with any source, and your data passed as a parameter you get:

    
       
       
       
    
    

    UPDATE: this feature also depends on parser support for data-uris. I wasn't aware of that since my XML environment has that support, so changing different XSLT processors didn't make any difference. I used Oxygen XML Editor 15.2, in a Mac OS X environment. I will update this information when I discover exactly which parser it is using.

提交回复
热议问题