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 8277001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:33:30+00:00 2026-06-08T08:33:30+00:00

This is my XML document. I want to convert this XML document into another

  • 0

This is my XML document. I want to convert this XML document into another XML document (see required output XML section).

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                xmlns:v="urn:schemas-microsoft-com:vml">
    <w:body>
        <w:p>
            <w:pPr>
            </w:pPr>
            <w:r>
                <w:t>Text1-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
                <w:pStyle w:val="Heading2" /> 
            </w:pPr>
            <w:r>
                <w:t>Text2-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
            </w:pPr>
            <w:r>
                <w:t>Text3-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
            </w:pPr>
            <w:r>
                <w:t>Text4-</w:t>
            </w:r>  
        </w:p>

         <w:p>
            <w:pPr>
                <w:pStyle w:val="Heading3" /> 
            </w:pPr>
            <w:r>
                <w:t>Text2.1-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
                <w:pStyle w:val="Heading2" /> 
            </w:pPr>
            <w:r>
                <w:t>Text5-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
            </w:pPr>
            <w:r>
                <w:t>Text6-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
                <w:pStyle w:val="Heading3" /> 
            </w:pPr>
            <w:r>
                <w:t>Text7-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
            </w:pPr>
            <w:r>
                <w:t>Text8-</w:t>
            </w:r>  
        </w:p>  

        <w:p>
            <w:pPr>
                <w:pStyle w:val="Heading1" /> 
            </w:pPr>
            <w:r>
                <w:t>Text9-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
            </w:pPr>
            <w:r>
                <w:t>Text10-</w:t>
            </w:r>  
        </w:p>

<w:p>
            <w:pPr>
                <w:pStyle w:val="Heading2" /> 
            </w:pPr>
            <w:r>
                <w:t>Text11-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:pPr>
            </w:pPr>
            <w:r>
                <w:t>Text12-</w:t>
            </w:r>  
        </w:p>

        <w:p>
            <w:r>
                <w:pict>
                    <v:shape>
                        <v:textbox>
                            <w:txbxcontent>
                                <w:p>
                                    <w:pPr>
                                        <w:pStyle w:val="Heading1" /> 
                                    </w:pPr>
                                    <w:r>
                                        <w:t>   
                                            Drawing Description_1
                                        </w:t>
                                    </w:r>
                                </w:p>          
                                <w:p>
                                    <w:pPr>
                                        <w:pStyle w:val="Heading1" /> 
                                    </w:pPr>
                                    <w:r>
                                        <w:t>   
                                            Drawing Description_2
                                        </w:t>
                                    </w:r>
                                </w:p>
                            </w:txbxcontent>
                        </v:textbox>
                    </v:shape>
                </w:pict>
            </w:r>
        </w:p>

    </w:body>
    </w:document>

The required output XML is :

 <?xml version="1.0" encoding="utf-8"?><Document xmlns:v="urn:schemas-microsoft-com:vml" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
    <Paragraph>Text1-</Paragraph>
    <Heading2>
       <Title>Text2-</Title>
       <Paragraph>Text3-</Paragraph>
       <Paragraph>Text4-</Paragraph>
       <Heading3>
          <Title>Text2.1-</Title>
       </Heading3>
    </Heading2>
    <Heading2>
       <Title>Text5-</Title>
       <Paragraph>Text6-</Paragraph>
       <Heading3>
         <Title>Text7-</Title>
         <Paragraph>Text8-</Paragraph>
       </Heading3>
   </Heading2>
   <Heading1>
      <Title>Text9-</Title>
      <Paragraph>Text10-</Paragraph>
      <Heading2>
       <Title>Text11-</Title>
         <Paragraph>Text12-</Paragraph>
         <txtContentGroup>
              <Paragraph>Drawing Description_1</Paragraph>
              <Paragraph>Drawing Description_2</Paragraph>
         </txtContentGroup>
       </Heading2>
    </Heading1>
 </Document>

This is my XSLT 2.0 Implementation for this :

