0

I want to select String between < and >.

Input :

<p type="Endnote Text">&lt;p:endnote_bl1&gt;This is a bullet list in an endnote</p>
<p type="Endnote Text">&lt;p:endnote_bl2&gt;This is a bullet list in an endnote</p>
<p type="Endnote Text">&lt;p:endnote_bl3&gt;This is a bullet list in an endnote</p>

I want to select p:endnote_bl1,p:endnote_bl2, etc.. from the text. It means whatever text between &lt; and &gt;. How can I write the XPath for this.

2
  • Your starting tag is <p> but your closing tag is </tps:p>. Commented Jul 29, 2019 at 11:19
  • @JackFleeting really sorry I edit code again Commented Jul 29, 2019 at 11:22

2 Answers 2

1

In XSLT, using xpath, you can simply select all p elements (or tps:p elements, if you do have namespaces), and use substring-before and substring-after to extract the text, although do note this assumes one occurrence of each of &lt; and &gt;

<xsl:for-each select="//p[@type='Endnote Text']">
  <xsl:value-of select="substring-before(substring-after(., '&lt;'), '&gt;')" />
  <xsl:text>&#10;</xsl:text>
</xsl:for-each>

See it in action at http://xsltfiddle.liberty-development.net/bnnZX7

If you could use XSLT 2.0, you could do it without the xsl:for-each...

<xsl:value-of select="//p[@type='Endnote Text']/substring-before(substring-after(., '&lt;'), '&gt;')" separator="&#10;" />

Or you could also use replace in XSLT 2.0....

<xsl:value-of select="//p[@type='Endnote Text']/replace(., '&lt;(.+)&gt;.*', '$1')" separator="&#10;" />
Sign up to request clarification or add additional context in comments.

Comments

0

You could use just xpath, if we fix your xml like so:

<doc>
  <p type="Endnote Text">&lt;p:endnote_bl1&gt;This is a bullet list in an endnote</p>
  <p type="Endnote Text">&lt;p:endnote_bl2&gt;This is a bullet list in an endnote</p>
  <p type="Endnote Text">&lt;p:endnote_bl3&gt;This is a bullet list in an endnote</p>
</doc>

Then this expression,

doc/p/substring(./text(),2,13)

will select

p:endnote_bl1
p:endnote_bl2
p:endnote_bl3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.