【问题标题】:XSLT 1.0 trying to convert node to lowercase to be compared with stringXSLT 1.0 试图将节点转换为小写以与字符串进行比较
【发布时间】:2013-02-05 01:35:31
【问题描述】:

我有一个名为 $SearchRecipe 的参数,它包含一个字符串,我总是将小写字符串传递给它。

我有一个 XML 文件,我正在使用我的 xslt 和 xml 文件中的一些数据示例访问它:

<ARecipe>
    <RecipeNo>117</RecipeNo>
    <ItemName>Veggie Sausages with Beans and Chips</ItemName>
    <RecipeInfo>
        <Ingredients>Linda Mcartney Sausages(2 per serving), Beans (400g per serving), Chips(Handful), Random Chillis (up to you how much)</Ingredients>
        <Instructions>Put on fat fryer, insert chips and sausages. Put on wok, insert beans and add some random chillis etc. Heat beans and remove cooked Sausages and Chips. Place sausages and chips in tissue to remove excess oil. Place in a plate and serve warm.</Instructions>
        <RecipeType>Main</RecipeType>
        <Vegetarian>No</Vegetarian>
    </RecipeInfo>
</ARecipe>

我正在运行一个查询来遍历所有 ItemName 节点并将其与我的字符串进行比较,它工作正常。例如,如果我将 V 放入字符串中,它与此 (ARecipe) 中的 ItemName 匹配,这将显示在我的 xslt 输出中。但是,如果我将 v 作为值传递,那么它不会带来这个特定的节点(ARecipe)。

我正在使用这些行来浏览 xml 文件:

<xsl:variable name="matchedRecipes"
     select="ARecipe[
           ($SearchType = 'Start' and starts-with($Test, $SearchRecipe)) or
           ($SearchType = 'Contains' and contains(ItemName, $SearchRecipe)) or
           ($SearchType = 'RecipeType' and 
                        contains(RecipeInfo/RecipeType, $SearchRecipe))
                      ]" />

  <xsl:if test="$SearchType = 'Start' or $SearchType = 'Contains'">
    <xsl:apply-templates select="$matchedRecipes">

到目前为止我尝试过的是:

        <xsl:variable name="Test">
    <xsl:value-of select="translate('ARecipe/ItemName',$ucletters,$lcletters)"/>
    </xsl:variable>

我是大多数语言等方面的新手,但我可以很容易地在 C# 中做这种事情,我与 xslt 有着绝对的夜生活。此外,当我写这个寻求帮助的请求时,我已经做了几个小时的工作,最后一个小时我花了各种各样的方法来解决这个小写问题。因此,如果我没有正确询问,我想提前道歉。

如果你想看大图,我已经在这里上传了我的代码:

http://pastebin.com/w8AsiQRg

【问题讨论】:

标签: xslt translate lowercase


【解决方案1】:

你需要这样做:

<xsl:variable name="SearchLc" select="translate($SearchRecipe, $ucletters, $lcletters)" />
<xsl:variable name="matchedRecipes"
     select="ARecipe[
           ($SearchType = 'Start' and 
              starts-with(translate(ItemName, $ucletters, $lcletters), $SearchLc)) or
           ($SearchType = 'Contains' and 
               contains(translate(ItemName, $ucletters, $lcletters), $SearchLc)) or
           ($SearchType = 'RecipeType' and 
                    contains(translate(RecipeInfo/RecipeType, $ucletters, $lcletters),
                                 $SearchRecipe))
                      ]" />

您可以提前获取搜索文本的小写版本,但随后您需要对选择 XPath 中的各个项目进行小写。

【讨论】:

  • 我现在明白了,效果很好。当我通过将其设置为默认值进行测试时,我得到了正确的变量语法,我猜测将其从大写转换为小写的语法也是正确的,我错过了将 ItemName 转换为小写的部分。干杯@JLRishe
猜你喜欢
  • 2015-07-12
  • 2011-09-24
  • 2012-08-21
  • 2013-05-21
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多