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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:01:53+00:00 2026-06-06T17:01:53+00:00

I get my required xml elements in block.But at the same, i also put

  • 0

I get my required xml elements in block.But at the same, i also put block for catching other xml elements.But it is not worked for me…

This is XML Document

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<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="Heading1" /> 
        </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="Heading1" /> 
        </w:pPr>
        <w:r>
            <w:t>Text5-</w:t>
        </w:r>  
    </w:p>

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

This is XSLT File

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
 exclude-result-prefixes="w">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <Document>

      <xsl:variable name="headingName" select="(//w:body/w:p/w:pPr/w:pStyle[starts-with(@w:val, 'Heading')])[1]/@w:val"/>
      <xsl:variable name="topLevelHeadings" select = "//w:body/w:p[w:pPr/w:pStyle/@w:val = $headingName]"/>

      <xsl:choose>

        <xsl:when test="$topLevelHeadings">
          <Heading>
          <xsl:apply-templates select="$topLevelHeadings">
          </xsl:apply-templates>
          </Heading>
        </xsl:when>

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

      </xsl:choose>   

    </Document>
  </xsl:template>  

  <xsl:template match="w:p">
    <Paragraph>
      <xsl:apply-templates />
    </Paragraph>
  </xsl:template>

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

</xsl:stylesheet>

My Generated Output is:

<Document>
  <Heading>
  <Paragraph>Text2-</Paragraph> 
  <Paragraph>Text5-</Paragraph> 
  </Heading>
</Document>

But My Required output is:

<Document>
      <Paragraph>Text1-</Paragraph> 
      <Heading>
           <Paragraph>Text2-</Paragraph> 
      </Heading>
      <Paragraph>Text3-</Paragraph> 
      <Paragraph>Text4-</Paragraph> 
      <Heading>
           <Paragraph>Text5-</Paragraph> 
      </Heading>
</Document>

I think, I have some problem with Block. So, Please Guide me to get out of this issue…

  • 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-06T17:01:55+00:00Added an answer on June 6, 2026 at 5:01 pm

    The problem with your xsl:choose here is that it is only being used once, and it is checking only if there is at least one w:p element with a heading. So, you will only get one Heading element output. For you xsl:choose to work, you really need to use it within the w:p template

    <xsl:template match="w:p">
       <xsl:choose>
          <xsl:when test="w:pPr/w:pStyle[starts-with(@w:val, 'Heading')]">
             <Heading>
                <Paragraph>
                   <xsl:apply-templates/>
                </Paragraph>
             </Heading>
          </xsl:when>
          <xsl:otherwise>
             <Paragraph>
                <xsl:apply-templates/>
             </Paragraph>
          </xsl:otherwise>
       </xsl:choose>
    </xsl:template>
    

    However, you don’t really need the xsl:choose here. You can probably get away with just have a specific template to match w:p elements which have a matching w:pPr element

    <xsl:template match="w:p[w:pPr/w:pStyle[starts-with(@w:val, 'Heading')]]">
    

    In this template you can then output the Heading element. You would then have a separate template to match all other w:p elements to output the paragraph, and if you give it a name, you could call it from the previous template too to share code

    <xsl:template match="w:p" name="para"> 
    

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" exclude-result-prefixes="w">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:template match="/">
          <Document>
             <xsl:apply-templates/>
          </Document>
       </xsl:template>
    
       <xsl:template match="w:p[w:pPr/w:pStyle[starts-with(@w:val, 'Heading')]]">
          <Heading>
             <xsl:call-template name="para"/>
          </Heading>
       </xsl:template>
    
       <xsl:template match="w:p" name="para">
          <Paragraph>
             <xsl:apply-templates/>
          </Paragraph>
       </xsl:template>
    
       <xsl:template match="w:r/w:t">
          <xsl:value-of select="."/>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your input XML, the following is output

    <Document>
       <Paragraph>Text1-</Paragraph>
       <Heading>
          <Paragraph>Text2-</Paragraph>
       </Heading>
       <Paragraph>Text3-</Paragraph>
       <Paragraph>Text4-</Paragraph>
       <Heading>
          <Paragraph>Text5-</Paragraph>
       </Heading>
    </Document>
    

    Actually, the final template for <xsl:template match="w:r/w:t"> isn’t strictly needed, because the default behaviour for XSLT when it matches an element for which there is not a specific template is to output the text anyway.

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

Sidebar

Related Questions

I have an xml file which describes (among other things) elements with attribute values
This is probably something extremely simple but I can't get my head around it
I have an XML form with an element 0, which is well-formed but not
I have some source XML containing different elements that get transformed by a stylesheet
This is my XML Document. <?xml version=1.0 encoding=UTF-8 standalone=yes?> <w:document xmlns:w=http://schemas.openxmlformats.org/wordprocessingml/2006/main> <w:body> <w:p> <!--
Does anybody know which version of sed is required to get option -i to
I get the following error: One or more types required to compile a dynamic
I always get back Content Length Required 411, what am I doing wrong here?
i am trying to get the state of the required field validators in an
Is there a quick way to get a string containing the sql datatype required

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.