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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:08:07+00:00 2026-05-18T22:08:07+00:00

I want to produce an HTML file like this example: English En Lorem ipsum

  • 0

I want to produce an HTML file like this example:

English

Alt

En Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Nunc rutrum, eros sit amet ornare faucibus.

Français

Fr Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Nunc rutrum, eros sit amet ornare faucibus.

From the following example XML source, on which I have no control:

<?xml version="1.0" encoding="ISO-8859-1"?>
<content id="">
    <header language="en">
        <enabled>true</enabled>
        <img src="https://i.stack.imgur.com/xGCNw.gif" />
        <!-- more header-related elements -->
    </header>
    <header language="fr">
        <enabled>false</enabled>
        <img src="" />
        <!-- more header-related elements -->
    </header>
    <html language="en" type="source">
    <![CDATA[
        <p>En Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        <p>Nunc rutrum, eros sit amet ornare faucibus.</p>
        ]]>
    </html>
    <html language="fr" type="source">
    <![CDATA[
        <p>Fr Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        <p>Nunc rutrum, eros sit amet ornare faucibus.</p>
        ]]>
    </html>
</content>

So I wrote this XSLT to do it:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <h1>English</h1>
        <hr/>
        <xsl:for-each select="content/header">
            <xsl:call-template name="header">
                <xsl:with-param name="lang">en</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="content/html">
            <xsl:call-template name="html">
                <xsl:with-param name="lang">en</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <hr/>
        <h1>Français</h1>
        <hr/>
        <xsl:for-each select="content/header">
            <xsl:call-template name="header">
                <xsl:with-param name="lang">fr</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
        <xsl:for-each select="content/html">
            <xsl:call-template name="html">
                <xsl:with-param name="lang">fr</xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="header" match="*">
        <xsl:param name='lang'/>
        <xsl:if test="current()[@language=$lang]">
            <xsl:if test="enabled[normalize-space(text())='true']">
                <xsl:call-template name="image"/>
                <!-- more header-related elements -->
            </xsl:if>
        </xsl:if>
    </xsl:template>

    <xsl:template name="image" match="*">
        <xsl:if test="img[not(normalize-space(@src)='')]">
            <xsl:copy-of select="img"/>
        </xsl:if>
    </xsl:template>

    <xsl:template name="html" match="*">
        <xsl:param name='lang'/>
        <xsl:if test="current()[@language=$lang]">
            <xsl:value-of select="node()" disable-output-escaping="yes" />
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

That said, my solution feels very verbose, but seems to enable useful composition and reutilization properties, which are two things very important to me, since I will need to transform lots of XML documents whose structure is similar to this example; and thus, if most of my XSLTs can reuse parts of other XSLTs, this will be very useful.

A few notes of interest:

  • Transformation speed is of no importance, although it might be interesting to learn more on this aspect.
  • The transformations will never need to handle XML documents which encode more than the 2 example languages (en and fr).

So I would like to learn if my solution looks either anti-idiomatic-or even plain wrong-to you, XSLT wranglers.

  • 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-18T22:08:08+00:00Added an answer on May 18, 2026 at 10:08 pm

    This transformation doesn’t contain any conditional XSLT instructions or any named templates:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:my" exclude-result-prefixes="my">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <my:langs>
      <lang short="en">English</lang>
      <lang short="fr">Français</lang>
     </my:langs>
    
     <xsl:variable name="vLangs" select="document('')/*/my:langs/*"/>
    
     <xsl:template match="header">
      <xsl:apply-templates select="node()|@*"/>
      <hr/>
      <xsl:apply-templates select=
            "../html[@language=current()/@language]/text()"/>
     </xsl:template>
    
     <xsl:template match="header/@language">
      <h1><xsl:value-of select="$vLangs[@short=current()]"/></h1>
     </xsl:template>
    
     <xsl:template match="header[enabled='true']/img[@src and not(@src='')]">
      <xsl:copy-of select="."/>
     </xsl:template>
    
     <xsl:template match="html/text()">
      <xsl:value-of select="." disable-output-escaping="yes"/>
     </xsl:template>
    
     <xsl:template match="html|text()"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <content id="">
        <header language="en">
            <enabled>true</enabled>
            <img src="https://i.stack.imgur.com/xGCNw.gif" />
            <!-- more header-related elements -->
        </header>
        <header language="fr">
            <enabled>false</enabled>
            <img src="" />
            <!-- more header-related elements -->
        </header>
        <html language="en" type="source">
        <![CDATA[
                 <p>En Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
                 <p>Nunc rutrum, eros sit amet ornare faucibus.</p>
                 ]]>
       </html>
        <html language="fr" type="source">
        <![CDATA[
                <p>Fr Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
                <p>Nunc rutrum, eros sit amet ornare faucibus.</p>
                ]]>
     </html>
    </content>
    

    the wanted, correct result is produced:

    <h1>English</h1>
    <img src="https://i.stack.imgur.com/xGCNw.gif" />
    <hr />
    
                 <p>En Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
                 <p>Nunc rutrum, eros sit amet ornare faucibus.</p>
    
       <h1>Français</h1>
    <hr />
    
                <p>Fr Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
                <p>Nunc rutrum, eros sit amet ornare faucibus.</p>
    

    Do note:

    1. The use of templates and pattern matching.

    2. The use of <xsl:apply-templates>

    3. The use of predicates both in the match pattern of the templates and for selecting the necessary node-list in <xsl:apply-templates>

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

Sidebar

Related Questions

I have a on memory Datatable like this. I want to produce xml file
I'd like to extract the text from an HTML file using Python. I want
I have a list of mysql timestamps. I want to produce an html menu,
I want to use Facelets to build a static HTML prototype. This prototype will
I'm using http://tidy.sourceforge.net/ to convert HTML to XHTML and I want to transform this
I have an html form that looks something like this: <div class=field> <input id=product_name
I want to produce a desktop application with a very simple GUI (a background
I want to produce a list of all of the labels using the command
If I want to produce a Base64-encoded output, how would I do that in
I want a visual GUI designer that will produce XML output in the format

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.