简体   繁体   English

如何基于xslt中以“ pro_”开头的属性值选择max xml属性?

[英]how to select max xml attribute based on its attribute value start with “pro_” in xslt?

I have a weird requirement to get the max value that starts with specified string.Below is my sample source code,As we have seen the book element has an attribute called id ,as we have seen,some value of the id attribute are start with "pro_",such as pro_60 , pro_55 ,while the other attribute value are just pure number,such as 70 , 40 . 我有一个怪异的要求来获取以指定字符串开头的最大值。下面是我的示例源代码,正如我们所见, book元素具有一个名为id的属性,如我们所见, id属性的某些值是start与“亲_”,如pro_60,pro_55,而其他的属性值都只是纯数,例如70,40。

So my question is how to get the attribute value that starts with pro_ and the number is the max number .In this example the max number starts with pro_ is 65 ,so the expected result is pro_65 ,I can do it in my code ,but I am wondering can we do it just by using ?Any help is very greatful! 所以我的问题是如何获取以pro_开头的属性值,该数字为最大数 。在此示例中,以pro_开头的最大数为65 ,因此预期结果为pro_65 ,我可以在我的代码中进行操作,但我想知道我们是否可以仅使用做到这一点 ?任何帮助都非常有用!

XPath version is 1.0 . XPath版本是1.0

<?xml version="1.0" encoding="UTF-8"?>
 <datas>
    <books>
        <book id="pro_60">Java</book>
        <book id="pro_55">Golang</book>
        <book id="pro_40">PHP</book>
        <book id="pro_65">C++</book>
        <book id="pro_55">Python</book>
        <book id="70">Javascript</book>
        <book id="40">HTML5</book>
    </books>
</datas>

Using XPath 2.0: 使用XPath 2.0:

/datas/books/book/@id[starts-with(., 'pro_')][not(../../book/@id/number(substring-after(., 'pro_')) > number(substring-after(., 'pro_')))]

As it depends on the feature of using functions calls in the last step of a path to compute a sequence of atomic values it can not be directly translated to XPath 1.0. 由于它取决于在路径的最后一步中使用函数调用来计算原子值序列的功能,因此不能直接转换为XPath 1.0。

A shorter XPath 2.0 expression : 较短的XPath 2.0表达式

max(/*/*/*/@id[starts-with(.,'pro_')]/number(substring(.,5)))

This just calculates the maximum value. 这只是计算最大值。

To select all elements with id attributes that have this maximum value, use : 要选择所有具有此最大值的id属性的元素,请使用

(/*/*/*[@id = concat('pro_', string(max(/*/*/*/@id[starts-with(.,'pro_')]/number(substring(.,5)))))]

As for XPath 1.0, this cannot be computed using a single XPath 1.0 expression. 对于XPath 1.0,不能使用单个XPath 1.0表达式来计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM