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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:31:31+00:00 2026-06-07T22:31:31+00:00

I’m trying to create a xml using xslt. This is the xml that is

  • 0

I’m trying to create a xml using xslt.

This is the xml that is going to be transformed:



    <?xml version="1.0" encoding="utf-8"?>
    <document>
        <document_head>
            <title>This is the title</title>
            <version>This is the title</version>
        </document_head>
        <document_body>
            <paragraph id="TDKR">
                <text>
                    Oh no!, you're not Silvia, you're one of the kung fu creatures on the rampage... Two!
                </text>
                <properties>
                    <size>13px</size>
                    <color>#000000</color>
                </properties>
                <author>Current user</author>
            </paragraph>
            <paragraph id="AAHD">         
                <properties></properties>       
            </paragraph>
        </document_body>
    </document>


This is what I need:



    <?xml version="1.0" encoding="UTF-8"?>
    <document>
        <document_head>
            <title>This is the title</title>
            <version>This is the title</version>
        </document_head>
        <document_body>
            <paragraph>           
                <properties id="TDKR">
                    <size>13px</size>
                    <color>#000000</color>
                </properties>         
            </paragraph>
            <paragraph>           
                <properties id="AAHD">
                    <size></size>
                    <color></color>
                </properties>
            </paragraph>
        </document_body>
    </document>


This is the XSLT I have:



    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
        <xsl:output method="xml" indent="yes" omit-xml-declaration="no" />

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

        <!--START Copy of paragraph ID Attribute-->
            <xsl:template match="paragraph/properties" >
                <xsl:copy>
                    <xsl:apply-templates select="../@id | @* | node()"/>
                </xsl:copy>
            </xsl:template>
            <xsl:template match="paragraph">              
                <xsl:copy>
                    <xsl:apply-templates select="node()"/>
                </xsl:copy>
            </xsl:template>
        <!--END__ Copy of paragraph ID Attribute-->

        <!--START Filter of undesired elements--> 
            <xsl:template match="paragraph/text" />
            <xsl:template match="paragraph/author" />
        <!--END__ Filter of undesired elements--> 

        <!--START Creation of empty elements-->
            <xsl:variable name="empty" select="''"/> 

            <xsl:template match="*[not(node())]">
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()" />
                    <xsl:value-of select="$empty"/>
                </xsl:copy>
            </xsl:template>
        <!--END__ Creation of empty elements-->
    </xsl:stylesheet>


This is what the transformation produces:



    <?xml version="1.0" encoding="UTF-8"?>
    <document>
        <document_head>
            <title>This is the title</title>
            <version>This is the title</version>
        </document_head>
        <document_body>
            <paragraph>           
                <properties id="TDKR">
                    <size>13px</size>
                    <color>#000000</color>
                </properties>         
            </paragraph>
            <paragraph>           
                <properties/>     
            </paragraph>
        </document_body>
    </document>

Can anyone tell me what I can do to achieve this?

  • 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-07T22:31:32+00:00Added an answer on June 7, 2026 at 10:31 pm

    Easy one …

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*" />
    
    <xsl:template match="@*|node()">
     <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
    </xsl:template>
    
    <xsl:template match="text|author|paragraph/@id" />
    
    <xsl:template match="properties">
     <xsl:copy>
       <xsl:apply-templates select="@*"/>
       <xsl:attribute name="id"><xsl:value-of select="../@id" /></xsl:attribute>
       <xsl:apply-templates select="node()[not(self::size|self::color)]"/>
       <size>
         <xsl:apply-templates select="size/@*|size/node()" /> 
       </size>
       <color>
         <xsl:apply-templates select="color/@*|color/node()" /> 
       </color>
     </xsl:copy>  
    </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
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'm working with an upstream system that sometimes sends me text destined for HTML/XML
I know there's a lot of other questions out there that deal with this
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.