XSLT to add new elements to the root element of an XML

前端 未结 1 820
攒了一身酷
攒了一身酷 2021-01-25 03:51

I want to add a couple of elements at the end of an XML document, right before the closing of the root node, and I\'m using an XSL to do the transformation.

The source X

1条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-25 04:02

    Try it this way?

    
    
    
    
    
    
        
            
        
    
    
    
    
        
            
            
            my content
        
    
    
    
    

    If all you ever want to do is add the new element/s, then you could shorten this to:

    
    
    
    
    
    
        
            
            
            my content   
        
    
    
    
    

    Note:

    In your example input, the root element - and by inheritance, all of its child nodes - is in a namespace. If you want the added element/s to be in the same namespace, use:

    my content
    

    instead of:

    my content
    

    Edit:

    why doesn't it work if I put ( is the document root node) instead of ?

    That too is the result of the root element being in a namespace. In order to address it by name, you need to assign a prefix to the namespace and use it in the XPpath expression selecting the node:

    
    
    
    
    
    
    
        
            
            
            my content   
        
    
    
    
    

    Once you have defined the prefix, you can also use it to place the added element/s in the namespace bound to the prefix:

        
        my content   
    

    Alternatively you could do:

        
        my content 
    

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