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

  • SEARCH
  • Home
  • 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 8335169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:35:12+00:00 2026-06-09T03:35:12+00:00

I am transforming all   <p>   tags and renaming these nodes to  

  • 0

I am transforming all  <p>  tags and renaming these nodes to  <Body_Text>, on the condition that they are within the  <body>  element (using  xsl:if).

My code is doing this, however it is omitting all  <p>  elements that are outside of the  <body>  element, which I don’t want it to do.


Here is the XML to be transformed:

<root>
<p>asdf</p>
<body>
<p>asdfasdf</p>
<p>asdfasdf</p>
</body>
<footer>
<p>asdf</p>
</footer>
</root>

…here is the XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>

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

    <!-- Body_Text -->
    <xsl:template match="p">
      <xsl:if test="parent::body">
        <Body_Text>
          <xsl:apply-templates select="@*|node()"/>
        </Body_Text>
      </xsl:if>
    </xsl:template>

</xsl:stylesheet>

…and here is the undesired result (omitting all  <p>  elements that are outside of the  <body>  element, which I don’t want it to do):

<root>
<body>
<Body_Text>asdfasdf</Body_Text>
<Body_Text>asdfasdf</Body_Text>
</body>
<footer/>
</root>

This was tested at http://xslt.online-toolz.com/tools/xslt-transformation.php.


This is the desired result, which I am not getting:

<root>
<p>asdf</p>
<body>
<Body_Text>asdfasdf</Body_Text>
<Body_Text>asdfasdf</Body_Text>
</body>
<footer>
<p>asdf</p>
</footer>
</root>
  • 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-06-09T03:35:15+00:00Added an answer on June 9, 2026 at 3:35 am

    No xsl:if is needed — just specify proper template patterns.

    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="body//p">
      <Body_Text><xsl:apply-templates select="@*|node()"/></Body_Text>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <root>
        <p>asdf</p>
        <body>
            <p>asdfasdf</p>
            <p>asdfasdf</p>
        </body>
        <footer>
            <p>asdf</p>
        </footer>
    </root>
    

    produces the wanted, correct result:

    <root>
       <p>asdf</p>
       <body>
          <Body_Text>asdfasdf</Body_Text>
          <Body_Text>asdfasdf</Body_Text>
       </body>
       <footer>
          <p>asdf</p>
       </footer>
    </root>
    

    Explanation:

    The template pattern of:

     <xsl:template match="body//p">
    

    causes this template to be selected for execution only on p elements that are descendents of a body element. All the rest nodes are copied “as-is” by the identity rule.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that I'm transforming using XSL and then spitting out
I have an XSL that is transforming one XSD into another XSD that has
I am using xslt 2.0 for transforming one xml format to another xml format.But
I am using those codes for rotation transforming an image, but it doesn't work.
I recently started transforming all tables from our Oracle production into models so I
I am transforming some XML data to fit into document that is in a
How do I print out the attribute values of all message and lifeline nodes
I'm transforming an XHTML file to XML. I'm having issues that the XHML entity
We've got a mature body of code that loads data from files into a
How can I preserve entity references when transforming XML with XSLT (2.0)? With all

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.