<xsl:stylesheet 
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xpath-default-namespace="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:mf="http://example.com/mf"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
  xmlns:user="http://http://stackoverflow.com/questions/11356668"
  exclude-result-prefixes="xs w fn mf user">

  <xsl:output indent="yes"/> 

  <xsl:function name="mf:group" as="element()*">
      <xsl:param name="paragraphs" as="element()*"/>
      <xsl:param name="level" as="xs:integer"/>

      <xsl:for-each-group select="$paragraphs" group-starting-with="p[pPr/pStyle/@w:val = concat('Heading', $level)]">   

        <xsl:choose>
            <xsl:when test="self::p[pPr/pStyle/@w:val = concat('Heading', $level)]">
                <xsl:element name="Heading{$level}">
                <Title>
                  <xsl:apply-templates select="./r/t"/>
                </Title>                

                <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
          </xsl:element>
        </xsl:when>

        <xsl:when test="current-group()[self::p[pPr/pStyle/@w:val = concat('Heading', $level + 1)]]">
          <xsl:sequence select="mf:group(current-group(), $level + 1)">
          </xsl:sequence>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="current-group()">
           </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:function>

  <xsl:template match="document">

    <Document>
       <xsl:sequence select="mf:group(body/p, 1)"/>
    </Document>

  </xsl:template>

  <xsl:template match="p">

      <xsl:choose>
           <xsl:when test="(./r/t)">
               <xsl:choose>
                 <xsl:when test="descendant::w:p[w:r/w:t] | descendant::w:p[//w:r/w:t[ancestor::w:pict]][2] ">         
                       <txtContentGroup>            

                                <Paragraph>              
                                    <xsl:apply-templates select="./r/t"/>            
                              </Paragraph>

                              <xsl:apply-templates select="descendant::w:p">
                              </xsl:apply-templates>  

                       </txtContentGroup>          
                </xsl:when>  

                <xsl:otherwise>         
                    <Paragraph>
                       <xsl:apply-templates select="./r/t"/>
                    </Paragraph>
        </xsl:otherwise>
      </xsl:choose>

      </xsl:when>
         <xsl:otherwise>
           <xsl:choose>
             <xsl:when test="descendant::w:p[not(ancestor::w:tbl)]">
               <xsl:apply-templates select="descendant::w:p">
               </xsl:apply-templates>
             </xsl:when>     
            </xsl:choose>
         </xsl:otherwise>
       </xsl:choose>
    </xsl:template>

  <xsl:template match="/r/t">
    <xsl:value-of select="."/>
  </xsl:template> 
</xsl:stylesheet>

But it generates the following output :

    <?xml version="1.0" encoding="utf-8"?><Document xmlns:v="urn:schemas-microsoft-com:vml" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
    <Paragraph>Text1-</Paragraph>
    <Heading2>
       <Title>Text2-</Title>
       <Paragraph>Text3-</Paragraph>
       <Paragraph>Text4-</Paragraph>
       <Heading3>
         <Title>Text2.1-</Title>
      </Heading3>
    </Heading2>
    <Heading2>
       <Title>Text5-</Title>
       <Paragraph>Text6-</Paragraph>
       <Heading3>
         <Title>Text7-</Title>
         <Paragraph>Text8-</Paragraph>
       </Heading3>
    </Heading2>
    <Heading1>
       <Title>Text9-</Title>
       <Paragraph>Text10-</Paragraph>
       <Heading2>
          <Title>Text11-</Title>
          <Paragraph>Text12-</Paragraph>
          <Paragraph>Drawing Description_1</Paragraph>
          <Paragraph>Drawing Description_2</Paragraph>
       </Heading2>
   </Heading1>
   </Document>

My condition is :

Whenever <w:p> elements contains more than one descendant::w:p[w:r/w:t] in it and also that descendant::w:p[w:r/w:t] has the ancestor of <w:pict> then I want to create the <txtContentGroup> elements and put all the descendant::w:p[w:r/w:t] inside it.

  • 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-08T08:33:32+00:00Added an answer on June 8, 2026 at 8:33 am

    With this kind of input you should be using template rules, not deeply nested xsl:choose instructions. Your English statement

    Whenever <w:p> elements contains more than one descendant::w:p[w:r/w:t] in it and also it has the ancestor of <w:pict> then I want to create the <txtContentGroup> elements and put all the descendant::w:p[w:r/w:t] inside it.

    maps directly to the template rule:

    <xsl:template match="w:pict//w:p[descendant::w:p[w:r/w:t][2]]">
      <textContentGroup>
        <xsl:apply-templates select="descendant::w:p[w:r/w:t]"/>
      </textContentGroup>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my XML document. I want to convert this XML document into another
I am trying to convert an xml document into an associative array. I want
In an XSLT, I want to convert an XML document to another one. The
I want to convert one xml file into another xml file using xslt.here, i
I have an XML document that has been created using utf-8 encoding. I want
I want to store an XML document in sessionStorage . When I do this
I want to convert an java string containing xml to a w3c dom document
I want to convert a single XML file into a human-readable format by JavaScript.
Hi I want to convert XML node and its child into a string with
I have a large XML document that I want to convert to a Java

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.