Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3339784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:31:57+00:00 2026-05-18T00:31:57+00:00

I want to transform processing instructions in a source xml to some tag in

  • 0

I want to transform processing instructions in a source xml to some tag in an output

Input

<?xml version="1.0" encoding="utf-8"?>
<root>
    <?PI_start?> SOME TEXT <?PI_end?>
</root>

I want to have the output xml like that

<root>
    <tag> SOME TEXT </tag>
</root>

Can I do it? If yes what xsl must I use for transform?

I found only a way to transform PIs to the opening and closing tags. PI can contain some content.

Input XML

<root>
    <?PI SOME TEXT?>
</root>

XSL

<xsl:template match="processing-instruction('PI')">
    <tag><xsl:value-of select="."/></tag>
</xsl:template>

Output

<tag>SOME TEXT</tag>

But this is a bit not my case

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:31:57+00:00Added an answer on May 18, 2026 at 12:31 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="processing-instruction('PI_start')">
      <tag>
       <xsl:apply-templates mode="copy" select=
           "following-sibling::node()[1][self::text()]"/>
      </tag>
     </xsl:template>
    
     <xsl:template match=
     "processing-instruction('PI_end')
     |
      text()[preceding-sibling::node()[1]
                  [self::processing-instruction('PI_start')]]
     "/>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <?PI_start?> SOME TEXT <?PI_end?>
    </root>
    

    produces the wanted, correct result:

    <root>
       <tag> SOME TEXT </tag>
    </root>
    

    Do note:

    1. The identity rule is used to copy all nodes “as-is”.

    2. We have additional templates only for nodes that should be changed in some way.

    3. The template matching the first PI “does almost all the work”. It creates a tag element and applies templates to the following-sibling node if it is a PI.

    4. We apply templates in mode “copy” for the text node immediate sibling of the first PI.

    5. The mode “copy” isn’t declared anywhere and this causes the default template for processing text nodes to be selected — its action is to just copy the text node. This is a trick that saves us from the need to define a template in the “copy” mode.

    6. We have an empty template that actually deletes the unwanted nodes: the second PI and what would be a second copy of the first PI’s immediate-sibling text node.

    Update: The OP has indicated that he is also interested in the case where in-between the two PIs there might be different nodes (not only text nodes).

    This is a lot more complex task and here is one solution:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kSurrounded" match="node()"
      use="concat(
            generate-id(preceding-sibling::processing-instruction('PI_start')[1]),
            '+++',
            generate-id(following-sibling::processing-instruction('PI_end')[1])
                 )"/>
    
     <xsl:template match="node()|@*" name="identity">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="processing-instruction('PI_start')">
      <tag>
       <xsl:apply-templates mode="copy" select=
           "key('kSurrounded',
                 concat(generate-id(),
                       '+++',
                       generate-id(following-sibling::processing-instruction('PI_end')[1])
                       )
                 )"/>
      </tag>
     </xsl:template>
    
     <xsl:template match=
     "processing-instruction('PI_end')
     |
      node()[(preceding-sibling::processing-instruction('PI_start')
             |
              preceding-sibling::processing-instruction('PI_end')
              )
               [last()][self::processing-instruction('PI_start')]
            and
             (following-sibling::processing-instruction('PI_start')
            |
              following-sibling::processing-instruction('PI_end')
              )
               [1][self::processing-instruction('PI_end')]
            ]
     "/>
    
     <xsl:template match="node()" mode="copy">
      <xsl:call-template name="identity"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when the above transformation is applied on the following XML document:

    <root>
        <?PI_start?> <strong>Some</strong> TEXT <?PI_end?> XA <?PI_end?>
    </root>
    

    the wanted, correct output is produced:

    <root>
        <tag>
            <strong>Some</strong> TEXT 
        </tag> XA 
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to transform an XML document. The source XML looks like this: <svc:ElementList>
I want to pull a series of relationships out of xml files and transform
I want to read an xml file, apply a transform, then write to another
I want to use ImageJ to do some processing of several thousand images. Is
I want to implement some image-processing algorithms which are intended to run on a
Is there a way to avoid processing of already processed nodes? Input XML <?xml
I want to do some image processing work like face detection or something during
I want to transform /foo/bar/.. to /foo Is there a bash command which does
I have IQueryable list of objects of type T which I want to transform
I'm trying to canonicalize the representation of some XML data by sorting each element's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.