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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:02:19+00:00 2026-05-26T08:02:19+00:00

I’m working on a java code generation and the XSLT in which there are

  • 0

I’m working on a java code generation and the XSLT in which there are many of <xsl:if> or <xsl:choose> is not maintenable.

The field order is important for message, and the only way I’ve found to generate different field is to use switch case on different attribute.

This is an example XML data:

<?xml version="1.0" encoding="ISO-8859-1"?>
<message class="Message" length="28" id="0x12457836" 
                            package="org.goinfre.mail.data">
  <comment>bulk message</comment>
  <field name="state" type="short" size="2"/>
  <field name="reserve" size="2" reserved="true"/>
  <field name="reserve" size="1" reserved="true"/>
  <field name="identification" type="char">
    <array size="32" sizeName="IDENT_SIZE" encoding="UTF8" />
  </field>
  <field name="validity" type="int" size="4"/>
  <field name="voie" type="long" size="8"/>
</message>

The corresponding XSL is too long, but for example on each field I use an <xsl:choose> because the order of coding and decoding message’s buffer is important. I need to use each field more than once.

<xsl:template match="/message">
  [...]
  <xsl:apply-templates select="field" mode="generateField"/><xsl:text>
  [...]
@Override
public byte[] toByteArray() {
    ByteBuffer buffer = ByteBuffer.allocate(LENGTH);
    super.toByteArray(buffer);
</xsl:text>
<xsl:apply-templates select="field" mode="generateBuffer"/>
    <xsl:text>
    return buffer.array();
}
</xsl:template>
<!--
*****************************************************************
** Generate a private field declaration.
**************************************************************-->
<xsl:template match="field" mode="generateField">
    <xsl:choose>
        <xsl:when test="array">
private int <xsl:value-of select="array/@sizeName"/><xsl:text> = </xsl:text><xsl:value-of select="array/@size"/>;
private <xsl:value-of select="@type"/><xsl:text>[] </xsl:text><xsl:value-of select="@name"/>
            <xsl:text> = new </xsl:text><xsl:value-of select="@type"/>
            <xsl:text>[</xsl:text><xsl:value-of select="array/@sizeName"/><xsl:text>];</xsl:text>
        </xsl:when>
        <xsl:when test="not(@reserved)">
private <xsl:value-of select="@type"/><xsl:text> </xsl:text><xsl:value-of select="@name"/>;
        </xsl:when>
    </xsl:choose>
</xsl:template>

I want to use different elements (field-array, field-reserved, field…) for each field, but the order is not respected.

  • Is this a valid way to use XSL ?
  • Do you have any tips on how to generate code with XSLT?

Note: I can still change the format of the XML for now.

  • 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-26T08:02:20+00:00Added an answer on May 26, 2026 at 8:02 am

    Create more specialized templates. Move the criteria from your conditional statements that are inside of the template up into the match criteria of the template(s).

    Note: that multiple templates may match an element. If they have the same level of specificity in the match criteria, then they will get the same computed priority. You may have to specify @priority on the templates in order to “break the tie” and control which one should fire. In this instance, I added priority="1" to the template for field[array], to ensure that it matched instead of the one for field[not(@reserved)]`.

    For instance:

    <!--
    *****************************************************************
    ** Generate a private field declaration 
        for fields that have array elements.
    **************************************************************-->
    <xsl:template match="field[array]" priority="1" mode="generateField">
    
    private int <xsl:value-of select="array/@sizeName"/><xsl:text> = </xsl:text><xsl:value-of select="array/@size"/>;
    private <xsl:value-of select="@type"/><xsl:text>[] </xsl:text><xsl:value-of select="@name"/>
                <xsl:text> = new </xsl:text><xsl:value-of select="@type"/>
                <xsl:text>[</xsl:text><xsl:value-of select="array/@sizeName"/><xsl:text>];</xsl:text>
    
    </xsl:template>
    
    <!--
    *****************************************************************
    ** Generate a private field declaration
        for fields that do not have the attribute reserved.
    **************************************************************-->
    <xsl:template match="field[not(@reserved)]" mode="generateField">
    
    private <xsl:value-of select="@type"/><xsl:text> </xsl:text><xsl:value-of select="@name"/>;
    
    </xsl:template>
    

    It may also improve performance, as the match criteria has an opportunity to be optimized by XSLT processors.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.