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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:07:02+00:00 2026-06-09T10:07:02+00:00

I have to convert from one XML (XHTML) file to another using XSLT. The

  • 0

I have to convert from one XML (XHTML) file to another using XSLT. The rules of transformation are:

  1. <div id="ta12" class="bl" style="dis:bl"> has to be replaced with <div class="pass" value="50">
  2. The values of id=”t0b” and “t1b” have to be replaced with id=”ta0b8″ and “ta3b8” respectively.
  3. <input type="radio" name="o0" id="t0"/> has to be replaced with <input type="radio" name="key0b8" value="0" id="ta0q" class="block" /> (And likewise in the file)

Input file:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
    </head>  
    <body>
      <div class="iDev">
      <div id="ta12" class="bl" style="dis:bl"></div>

        <div class="q">
          <div id="t0b" class="block">1<span style="color">TEXT1</span>
          </div><br />
          T <input type="radio" name="o0" id="t0"/> 
          F <input type="radio" name="op0" id="f0"/>
          <div id="sfb"></div>
        </div><br />

        <div class="q">
          <div id="t1b" class="block">2<span style="color">TEXT2</span>
          </div><br />
          T <input type="radio" name="o1" id="t1" /> 
          F <input type="radio" name="op1" id="f1" />
          <div id="sfb"></div>
        </div>
      </div>
    </body>
    </html>

Output File:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8" />
</head>
<body>
  <div class="iDev">
  <div class="pass" value="50"></div>

    <div class="q">
      <div id="ta0b8" class="block">1<span style="color">TEXT1</span>
      </div><br />
      T<input type="radio" name="key0b8" value="0" id="ta0q" />
      F<input type="radio" name="key0b8" value="1" id="ta1q" />
      <div id="sfb"></div>
    </div><br />

    <div class="q">
      <div id="ta3b8" class="block">2 <span style="color">TEXT2</span>
      </div><br />
      T<input type="radio" name="key3b8" value="0" id="ta0q3" />
      F<input type="radio" name="key3b8" value="1" id="ta1q3" />
      <div id="sfb"></div>
    </div>
  </div>
</body>
</html>

In my XSLT I have created an identity template which includes the entire input file and then I’m trying to do the required modifications. I’m able to do the first task by-

<xsl:template match="xhtml:div[@id='ta12']">
  <xsl:attribute name="class">pa</xsl:attribute>
  <xsl:attribute name="value">10</xsl:attribute>
</xsl:template>

In the output it produces the required Div tag but it removes the <div class="iDev"> tag. Can anyone please tell me the solution for producing the desired output from the given input. Thanking you!

  • 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-09T10:07:04+00:00Added an answer on June 9, 2026 at 10:07 am

    I am just going to address your first rule, as this appears to be the focus of your question. If you need help with rules 2 and 3, ask separate questions on them please.

    In general, the pattern of solution for XSLT 1.0 to copy an element (non-deeply) will as shown following. By non-deeply, I mean dropping any child nodes.

    Listing 1

    <xsl:template match="some-element-pattern">
      <xsl:copy>
       <xsl:copy-of select="@*" />
       <xsl:attribute name="my-attrib-to-set">value-to-set</xsl:attribute>
      </xsl:copy>
    </xsl:template>
    

    Alternatively, you can replace the xsl:copy-of with xsl:apply-templates if there is some potential for other attribute processing other that straight copying. xsl:apply-templates is the more generic form.

    So applying this solution pattern to your case, the template you want is …

    Listing 2

    <xsl:template match="xhtml:div[@id='ta12']">
      <xsl:copy>
       <xsl:copy-of select="@*" />
       <xsl:attribute name="class">pa</xsl:attribute>
       <xsl:attribute name="value">10</xsl:attribute>
      </xsl:copy>
    </xsl:template>
    

    NB. In a previous solution, I may have given you this inefficient template…

    Listing 3

    <xsl:template match="xhtml:div[@id='ta12']">
      <xsl:copy>
       <xsl:apply-templates select="@*[not(@class)]" />
       <xsl:attribute name="class">pa</xsl:attribute>
      </xsl:copy>
    </xsl:template>
    

    I now realize that this is wrong. The predicate not(@class) is pointless and always returns true, as the attribute:: axis always returns an empty sequence when the focus item is an attribute. The thinking behind listing 3, was to eliminate the extraneous processing of the class attribute in cases where the matched input div element has a class attribute. If you really wanted to gain this kind of efficiency over listing 2, in XSLT 1.0, you could do as listing 4, but the code is some-what ugly, and I am not sure it is worth it.

    Listing 4

    <xsl:template match="xhtml:div[@id='ta12']">
      <xsl:copy>
       <xsl:apply-templates select="@*[local-name()!='class']
                                      [local-name()!='value']" />
       <xsl:attribute name="class">pa</xsl:attribute>
       <xsl:attribute name="value">10</xsl:attribute>
      </xsl:copy>
    </xsl:template>
    

    Listing 4 only works for class and value being in the null namespace. Some alternation is required if the attributes in question are in a namespace.

    And here is a final option for you. If you were prepared to sacrifice the generality of xsl:copy and wanted to suck on the juicy marrow of Attribute Value Templates, you could employ listing 5. In listing 5, you can replace the ‘pa’ and ‘va’ with Attribute Value Templates, as required. A further disadvantage of this solution, is that it now become necessary, not merely an efficiency measure, to ensure that @class and @value attributes are not processed within the child xsl:apply-templates, or they will over-write the intended literal values.

    Listing 5

    <xsl:template match="xhtml:div[@id='ta12']">
      <xhtml:div class="pa" value="va">
       <xsl:apply-templates select="@*[local-name()!='class']
                                      [local-name()!='value']" />
      </xhtml:div>
    </xsl:template>
    

    And finally, I know you are only interested in XSLT 1.0, but just for a bit of fun, I will mention the general pattern of solution for XSLT 2.0. This is shown in listing 6.

    Listing 6

    <xsl:template match="some-element-pattern">
      <xsl:copy>
       <xsl:apply-templates select="@* except @my-attrib-to-set" />
       <xsl:attribute name="my-attrib-to-set">value-to-set</xsl:attribute>
      </xsl:copy>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to convert from one XML (XHTML) file to another using XSLT. The
I have two XHTML files and my task is to convert from one to
So basically what I have been doing is convert one xml file to a
I'm writing an XSLT data map from one XML schema to another XML schema.
I have a class Something that implements ISomething . How can I convert/cast from
I've got some code to convert a large (many gigabytes) XML file into another
I am creating an xml and .blm file from one set of data, I
I leveraging the XmlSerializer to convert to/from XML. Here is an example class: [XmlRootAttribute(myClass)]
I have a text read from a XML file stored in UTF8 encoding. C#
I'm trying to convert an XML file into the markup used by dokuwiki, using

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.