【问题标题】:XSLT: analyze-string, replace on regular expressionsXSLT:分析字符串,替换正则表达式
【发布时间】:2018-10-04 13:07:01
【问题描述】:

这是这个问题的后续: XSLT: analyze-string on text with children nodes

我正在尝试对可能包含子节点的元素使用分析字符串。这是源文件的 sn-p:

    <year>
         1975 music
     </year>
     <year>
         1985<genre>rock</genre>music
     </year>
     <year>
         2005 music
     </year>

现在是下一个练习。使用相同的xml输入,我想添加属性:

    <year decade="7">
         70's music
     </year>
     <year decade="8">
         80's<genre>rock</genre> music
     </year>

我需要分析字符串吗? 谢谢!

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

    您需要添加一个匹配year的模板,该模板可以获取包含年份的文本节点,并从中提取相关数字,以创建十年

    试试这个模板

    <xsl:template match="year">
      <xsl:variable name="text" select="normalize-space(text()[matches(., $pattern)])" />
      <year decade="{replace($text, concat('.*', $pattern, '.*'), '$1')}">
        <xsl:apply-templates />
      </year>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 2012-04-25
      • 2015-11-30
      相关资源
      最近更新 更多