Retrieving XML node from a path specified in an attribute value of another node

后端 未结 3 712
甜味超标
甜味超标 2021-01-03 09:11

From this XML source :



  
    

        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 09:55

    The problem with what you've got:

    
    ...
    
    

    is that it assumes elementName is only a single element's name. If it's an arbitrary XPath expression, the test will fail.

    The second problem you'll run into (or probably already have) is that attribute value templates are not allowed in select clauses, so you can't do something simple like this:

    
    

    What you need is something that will dynamically create the XPath expression to the element you're looking for, and then dynamically evaluate that expression.

    For a solution, I turned to EXSLT's evaluate() function, in the dynamic library. I had to use it twice: once to build up the entire XPath expression representing the query, and once to evaluate that query. The advantage of this approach is that you get access to evaluate's full XPath parsing and execution capabilities.

    
    
    ...
    
    

    where the dyn namespace is declared up top as http://exslt.org/dynamic. Figuring out where to quote here is tricky and took me several tries to get right.

    Using these instead of your elementName and value-of expressions, I get:

    
    
    
    Test
    
    
    Foo Bar Fizz
    testBar testFizz
    testBar2 testFizz2

    which is what I think you're looking for.

    Unfortunately, I'm not versed in MSXML, so I can't tell you whether your specific XSLT processor supports this extension or something similar.

提交回复
热议问题