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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:07:41+00:00 2026-05-20T18:07:41+00:00

I’m transforming XML to XML using the following XSLT. I need to validate the

  • 0

I’m transforming XML to XML using the following XSLT. I need to validate the source XML for required element. If the sibling node’s value is missing for the required node then create a new node.
Here is the XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <Data Schema="XML A">
            <xsl:apply-templates/>
        </Data>
    </xsl:template>
    <xsl:template match="Attribute[not(Type=following::Type)]">
        <Attributes type="{Type}">
            <xsl:apply-templates 
                 select="../Attribute[Type=current()/Type]" mode="out"/>
        </Attributes>
    </xsl:template>
    <xsl:template match="Attribute" mode="out">
        <Attr id="{id}" name="{Name}" value="{Value}"/>
    </xsl:template>
    <xsl:template match="Attribute"/>
</xsl:stylesheet>

Here is the XML

<?xml version="1.0" encoding="windows-1252"?>
<XML>
    <Attributes>
        <Attribute>
            <id>331</id>
            <Name>Enviornment</Name>
            <Type>common</Type>
            <Value>Development</Value>
        </Attribute>
        <Attribute>
            <id>79</id>
            <Name>Retail</Name>
            <Type>common</Type>
            <Value></Value>
        </Attribute>
        <Attribute>
            <id>402</id>
            <Name>Gender</Name>
            <Type>category</Type>
            <Value>Men</Value>
        </Attribute>
    </Attributes>
</XML>

And if the required elements is missing then it should create the following XML. I have multiple required elements.

<?xml version="1.0" encoding="utf-8"?>
<Data Schema="XML A">
  <Attributes type="common">
    <Attr id="331" name="Enviornment" value="Development" />
    <Attr id="79" name="Retail" value="" />
  </Attributes>
  <Attributes type="category">
    <Attr id="402" name="Gender" value="Men" />
  </Attributes>
  <errorCodes>
    <errorCode>"value for Retail is missing."</errorCode>
  </errorCodes>
</Data>

And if it can be done using the following XSLT then it will be a big plus. Thanks in Advance.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="type" match="Attribute" use="Type"/>
    <xsl:template match="/">
        <Data Schema="XML A">
            <xsl:apply-templates select="XML/Attributes/Attribute">
                 <xsl:sort select="Type" order="descending"/>
            </xsl:apply-templates>
        </Data>
    </xsl:template>
    <xsl:template 
            match="Attribute[generate-id()=generate-id(key('type', Type)[1])]">
        <Attributes type="{Type}">
            <xsl:apply-templates 
                    select="../Attribute[Type=current()/Type]" mode="out"/>
        </Attributes>
    </xsl:template>
    <xsl:template match="Attribute" mode="out">
         <Attr id="{id}" name="{Name}" value="{Value}"/>
    </xsl:template>
    <xsl:template match="Attribute"/>
</xsl:stylesheet>
  • 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-20T18:07:42+00:00Added an answer on May 20, 2026 at 6:07 pm

    The following stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:key name="type" match="Attribute" use="Type"/>
        <xsl:template match="/">
            <Data Schema="XML A">
                <xsl:apply-templates select="XML/Attributes/Attribute">
                     <xsl:sort select="Type" order="descending"/>
                </xsl:apply-templates>
                <errorCodes>
                    <xsl:apply-templates select="XML/Attributes/Attribute" 
                                         mode="errors"/>
                </errorCodes>
            </Data>
        </xsl:template>
        <xsl:template 
                match="Attribute[generate-id()=generate-id(key('type', Type)[1])]">
            <Attributes type="{Type}">
                <xsl:apply-templates 
                        select="../Attribute[Type=current()/Type]" mode="out"/>
            </Attributes>
        </xsl:template>
        <xsl:template match="Attribute" mode="out">
             <Attr id="{id}" name="{Name}" value="{Value}"/>
        </xsl:template>
        <xsl:template match="Attribute"/>
        <xsl:template match="Attribute" mode="errors"/>
        <xsl:template match="Attribute[Value='']" mode="errors">
            <errorCode>"value for <xsl:value-of select="Name"/> is missing."</errorCode>
        </xsl:template>
    </xsl:stylesheet>
    

    Produces the desired output:

    <Data Schema="XML A">
        <Attributes type="common">
            <Attr id="331" name="Enviornment" value="Development" />
            <Attr id="79" name="Retail" value="" />
        </Attributes>
        <Attributes type="category">
            <Attr id="402" name="Gender" value="Men" />
        </Attributes>
        <errorCodes>
            <errorCode>"value for Retail is missing."</errorCode>
        </errorCodes>
    </Data>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have thousands of HTML files to process using Groovy/Java and I need to
In my XML file chapters tag has more chapter tag.i need to display chapters
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.