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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:38:52+00:00 2026-05-15T18:38:52+00:00

I have a series of medium-sized XML documents, which are mainly text with a

  • 0

I have a series of medium-sized XML documents, which are mainly text with a few nodes representing macros to be expanded, e.g.:

<foo>Some text <macro>A1</macro> ... <macro>B2</macro> ...etc...</foo>

My goal is to replace each macro with the corresponding XML. Usually it’s a single <img> tag with different attributes, but it could be some other HTML as well.

The stylesheet is generated programatically, and one way to do that would be to have a template per macro, e.g.

<xsl:template match="macro[.='A1']">
    <!-- content goes here -->
</xsl:template>
<xsl:template match="macro[.='A2']">
    <!-- other content goes here -->
</xsl:template>
<xsl:template match="macro[.='B2']">
    <!-- etc... -->
</xsl:template>

It works just fine, but it can have up to a hundred macros and it’s not very performant (I’m using libxslt.) I’ve tried a couple of alternative such as:

<xsl:template match="macro">
    <xsl:choose>
        <xsl:when test=".='A1'">
            <!-- content goes here -->
        </xsl:when>
        <xsl:when test=".='A2'">
            <!-- other content goes here -->
        </xsl:when>
        <xsl:when test=".='B2'">
            <!-- etc... -->
        </xsl:when>
    </xsl:choose>
</xsl:template>

It’s slightly more performant. I have tried adding another level of branching such as:

<xsl:template match="macro">
    <xsl:choose>
        <xsl:when test="substring(.,1,1) = 'A'">
            <xsl:choose>
                <xsl:when test=".='A1'">
                    <!-- content goes here -->
                </xsl:when>
                <xsl:when test=".='A2'">
                    <!-- other content goes here -->
                </xsl:when>
            </xsl:choose>
        </xsl:when>
        <xsl:when test=".='B2'">
            <!-- etc... -->
        </xsl:when>
    </xsl:choose>
</xsl:template>

It loads slightly slower (the XSL is bigger and a bit more complicated) but it executes slightly faster (each branch can eliminate several cases.)

Now I am wondering, is there a better way to do that? I have around 50-100 macros. Normally, the transformation is executed using libxslt but I can’t use proprietary extensions from other XSLT engines.

Any input is welcome 🙂

  • 1 1 Answer
  • 1 View
  • 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-15T18:38:53+00:00Added an answer on May 15, 2026 at 6:38 pm

    This would be another way:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
        <xsl:variable name="dummy">
                <mac id="A1">success</mac>
                <mac id="A2">fail</mac>
                <mac id="B1">This <b>fail</b></mac>
                <mac id="B2">This <b>success</b></mac>
        </xsl:variable>
        <xsl:key name="macro" match="mac" use="@id"/>
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="macro">
            <xsl:variable name="me" select="."/>
            <xsl:for-each select="document('')">
                <xsl:copy-of select="key('macro',$me)/node()"/>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    

    Note: This was the performance: XML parse 1,805ms, XSL parse 0,483ms, Transform 0,215ms

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

Sidebar

Related Questions

I have a series of tables which I want to all have an order
I have a series of strings, which are two numbers: 1.782-100.799 -18.107-102.016 -17.504104.059 How
I have a series of text that contains mixed numbers (ie: a whole part
I have a series of delimited files, some of which have some bad data
I have a series of list items which contain data attributes to store information
I have a series of functions which populate a series of drop down boxes.
I have a series of input elements each with a few <div> tags separate.
I need to create a pdf file which will have series of images, where
I have a series of views which, in an editing mode, add UIButtons in
I have a series of Text- and Comboboxes along a jTable. I use the

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.