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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:50:33+00:00 2026-05-26T21:50:33+00:00

I am new to xsl. I want to transform an xml from, <result name=response

  • 0

I am new to xsl. I want to transform an xml from,

<result name="response" numFound="1" start="0">
  <doc>
    <str name="q">what</str>
    <arr name="suggestion">
                <str>what1</str>
                <str>what2</str>
   </arr>
  </doc>
</result>

to,

<result name="response" numFound="2" start="0">
  <doc>
    <str name="q">what</str>
    <str name="suggestion">what1</str>
  </doc>
  <doc>
    <str name="q">what</str>
    <str name="suggestion">what2</str>
  </doc>
</result>

I could extract the texts, “what1” and “what2” using,

<xsl:template match="/response/result[@name='response']">
             <xsl:for-each select="./doc/arr[@name='suggestion']/str">
                    <xsl:value-of select="normalize-space(.)"/>
                    <xsl:value-of select="$endl"/>
             </xsl:for-each>
    </xsl:template>

But I don’t know how to enclose the same into output xml format. Can anyone please help…

Additional feature:

Can anyone please tell if I could add a field to the output doc called <float name="score"> which would get incremented by 100 with each doc?

eg)
output:

   <response>
      <lst name="responseHeader">
        <int name="status">0</int>
        <int name="QTime">1</int>
        <lst name="params">
          <str name="indent">on</str>
          <str name="q">"what"</str>
        </lst>
      </lst>
      <result numFound="2" name="response" start="0">
        <doc>
          <str name="query">what</str>
          <str>what1</str>
          <float name="score">100</float>
        </doc>
        <doc>
          <str name="query">what</str>
          <str>what2</str>
          <float name="score">200</float>
        </doc>
        <doc>
          <str name="query">what</str>
          <str>what3</str>
          <float name="score">300</float>
        </doc>
      </result>
    </response>

Can you please tell what function do I suppose to use?

  • 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-26T21:50:34+00:00Added an answer on May 26, 2026 at 9:50 pm

    Here is a solution to the OP’s comment that updates the question:

    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="result">
      <result numFound="{count(doc/arr/str)}">
       <xsl:apply-templates select="@*[not(name()='numFound')]"/>
       <xsl:apply-templates/>
      </result>
     </xsl:template>
    
     <xsl:template match="arr[@name='suggestion']/str">
      <doc>
        <xsl:copy-of select="../../str"/>
        <xsl:copy-of select="."/>
      </doc>
     </xsl:template>
    
     <xsl:template match="doc|doc/arr">
      <xsl:apply-templates/>
     </xsl:template>
     <xsl:template match="doc/str "/>
    </xsl:stylesheet>
    

    when applied on the following (provided in the OP’s comment) XML document:

    <response>
        <lst name="responseHeader">
            <int name="status">0</int>
            <int name="QTime">1</int>
            <lst name="params">
                <str name="indent">on</str>
                <str name="q">"what"</str>
            </lst>
        </lst>
        <result name="response" numFound="1" start="0">
            <doc>
                <str name="q">what</str>
                <arr name="suggestion">
                    <str>what1</str>
                    <str>what2</str>
                </arr>
            </doc>
        </result>
    </response>
    

    produces the wanted, correct result:

    <response>
       <lst name="responseHeader">
          <int name="status">0</int>
          <int name="QTime">1</int>
          <lst name="params">
             <str name="indent">on</str>
             <str name="q">"what"</str>
          </lst>
       </lst>
       <result numFound="2" name="response" start="0">
          <doc>
             <str name="q">what</str>
             <str>what1</str>
          </doc>
          <doc>
             <str name="q">what</str>
             <str>what2</str>
          </doc>
       </result>
    </response>
    

    Update:

    In an update to his question, the OP requested:

    Can anyone please tell if I could add a field to the output doc called
    <float name="score"> which would get incremented by 100 with each
    doc?

    This is extremely easy to achieve. We add just three lines of code to the existing 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="result">
        <result numFound="{count(doc/arr/str)}">
            <xsl:apply-templates select="@*[not(name()='numFound')]"/>
            <xsl:apply-templates/>
        </result>
     </xsl:template>
    
     <xsl:template match="arr[@name='suggestion']/str">
        <doc>
            <xsl:copy-of select="../../str"/>
            <xsl:copy-of select="."/>
            <float name="score">
             <xsl:value-of select="100*position()"/>
            </float>    
        </doc>
     </xsl:template>
    
     <xsl:template match="doc|doc/arr">
        <xsl:apply-templates/>
     </xsl:template>
     <xsl:template match="doc/str "/>
    </xsl:stylesheet>
    

    When this transformation is applied to this XML document:

    <response>
        <lst name="responseHeader">
            <int name="status">0</int>
            <int name="QTime">1</int>
            <lst name="params">
                <str name="indent">on</str>
                <str name="q">"what"</str>
            </lst>
        </lst>
        <result name="response" numFound="1" start="0">
            <doc>
                <str name="q">what</str>
                <arr name="suggestion">
                    <str>what1</str>
                    <str>what2</str>
                    <str>what3</str>
                </arr>
            </doc>
        </result>
    </response>
    

    the wanted, correct result is produced:

    <response>
       <lst name="responseHeader">
          <int name="status">0</int>
          <int name="QTime">1</int>
          <lst name="params">
             <str name="indent">on</str>
             <str name="q">"what"</str>
          </lst>
       </lst>
       <result numFound="3" name="response" start="0">
          <doc>
             <str name="q">what</str>
             <str>what1</str>
             <float name="score">100</float>
          </doc>
          <doc>
             <str name="q">what</str>
             <str>what2</str>
             <float name="score">200</float>
          </doc>
          <doc>
             <str name="q">what</str>
             <str>what3</str>
             <float name="score">300</float>
          </doc>
       </result>
    </response>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to transform some xml using an xsl-file and output the result somehow
This code:: xslProcessor = new XSLTProcessor(); xslProcessor.importStylesheet(xsl); result = xslProcessor.transformToFragment(xml, document); works fine in
I am using Transformer to perform XSL transformation from XML to XHTML: import javax.xml.transform.Transformer;
Very new to XSL (and XML for that matter), but I need to step
I'm new in xml, sorry for the dumb question. I'm trying to create xsl
I'm currently working on a project which uses XSL-Transformations to generate HTML from XML.
I want to modify the XSLT 1.0 transform in the attached xsl file to
I want to create a html list () from an xml using xslt. I
I'm doing a xstl transformation with saxon from an XML document. The doc is
I have a xsl template which can replace some xml values. Now I want

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.