XSLT:如何检查URL中存在的特定协议或字符串

时间:2019-04-07 09:22:25

标签: xml xslt

我要在出现提示后显示此xml文件

页面应包含员工详细信息表。 列应具有以下标题:id,名字,姓氏,链接网址,薪水 该表必须包含相应的员工详细信息。

条件 如果当我单击链接时链接URL具有(http://或https://或www),则应在新标签页中打开 这意味着-016,024,056应该在新标签页中打开

其他链接应打开相同的标签 即-080、092应该打开相同的标签

employee.xml

            <?xml version = "1.0"?>  
            <?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>   
            <class>   
               <employee id = "016">  
                  <firstname>Aryan</firstname>   
                  <lastname>Gupta</lastname>   
                  <linkurl>https://www.aryanguptan.com/external</linkurl>   
                  <salary>30000</salary>  
               </employee>   
                    <employee id = "080">   
                  <firstname>Sam</firstname>   
                  <lastname>Alex</lastname>   
                  <linkurl>/user/internal/080</linkurl>   
                  <salary>10000</salary>   
               </employee>  
               <employee id = "024">   
                  <firstname>Sara</firstname>   
                  <lastname>Khan</lastname>   
                  <linkurl>http://www.sarakhan.com</linkurl>   
                  <salary>25000</salary>  
               </employee>   
                <employee id = "092">   
                  <firstname>John</firstname>   
                  <lastname>Samuel</lastname>   
                  <linkurl>/user/internal/092</linkurl>   
                  <salary>10000</salary>   
               </employee> 
               <employee id = "056">   
                  <firstname>Peter</firstname>   
                  <lastname>Symon</lastname>   
                  <linkurl>www.petersymon.com</linkurl>   
                  <salary>10000</salary>   
               </employee>    
            </class> 

这是样式表文件

employee.xsl

            <?xml version = "1.0" encoding = "UTF-8"?>   

                <xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  

                <xsl:variable name="tlds">
                      <tld>http://</tld>
                      <tld>https://</tld>
                      <tld>www.</tld>      
                   </xsl:variable>

                   <xsl:variable name="lookup" select="document('')//xsl:variable[@name='tlds']"/>   

                   <xsl:template match = "/">          

                      <html>   
                         <body>   
                            <h2>Employee </h2>   
                            <table border = "1">   
                               <tr bgcolor = "pink">   
                                  <th>ID</th>   
                                  <th>First Name</th>   
                                  <th>Last Name</th>   
                                  <th>Link URL</th>   
                                  <th>Salary</th>   
                               </tr>   

                               <xsl:for-each select = "class/employee">                                 
                                     <tr>   
                                        <td><xsl:value-of select = "@id"/></td>   
                                        <td><xsl:value-of select = "firstname"/></td>   
                                        <td><xsl:value-of select = "lastname"/></td>  

                                        <xsl:choose> 
                                            <xsl:when test="$lookup/tld[starts-with($linkurl)]">
                                                <td><a href="{linkurl}" target="_blank"><xsl:value-of select = "linkurl"/></a></td>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <td><a href="www.google.com/{linkurl}"><xsl:value-of select = "concat('www.google.com/',linkurl)"/></a></td>
                                            </xsl:otherwise>
                                        </xsl:choose>

                                        <td><xsl:value-of select = "salary"/></td>  
                                     </tr>                        
                               </xsl:for-each>   

                            </table>   
                         </body>   
                      </html>  
                   </xsl:template>    
                </xsl:stylesheet>  

当我执行employee.xml时,我在浏览器中收到此错误

XSLT转换过程中发生错误:调用XPath函数的参数数量错误。

还有是否可以单独重用varibale模板? 我期望这个输出

enter image description here

1 个答案:

答案 0 :(得分:1)

问题出在这一行:

<xsl:when test="$lookup/tld[starts-with($linkurl)]">

starts-with函数有两个参数;第一个是您要检查的字符串,第二个参数开头。换句话说,应该是这样:

<xsl:when test="$lookup/tld[starts-with($linkurl, .)]">

请注意,这是假设您已经在样式表中定义了$linkurl变量,因为当前已将其省略

<xsl:variable name="linkurl" select="linkurl" />