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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:09:44+00:00 2026-05-18T11:09:44+00:00

with the XML structure below: <root foo1=bar1 foo2=bar2 foo3=bar3> <foo1 foo1=bar1 /> <data> <foo1>bar1</foo1>

  • 0

with the XML structure below:

<root foo1="bar1" foo2="bar2" foo3="bar3">
    <foo1 foo1="bar1" />

    <data>
        <foo1>bar1</foo1>
        <foo2>bar2</foo2>
        <foo3>bar3</foo3>
    </data>
</root>

I would like to copy this XML structure into another one with some exception on attributes and/or node() names and get the following
result using XSLT 1.0:

<root foo1="bar1" foo2="bar2">    
    <data>
        <foo1>bar1</foo1>
        <foo3>bar3</foo3>
    </data>
</root>

My rules are:

1) Copy every root attributes except foo3

2) Copy every child nodes() unless the ones named foo1 and foo2

My actual XSL stylesheet. I managed to get the root attributes restriction working :

<xsl:template match="root">
    <root>
        <xsl:for-each select="./@*">
            <xsl:variable name="name" select="name()" />

            <xsl:if test="name() != 'foo3'">
                <xsl:attribute name="{$name}"><xsl:value-of select="." /></xsl:attribute>
            </xsl:if>
        </xsl:for-each>


    </root>
</xsl:template>

Also, one harder question:
What if I want to matches my attributes and nodes dynamically. I would like to specify server-side what
attributes and nodes() I would like to remove. It’s probably like generating a string that is then used in the <xsl:if>. I don’t know if that’s even possible.

Thank you.

  • 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-18T11:09:45+00:00Added an answer on May 18, 2026 at 11:09 am

    I would like to copy this XML
    structure into another one with some
    exception […]

    My rules are:

    1) Copy every root attributes except
    foo3

    2) Copy every child nodes() unless the
    ones named foo1 and foo2

    Update from comments:

    Hi, this almost work. Except that
    data/foo1 should be copied

    This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="root/@foo3|root/foo1|foo2"/>
    </xsl:stylesheet>
    

    Output:

    <root foo1="bar1" foo2="bar2">
        <data>
            <foo1>bar1</foo1>
            <foo3>bar3</foo3>
        </data>
    </root>
    

    Note: Overwriting identity rule with empty templates

    With the node names in parameter, this stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:param name="pStrip" select="'root/@foo3|root/foo1|foo2'"/>
        <xsl:template match="node()|@*" name="identity">
            <xsl:param name="pStripPaths" select="concat($pStrip,'|')"/>
            <xsl:param name="pNodePath">
                <xsl:call-template name="path"/>
                <xsl:text>|</xsl:text>
            </xsl:param>
            <xsl:variable name="vStripPath"
                          select="substring-before($pStripPaths,'|')"/>
            <xsl:choose>
                <xsl:when test="not($pStripPaths)">
                    <xsl:copy>
                        <xsl:apply-templates select="node()|@*"/>
                    </xsl:copy>
                </xsl:when>
                <xsl:when test="contains($pNodePath,concat('/',$vStripPath,'|'))"/>
                <xsl:otherwise>
                    <xsl:call-template name="identity">
                        <xsl:with-param name="pStripPaths"
                                        select="substring-after($pStripPaths,'|')"/>
                        <xsl:with-param name="pNodePath" select="$pNodePath"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
        <xsl:template match="node()" name="path" mode="path">
            <xsl:apply-templates select="parent::*" mode="path"/>
            <xsl:value-of select="concat('/',
                                         substring('@',
                                                   1 div (count(.|../@*)
                                                          = count(../@*))),
                                         name())"/>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <root foo1="bar1" foo2="bar2">
        <data>
            <foo1>bar1</foo1>
            <foo3>bar3</foo3>
        </data>
    </root>
    

    Note: In XML, the element name refers to the schema, mostly defining the hierarchie position, but yours is not the case.

    Edit: Just for fun, an XSLT 2.0 solution:

    <xsl:stylesheet version="2.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:xs="http://www.w3.org/2001/XMLSchema"
         xmlns:local="http://localhost">
        <xsl:param name="pStrip" select="'root/@foo3|root/foo1|foo2'"/>
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="*[local:match($pStrip,.)]|@*[local:match($pStrip,.)]"/>
        <xsl:function name="local:match" as="xs:boolean">
            <xsl:param name="pStripPaths" as="xs:string"/>
            <xsl:param name="pNode" as="item()"/>
            <xsl:variable name="vNodePath"
                          select="string-join(($pNode
                                                /ancestor::node()
                                                 /name(),
                                               if ($pNode instance of attribute())
                                               then concat('@',name($pNode))
                                               else name($pNode)),
                                               '/')"/>
            <xsl:sequence select="some $path in tokenize($pStripPaths,'\|')
                                      satisfies ends-with($vNodePath,
                                                          concat('/',$path))"/>
        </xsl:function>
    </xsl:stylesheet>
    

    Edit 2: All the stylesheet following the same string pattern.

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

Sidebar

Related Questions

I have a XML Structure that looks like this. <sales> <item name=Games sku=MIC28306200 iCat=28
I'm trying to deserialize an xml structure that looks like this: <somecontainer> <key1>Value1</key1> <key1>Value2</key1>
I'm working with an existing XML document which has a structure (in part) like
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
Given the following XML structure <html> <body> <div> <span>Test: Text2</span> </div> <div> <span>Test: Text3</span>
I have the following sample XML structure: <SavingAccounts> <SavingAccount> <ServiceOnline>yes</ServiceOnline> <ServiceViaPhone>no</ServiceViaPhone> </SavingAccount> <SavingAccount> <ServiceOnline>no</ServiceOnline>
I'm trying to design an XML document structure for my application. I want to
I need to create an XML schema that validates a tree structure of an
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
I have a huge bunch of XML files with the following structure: <Stuff1> <Content>someContent</name>

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.