简体   繁体   English

XSL:基于正则表达式将字符串拆分为标签

[英]XSL: split string into tags based on regex

I'm working in XSLT 2.0 and having trouble wrapping tags around parts of a string. 我在XSLT 2.0中工作,在将标签包装在字符串的一部分周围时遇到麻烦。 Here's what I've got: 这是我得到的:

<dc:subject>Red&#xD;
            Green&#xD;
            Blue
</dc:subject>

and desired output: 和所需的输出:

<subject>
  <topic>Red</topic>
  <topic>Green</topic>
  <topic>Blue</topic>
</subject>

I know I can use substring-before() to grab anything before the divider, but I don't know how to use it recursively to get each instance before the divider; 我知道我可以使用substring-before()在分隔符之前抓取任何东西,但是我不知道如何递归地使用它来在分隔符之前获取每个实例。 on the other hand, since I'm in 2.0 I can use replace() to hit all occurrences, but it won't accept an angle bracket so I can't just tell it to wrap each occurrence in the <topic> tags. 另一方面,由于我使用的是2.0版本,因此可以使用replace()击中所有匹配项,但是它不会接受尖括号,因此我不能仅仅告诉它将每个匹配项都包装在<topic>标记中。 What am I missing here? 我在这里想念什么?

<xsl:value-of select="replace(dc:subject, '.*&#xD;','????')"/>

or 要么

<xsl:value-of select="substring-before(dc:subject, '&#xD;')"/>

Thank you! 谢谢!

You are creating new nodes here, so it is not the right approach to use replace which is purely about replacing strings with other strings. 您在此处创建新节点,因此使用replace并不是正确的方法,而纯粹是将字符串替换为其他字符串。

You can use tokenize here to split the string, and then use xsl:for-each to create your new topic for each part 您可以在此处使用tokenize来拆分字符串,然后使用xsl:for-each为每个部分创建新topic

<subject>
  <xsl:for-each select="tokenize(dc:subject, '&#xD;')">
     <topic>
       <xsl:value-of select="normalize-space(.)" />
     </topic>
  </xsl:for-each>
</subject>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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