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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:16:37+00:00 2026-05-21T05:16:37+00:00

<xsl:template match=location> <xsl:if test=not(preceding::location)> <table> <tr> <th>Name</th> <th>City</th> <th>State</th> <th>Zip Code</th> <th>Country</th> </tr> </xsl:if>

  • 0
<xsl:template match="location">
        <xsl:if test="not(preceding::location)">
            <table>
                <tr>
                    <th>Name</th>
                    <th>City</th>
                    <th>State</th>
                    <th>Zip Code</th>
                    <th>Country</th>
                </tr>
        </xsl:if>
        <tr>
            <td><xsl:value-of select=".//name"/></td>
            <td><xsl:value-of select=".//city"/></td>
            <td><xsl:value-of select=".//state"/></td>
            <td><xsl:value-of select=".//zip"/></td>
            <td><xsl:value-of select=".//countdy"/></td>
        </tr>
        <xsl:if test="not(following::location)">
            </table>
        </xsl:if>
    </xsl:template>

Is the any way to allow mismatched tags in XSLT… or is there another way to achieve the same desired effect?

  • 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-21T05:16:37+00:00Added an answer on May 21, 2026 at 5:16 am

    Like Dimitre said, there is no way to allow mismatched tags in XSLT. There shouldn’t be a reason to have mismatched tags though.

    Looking at your template, it looks like you’re trying to build an html table out of all the <location> elements of your XML instance. You’re trying to open the table at the first <location> and trying to close the table at the last <location>.

    The easiest way to do this is to open your table at a higher level (parent/ancestor) and then populate the table with the <location> data.

    Here’s a sample XML file that has 3 <location>s:

    <doc>
      <location>
        <name>name 1</name>
        <city>city 1</city>
        <state>state 1</state>
        <zip>zip 1</zip>
        <country>country 1</country>
      </location>
      <location>
        <name>name 2</name>
        <city>city 2</city>
        <state>state 2</state>
        <zip>zip 2</zip>
        <country>country 2</country>
      </location>
      <location>
        <name>name 3</name>
        <city>city 3</city>
        <state>state 3</state>
        <zip>zip 3</zip>
        <country>country 3</country>
      </location>
    </doc>
    

    Here’s a stylesheet that will create the table:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="doc">
        <!--The table is inserted here.-->
        <table>
          <tr>
            <th>Name</th>
            <th>City</th>
            <th>State</th>
            <th>Zip Code</th>
            <th>Country</th>
          </tr>
          <!--This is where we apply the templates to populate the rows.-->
          <xsl:apply-templates select="location"/>
        </table>
      </xsl:template>
    
      <!--This template populates the row(s).-->
      <xsl:template match="location">
        <tr>
          <td>
            <xsl:value-of select="name"/>
          </td>
          <td>
            <xsl:value-of select="city"/>
          </td>
          <td>
            <xsl:value-of select="state"/>
          </td>
          <td>
            <xsl:value-of select="zip"/>
          </td>
          <td>
            <xsl:value-of select="country"/>
          </td>
        </tr>
      </xsl:template>
    
    </xsl:stylesheet>
    

    This is the output:

    <table>
       <tr>
          <th>Name</th>
          <th>City</th>
          <th>State</th>
          <th>Zip Code</th>
          <th>Country</th>
       </tr>
       <tr>
          <td>name 1</td>
          <td>city 1</td>
          <td>state 1</td>
          <td>zip 1</td>
          <td>country 1</td>
       </tr>
       <tr>
          <td>name 2</td>
          <td>city 2</td>
          <td>state 2</td>
          <td>zip 2</td>
          <td>country 2</td>
       </tr>
       <tr>
          <td>name 3</td>
          <td>city 3</td>
          <td>state 3</td>
          <td>zip 3</td>
          <td>country 3</td>
       </tr>
    </table>
    

    If for some reason you needed to create the <table> at the first <location>, you could still do that. It would require more code though.

    The following stylesheet produces the same output as the first stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="/doc">
        <xsl:apply-templates/>
      </xsl:template>
    
      <!--The table is created at the first location and
        the first row is populated.-->
      <xsl:template match="location[1]">
        <table>
          <tr>
            <th>Name</th>
            <th>City</th>
            <th>State</th>
            <th>Zip Code</th>
            <th>Country</th>
          </tr>
          <xsl:call-template name="location-row"/>
          <!--Here is where we apply the other template to populate the other rows.
            Notice we use a "mode" to differentiate the template from the generic
            "location" template.-->
          <xsl:apply-templates select="following-sibling::location" mode="not-first"/>
        </table>
      </xsl:template>
    
      <!--This template will output the other rows.-->
      <xsl:template match="location" mode="not-first" name="location-row">
        <tr>
          <td>
            <xsl:value-of select="name"/>
          </td>
          <td>
            <xsl:value-of select="city"/>
          </td>
          <td>
            <xsl:value-of select="state"/>
          </td>
          <td>
            <xsl:value-of select="zip"/>
          </td>
          <td>
            <xsl:value-of select="country"/>
          </td>
        </tr>
      </xsl:template>
    
      <!--This generic template matches locations other than the first one. 
        Basically it is consuming it so we don't get duplicate output.--> 
      <xsl:template match="location"/>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following isn't working as intended: <xsl:template match=xs:complexType> <xsl:param name=prefix /> <xsl:if test=$prefix='core'> <xsl:variable
So I'm using the identity design pattern for XSLT: <xsl:template match=@*|node()> <xsl:copy> <xsl:apply-templates select=@*|node()[not(@visible='false')]/>
Implementing a condition in template match <xsl:template match="a[!(img)and(not(@id))]"> I want to write a template
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
<xsl:template match=element[child]> The above works. What is the real syntax for the following pseudo-syntax?
I have a variable from which I need to dynamically generate nodes <xsl:template match=banner_discount_1
I'm using: <xsl:template match=material_id | location_code></xsl:template> To get rid of elements in the source
<xsl:template match=o:CustomDocumentProperties> <xsl:copy> <xsl:apply-templates select =@*|node() /> </xsl:copy> </xsl:template> In word 2003, I am
I want to check variable in template match, is it possible? like: <xsl:template match=*:Item
I have an xsl:param that I'm trying to use to do a template match

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.