Grabbing text between Processing Instructions in XSLT


Problem:

I'm trying to run an XSLT on the below XML to wrap text between certain PI's with a new element:

Input XML:

<doc>
   <p>
      <?addition?>hello<?addition?> text1
   </p>
   <p>text2</p>
   <p>
      <?addition?>bye<?addition?> text2
   </p>
</doc>

I am trying to wrap the text between the addition-start and addition-end in a <u> element like below.

Desired output:

<doc>
   <p>
      <u>hello</u> text1
   </p>
   <p>text2</p>
   <p>
      <u>bye</u> text2
   </p>
</doc>

My current XSLT is incorrectly grabbing text nodes that are not inbetween PI's, current stylesheet:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="#all"
    version="2.0">
    <xsl:template
        match="@* | node()">
        <xsl:copy
            copy-namespaces="no"
            exclude-result-prefixes="#all"
            inherit-namespaces="no">
            <xsl:apply-templates
                select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//p/text()[preceding::processing-instruction('addition')]
                                   [following::processing-instruction('addition')]">
        <u><xsl:value-of select="." /></u>
    </xsl:template>
</xsl:stylesheet>

Is there a way to tweak my XSLT to achieve this and wrap only the text surrounded by PI's with a <u>?


Solution:

I think you want

<xsl:template match="p/text()[preceding-sibling::node()[1][self::processing-instruction('addition')]]
                             [following-sibling::node()[1][self::processing-instruction('addition')]]">
    <u><xsl:value-of select="." /></u>
</xsl:template>

https://xsltfiddle.liberty-development.net/ehVZvvF

And, as Daniel Haley pointed out in his comment, you can furthermore add an empty template <xsl:template match="processing-instruction('addition')"/> to remove the processing instructions.

https://xsltfiddle.liberty-development.net/ehVZvvF/1

Recent Tips

  1. Foreign key refering to multiple tables
  2. Run a visual studio 2010 Windows7 project on a Windows XP machine
  3. How to apply groupname to HTML radio buttons in asp.net?
  4. What is the most stable modal dialog implementation across browsers to use from javascript for a web app?
  5. Move, Remove, and Replace information from Google Sheets with new entries using Google Script
  6. How can I get rid of jerkiness in WinForms scrolling animation?
  7. Update MySQL table column from another table entities
  8. Shoot fireball once in unity C#
  9. Adding a table of contents to a Microsoft Word Document using vbs
  10. Change locale in android app (onto Hindi)
  11. How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?
  12. Sharepoint with silverlight app
  13. What's the best way to write robots.txt for github pages using multiple repos?
  14. Biopython: Cant use .count() for biopython
  15. How can I find out the token balance of an address?
  16. ref value is undefined in vue (modal, textarea, $refs)
  17. Azure - HDInsight Hbase Data Insertion Failed
  18. SignalR overwriting OnConnected(), OnDisconnected()
  19. DatePickerDialog displays with two borders
  20. "type 'double' is not a subtype of type 'int' in type cast" error in flutter. What should i do?
  21. hiding the autocomplete list when user click outside the textbox is not working as expected
  22. JSF IceFaces basic problem with redisplaying input value
  23. How to validate material ui TextField in reactjs?
  24. Go and MongoDB connection won't work with panic log "no reachable server"
  25. WordPress Posts Pagination Not Working
  26. F# sprintf won't print in interactive console
  27. Spring Integration get FTP files recursively with outbound-gateway
  28. Jade mixins not getting working from external file
  29. Can not access defined exports from the webpack bundle?
  30. Completely new to Node.js - API Programming