extract text between single quotes in XSLT

2019-06-28 03:37发布

Would need to extract the text after the single quotes.

E.g.:

<div>('show')</div>

The variables is this:

<xsl:variable name="showName" select="substring-before(substring-after(//x:div, '%28%27'), '%27%29')" />

The result is none, the encoding caracters.

I think it can not be used %28%27 and %27%29. This is correct?.

Thanks.

标签: xslt
2条回答
疯言疯语
2楼-- · 2019-06-28 04:04

Techniques

If you are familiar with regular expression you could also use <xsl:analyze-string>.

Code Sample

Extract content between the single quotes. The content is later referred using regex-group(1) ) :

<xsl:analyze-string select="//x:div" regex="'(.*)'">
  <xsl:matching-substring>
    <xsl:value-of select="regex-group(1)"/>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <xsl:value-of select="."/>
  </xsl:non-matching-substring>
</xsl:analyze-string>
查看更多
地球回转人心会变
3楼-- · 2019-06-28 04:19

XSLT 1.0:

Just use:

 <xsl:variable name="vshowName" select=
 'substring-before(substring-after(/*, "&apos;"), "&apos;")'/>

Here is a complete transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:variable name="vshowName" select=
 'substring-before(substring-after(/*, "&apos;"), "&apos;")'/>

 <xsl:template match="/*">
  <xsl:value-of select="$vshowName"/>
 </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<div>('show')</div>

the wanted, correct result is produced:

show

XSLT 2.0:

In XSLT 2.0 one can use either of the following two approaches:

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

 <xsl:variable name="vPattern">
  ^[^']*'([^']*)'.*$
 </xsl:variable>

 <xsl:variable name="vshowName1" select="replace(/*, $vPattern, '$1', 'x')"/>

 <xsl:variable name="vshowName2" select=
 'replace(/*, "^[^&apos;]*&apos;([^&apos;]*)&apos;.*$", "$1", "x")'/>

 <xsl:template match="/*">
  <xsl:value-of select="$vshowName2"/>
===============
  <xsl:value-of select="$vshowName1"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the same XML document (above), the evaluation of both XPath expressions produces the same result:

show
===============
show

Do note:

If you want with a single Xpath expression (making xsl:analyze-string unnecessary) to extract/process all substrings surrounded by quotes (where there is unlimited possible number of such substrings), see this answer:

https://stackoverflow.com/a/15402185/36305

Finally, using xsl:analyze-string:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <xsl:analyze-string select="." regex="'(.*?)'">
   <xsl:matching-substring>
     <xsl:sequence select="regex-group(1)"/>
   </xsl:matching-substring>
  </xsl:analyze-string>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<div>('word1', 'word2', 'word3')</div>

the correct result is produced:

word1 word2 word3

This code (from another answer) is wrong:

<xsl:analyze-string select="//div" regex="'(.*)'">
  <xsl:matching-substring>
    <xsl:value-of select="regex-group(1)"/>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <xsl:value-of select="."/>
  </xsl:non-matching-substring>
</xsl:analyze-string>

When applied on the above XML document it produces:

(word1', 'word2', 'word3)
查看更多
登录 后发表回答