使用XSLT 2.0将字符串模式更改为元素

时间:2013-04-26 15:42:02

标签: xslt xpath xslt-2.0

我正在将嵌入的文本模式转换为元素。

我似乎所做的一切都是&lt;hit&gt;而不是<hit>

这是我的意见......

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <lst>
      <lst id="7c5d14cd1225d94ff8dd9cf06eb67f05">
         <arr name="note">
            <str>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tellus risus,
iaculis id convallis sagittis, convallis in purus. Suspendisse imperdiet, enim
aliquam varius gravida, eros sapien mattis metus, vel suscipit lacus lacus eget
sem. Nulla ullamcorper enim quis dolor pellentesque consectetur quis quis eros.
Pellentesque et urna @@pre@@corn@@post@@, vel gravida urna. Nulla facilisi. Sed condimentum
purus non magna tristique molestie. Aliquam vulputate lobortis cursus. Morbi
tincidunt lobortis feugiat. Praesent in sapien diam. Aliquam bibendum elit ut
massa tristique tincidunt eu vel nisl. Aenean @@pre@@corn@@post@@ nulla id urna laoreet tempus.
Etiam ultricies lacus a arcu ornare iaculis. In eget tempus nisi.
        </str>
         </arr>
      </lst>
      <lst id="0e18352acb70ff7c4441dfd201dd7cd1">
         <arr name="note">
            <str>
Proin vitae eleifend enim. Nullam quis mauris ipsum. Proin sem dolor, placerat
nec ornare et, pulvinar a arcu. Cras varius venenatis sapien, eu sagittis sem
laoreet nec. Sed congue elit et magna tincidunt in ultrices quam rhoncus.
Maecenas ullamcorper pellentesque lobortis. Nunc blandit semper neque, vel
rhoncus tortor lacinia nec. Praesent sed feugiat @@pre@@corn@@post@@. Integer vel arcu leo,
sit amet volutpat diam. Cras posuere tristique est, ut tristique nibh
sollicitudin et. Pellentesque vitae justo sapien, non imperdiet velit.
        </str>
         </arr>
      </lst>
   </lst>
</response>

我的结果应该是......

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <lst>
      <lst id="7c5d14cd1225d94ff8dd9cf06eb67f05">
         <arr name="note">
            <str>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tellus risus,
iaculis id convallis sagittis, convallis in purus. Suspendisse imperdiet, enim
aliquam varius gravida, eros sapien mattis metus, vel suscipit lacus lacus eget
sem. Nulla ullamcorper enim quis dolor pellentesque consectetur quis quis eros.
Pellentesque et urna <hit>corn</hit>, vel gravida urna. Nulla facilisi. Sed condimentum
purus non magna tristique molestie. Aliquam vulputate lobortis cursus. Morbi
tincidunt lobortis feugiat. Praesent in sapien diam. Aliquam bibendum elit ut
massa tristique tincidunt eu vel nisl. Aenean <hit>corn</hit> nulla id urna laoreet tempus.
Etiam ultricies lacus a arcu ornare iaculis. In eget tempus nisi.
        </str>
         </arr>
      </lst>
      <lst id="0e18352acb70ff7c4441dfd201dd7cd1">
         <arr name="note">
            <str>
Proin vitae eleifend enim. Nullam quis mauris ipsum. Proin sem dolor, placerat
nec ornare et, pulvinar a arcu. Cras varius venenatis sapien, eu sagittis sem
laoreet nec. Sed congue elit et magna tincidunt in ultrices quam rhoncus.
Maecenas ullamcorper pellentesque lobortis. Nunc blandit semper neque, vel
rhoncus tortor lacinia nec. Praesent sed feugiat <hit>corn</hit>. Integer vel arcu leo,
sit amet volutpat diam. Cras posuere tristique est, ut tristique nibh
sollicitudin et. Pellentesque vitae justo sapien, non imperdiet velit.
        </str>
         </arr>
      </lst>
   </lst>
</response>

1 个答案:

答案 0 :(得分:1)

基本上你会使用analyze-string例如

<xsl:template match="arr/str">
  <xsl:copy>
    <xsl:analyze-string select="." regex="@@pre@@(.+)@@post@@">
      <xsl:matching-substring>
        <hit>
          <xsl:value-of select="regex-group(1)"/>
        </hit>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:copy>
</xsl:template>

[edit]我已经更改了正则表达式,只是收集了不是@字符的任何内容,这里是一个完整的样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="arr/str">
  <xsl:copy>
    <xsl:analyze-string select="." regex="@@pre@@([^@]+)@@post@@">
      <xsl:matching-substring>
        <hit>
          <xsl:value-of select="regex-group(1)"/>
        </hit>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

如果一行中有多个模式,那么你应该得到更好的结果。如果它仍然无法正常工作,请发布更详细的输入数据。