【问题标题】:select the value of an xml attribute of an element if value of another attribute matches with value stored in a variable using xslt如果另一个属性的值与使用 xslt 存储在变量中的值匹配,则选择元素的 xml 属性的值
【发布时间】:2015-11-25 05:16:57
【问题描述】:

我有以下示例配置。我有一个 xsl,其中我将空格分隔的范围(getAccoutInfo getCustomerInfo)存储在变量“范围”中,您在下面的范围元素中看到的内容作为单独的输入属性。我正在使用 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>

我正在尝试使用“应用模板”将“输入”属性的相应“输出”属性的值打印为项目符号点,如上述配置中所示。模板复制如下。

<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>

但看起来 xsl 代码对我不起作用。有人可以指出错误并指出正确的方向吗?

【问题讨论】:

  • 你能在这种情况下显示你期望的输出吗?谢谢!
  • 请发布一个可重现的示例 - 请参阅:stackoverflow.com/help/mcve
  • @TimC 输出应如下所示,带有要点: • Account_Information • Customer_Information–

标签: xml xslt xpath xml-parsing


【解决方案1】:

如果您只是尝试选择 input 属性出现在标记化的 scopes 变量中的 scope 元素,那么这个简单的表达式应该可以解决问题....

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

试试这个 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>

当应用于以下输入时

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

以下是输出:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2017-01-29
    相关资源
    最近更新 更多