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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:22:04+00:00 2026-06-04T09:22:04+00:00

I’ve got a xml document, looking like this: <p> <c1 /> <c2 /> </p>

  • 0

I’ve got a xml document, looking like this:

<p>
  <c1 />
  <c2 />
</p>

The child elements c1 and c2 are optional, but for a processing step I need them to be existent. So I am trying to create a xslt stylesheet to add them as empty elements (the order of the children does not matter).

Here is my stylesheet:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="p[not(c1)]">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <c1 />
    </xsl:copy>
</xsl:template>

<xsl:template match="p[not(c2)]">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <c2 />
    </xsl:copy>
</xsl:template>

This works fine, as long as only one of the child elements is missing. But if both are missing, only c1 is created. How do I prevent that and force the creation of both c1 and c2 (and in reality, of about 10 children)?

Thanks.
Jost

  • 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-04T09:22:05+00:00Added an answer on June 4, 2026 at 9:22 am

    I would do it like this:

    <xsl:template match="p"> 
      <xsl:copy> 
        <xsl:apply-templates select="@*|node()"/> 
        <xsl:if test="not(c1)">
          <c1 /> 
        </xsl:if>
        <xsl:if test="not(c2)">
          <c2 /> 
        </xsl:if>
      </xsl:copy> 
    </xsl:template> 
    

    If you have a longer list of possible child nodes you can put them in a variable and use a for-each instead than individual if:

    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
      exclude-result-prefixes="msxsl">
    
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:variable name="childrenFragment">
        <c1/>
        <c2/>
      </xsl:variable>
    
      <xsl:template match="p">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
          <xsl:variable name="this" select="."/>
          <xsl:for-each select="msxsl:node-set($childrenFragment)/*">
            <xsl:variable name="localName" select="local-name()"/>
            <xsl:if test="not($this/*[local-name()=$localName])">
              <xsl:element name="{$localName}"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    just add all the elements you need in the childrenFragment variable.

    (the msxsl:node-set stuff is Microsoft-specific, if you are using another XSLT processor you’ll need something slightly different)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

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.