简体   繁体   中英

select the value of an xml attribute of an element if value of another attribute matches with value stored in a variable using xslt

I have below sample configuration. I have an xsl in which I have space seperated scope(getAccoutInfo getCustomerInfo) stored in variable 'scope', what you see below inside scope element as an individual input attribute. I am tokenizing them using str:tokenize

<scopes>
     <scope input="getAccountInfo" output="Account_Information"/>
     <scope input="getCustomerInfo" output="Customer_Information"/>
</scopes>

    <xsl:variable name="scopes" select="str:tokenize($scope, ' ')"/>
<ul style="list-style-type:disc">
  <li>
   <font size="2" style="font-family:verdana">
    <xsl:apply-templates select="$scopes[position()]"/>
   </font>
  </li>
</ul>

I am trying to print the values of corresponding 'output' attribute of an 'input' attribute as seen in above config as bullet points, using "apply-templates" to an individual token. The template is copied below.

<xsl:template match="token">
    <xsl:if test="$scopeMapping/*[local-name()='scopes']/*[local-name()='scope']/@*[local-name()='input']/text() = normalize-space(.)">

        <xsl:value-of select="$scopeMapping/*[local-name()='scopes']/*[local-name()='scope']/@*[local-name()='output']/text()"/>
    </xsl:if>
</xsl:template>

But looks like the xsl code is not working for me. Can someone point out what is wrong and point in right direction?

If you are simply trying to select the scope elements whose input attribute appears in the tokenized scopes variable, then this simple expression should do the trick....

<xsl:for-each select="//scopes/scope[@input = $scopes]">

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:str="http://exslt.org/strings"
                extension-element-prefixes="str">
    <xsl:output method="html" indent="yes" />

    <xsl:param name="scope" select="'getCustomerInfo getAccountInfo'" />
    <xsl:template match="/">
        <xsl:variable name="scopes" select="str:tokenize($scope, ' ')"/>
        <ul>
            <xsl:for-each select="//scopes/scope[@input = $scopes]">
                <li>
                    <xsl:value-of select="@output" />
                </li>
            </xsl:for-each>
        </ul>
    </xsl:template>
</xsl:stylesheet>

When applied to the following input

<scopes>
     <scope input="getAccountInfo" output="Account_Information"/>
     <scope input="getCustomerInfo" output="Customer_Information"/>
</scopes>

The following is output:

<ul>
<li>Account_Information</li>
<li>Customer_Information</li>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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