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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:50:16+00:00 2026-06-05T06:50:16+00:00

I have the following xml: <?xml version=1.0 encoding=UTF-8?> <abc1 formName=Form> <Level1> <Element1>ZZZ</Element1> <Element2> <SubElement1>Apples</SubElement1>

  • 0

I have the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<abc1 formName="Form">
    <Level1>
        <Element1>ZZZ</Element1>
        <Element2>
            <SubElement1>Apples</SubElement1>
            <SubElement2>Oranges</SubElement2>
            <SubElement3>Pears</SubElement3>
            <SubElement4>Blueberries</SubElement4>
            <SubElement5>Milkshakes</SubElement5>
        </Element2>
    </Level1>
    <Level1>
        <Element1>XXX</Element1>
        <Element2>
            <SubElement1>Apples</SubElement1>
            <SubElement2>Oranges</SubElement2>
            <SubElement3>Kiwifruit</SubElement3>
            <SubElement4>Blueberries</SubElement4>
            <SubElement5>Soda</SubElement5>
        </Element2>
    </Level1>
</abc1>

and what I need to be able to do is take a look at the values in some nodes, determine if they are different, and if so write some different html code with a page break between each section.

So, I need to compare the <Element1> values and see if they are different. The <Element1> values could be any text, and the number of <Element1>tags could be unlimited in the xml.

So in this case, we have two different <Element1> values: ‘ZZZ’ and ‘XXX’.

So something like the following if there are two ”:

<xsl:choose>
<xsl:when test="differentvalues = 'true'">
<!--  SomeHTMLCode!-->
<p style="page-break-after: always"/>
<!--  SomeHTMLCode!-->
</xsl:when>
<xsl:otherwise><!--  SomeHTMLCode!--></xsl:otherwise>
</xsl:choose>

but using the for-loop to search through the xml, and something like

<xsl:choose>
<xsl:when test="differentvalues = 'true'">
<!--  SomeHTMLCode!-->
<p style="page-break-after: always"/>
<!--  SomeHTMLCode!-->
<p style="page-break-after: always"/>
<!--  SomeHTMLCode!-->
</xsl:when>
<xsl:otherwise><!--  SomeHTMLCode!--></xsl:otherwise>
</xsl:choose>

if there are three different </Element1> values.

I’m not even sure if this can be done.
Thanks in advance for any help you can provide me with.

  • 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-05T06:50:17+00:00Added an answer on June 5, 2026 at 6:50 am

    This XPath expression gives you the wanted count of distinct values for Element1 (supposing an Element1 cannot have a descendent also Element1 (there is more efficient way to handle such problems using keys, but I wouldn’t talk about keys to a newbie):

    count(//Element1[not(. = preceding::Element1)])
    

    You can define a variable specifying the above value in its select attribute and then use this variable in the test attribute` of your conditional instructions.

    Even better, don’t use conditional instructions at all and specify the above XPath expression as part of a template match pattern:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/*[count(//Element1[not(. = preceding::Element1)]) = 2]">
         Two processing
     </xsl:template>
    
     <xsl:template match="/*[count(//Element1[not(. = preceding::Element1)]) = 3]">
         Three processing
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <abc1 formName="Form">
        <Level1>
            <Element1>ZZZ</Element1>
            <Element2>
                <SubElement1>Apples</SubElement1>
                <SubElement2>Oranges</SubElement2>
                <SubElement3>Pears</SubElement3>
                <SubElement4>Blueberries</SubElement4>
                <SubElement5>Milkshakes</SubElement5>
            </Element2>
        </Level1>
        <Level1>
            <Element1>XXX</Element1>
            <Element2>
                <SubElement1>Apples</SubElement1>
                <SubElement2>Oranges</SubElement2>
                <SubElement3>Kiwifruit</SubElement3>
                <SubElement4>Blueberries</SubElement4>
                <SubElement5>Soda</SubElement5>
            </Element2>
        </Level1>
    </abc1>
    

    the wanted, correct result is produced:

     Two processing
    

    When the same transformation is applied on the following XML document:

    <abc1 formName="Form">
        <Level1>
            <Element1>ZZZ</Element1>
            <Element2>
                <SubElement1>Apples</SubElement1>
                <SubElement2>Oranges</SubElement2>
                <SubElement3>Pears</SubElement3>
                <SubElement4>Blueberries</SubElement4>
                <SubElement5>Milkshakes</SubElement5>
            </Element2>
        </Level1>
        <Level1>
            <Element1>YYY</Element1>
            <Element2>
                <SubElement1>Apples</SubElement1>
                <SubElement2>Oranges</SubElement2>
                <SubElement3>Pears</SubElement3>
                <SubElement4>Blueberries</SubElement4>
                <SubElement5>Milkshakes</SubElement5>
            </Element2>
        </Level1>
        <Level1>
            <Element1>XXX</Element1>
            <Element2>
                <SubElement1>Apples</SubElement1>
                <SubElement2>Oranges</SubElement2>
                <SubElement3>Kiwifruit</SubElement3>
                <SubElement4>Blueberries</SubElement4>
                <SubElement5>Soda</SubElement5>
            </Element2>
        </Level1>
    </abc1>
    

    again the correct result is produced:

     Three processing
    

    Note:

    You need, of course, to substitute the bodies of the templates with your desired processing.

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

Sidebar

Related Questions

I have the following simplified XML: <?xml version="1.0" encoding="UTF-8" ?> <MATMAS05> <IDOC BEGIN="1"> <E1MARAM
I have the following simplified XML structure: <?xml version="1.0" encoding="UTF-8" ?> <INVOIC02> <IDOC BEGIN="1">
For example, say I have the following xml in a string: <?xml version=1.0 encoding=UTF-8?>
I have the following XML document <?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' xmlns:issues='http://schemas.google.com/projecthosting/issues/2009' gd:etag='W/DEAERH47eCl7ImA9WhZTFEQ.'><id>http://code.google.com/feeds/issues/p/chromium/issues/full/921</id><published>2008-09-03T22:51:22.000Z</published><updated>2011-03-19T01:05:05.000Z</updated><title>Incorrect rendering</title><content
I have the following simplified XML structure: <?xml version="1.0" encoding="UTF-8"?> <ExportData> <TransportHeader> <Timestamp>2011-01-16 06:00:33</Timestamp>
I have a following wsdl from a WCF service. <?xml version=1.0 encoding=utf-8?> <wsdl:definitions name=Service
I have the following XML: <?xml version=1.0 encoding=UTF-8?> <game> <name>Space Blaster</name> <description></description> <version>1</version> <fullscreen>false</fullscreen>
I have following XML schema: <?xml version=1.0 encoding=UTF-8 standalone=no?> <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema> <xs:element name=content type=contentType/>
I have the following XML: <?xml version=1.0 encoding=UTF-8?> <gnm:Workbook xmlns:gnm=http://www.gnumeric.org/v10.dtd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://www.gnumeric.org/v9.xsd> <office:document-meta xmlns:office=urn:oasis:names:tc:opendocument:xmlns:office:1.0
I have the following the layout file <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android xmlns:app=http://schemas.android.com/apk/res/com.company android:layout_width=fill_parent

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.