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

  • Home
  • SEARCH
  • 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 7833401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:41:30+00:00 2026-06-02T12:41:30+00:00

I have this xml: <?xml version=1.0 encoding=utf-8?> <xml> <head> <name>This is my XML</name> <version>This

  • 0

I have this xml:



    <?xml version="1.0" encoding="utf-8"?>
    <xml>
    <head>
        <name>This is my XML</name>
        <version>This is my XML</version>   
    </head>
    <body>
        <item id="SH2-99435">
            <properties>
                <property id="69">
                    <name>This is a property</name>
                    <amount>54.13</amount>
                    <estructure id="IZ4">
                        <name>caterpillar</name>
                        <location><zipCode>02210</zipCode><street>South Station</street></location>
                    </estructure>
                </property>
                <features id="ABC3">
                    <feature>If it works, a bug is another feature.</feature>
                    <feature>feature!!</feature>
                </features>
            </properties>
            <coding>
                <codename>Silent Hill 2</codename>
                <developer>Team Silent</developer>
                <publisher>Konami</publisher>
            </coding>
        </item>
        <item id="SH3-4498">
            <text value="it has values like the other item"/>
        </item>       
        <item id="MGS-2">
            <text value="it has values like the other item"/>
        </item>
    </body>
    </xml>


And I want to achieve this:



    <?xml version="1.0" encoding="utf-8"?>
    <xml>
    <head>
        <name>This is my XML</name>
        <version>This is my XML</version>   
    </head>
    <body>
        <item>
            <properties>
                <property id="69">
                    <amount>54.13</amount>
                    <estructure id="IZ4">
                        <name>caterpillar</name>
                        <location><zipCode>02210</zipCode><street>South Station</street></location>
                    </estructure>
                </property>
            </properties>
        </item>
        <item id="SH3-4498">
            <text value="it should have properties and the selected sons"/>
        </item>       
        <item id="MGS-2" >
            <text value="it should have properties and the selected sons"/>
        </item>
    </body>   
    </xml>
    
    

I have something like this, which filters correctly:



    <?xml version="1.0" encoding="utf-8"?>
    <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="xml/body/item/coding" />
        <xsl:template match="xml/body/item/properties/features" />
        <xsl:template match="xml/body/item/properties/property/name" />

    </xsl:stylesheet>


I have been told that there are going to be 10 files being produced by running this filter, each one with different tags; but if a new tag comes, we would have to change 10 files to exclude the unwanted tag, instead of only add a tag on the file that is needed. For example, on the other file, only coding is going to be included, and so on.

I tried with this:

    

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns">
        <xsl:output omit-xml-declaration="yes" indent="yes" />
        <xsl:strip-space elements="*" />
        <ns:WhiteList>
            <name>amount</name>
            <name>estructure</name>
        </ns:WhiteList>
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
            </xsl:copy>
        </xsl:template>
        <xsl:template
            match="*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]" />
    </xsl:stylesheet>
    

But it doesn’t copy the children of estructure.
Do you know what I could do?

Thanks.

Update. Added justification to make it this way and not the other way around, and more descriptive question.

  • 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-02T12:41:33+00:00Added an answer on June 2, 2026 at 12:41 pm

    I wouldn’t accept this answer, but in answer to your immediate question, you are testing for elements which are ancestors (or self) of the estructure element, but you ignore anything else. This means descendants of the estructure are ignored. Change your xsl:if condition to the following

    <xsl:if 
       test="descendant-or-self::*[generate-id(.)=$copyValue2]|ancestor::*[generate-id(.)=$copyValue2]">
    

    Yuck.

    Honestly, your first XSLT is much nicer and cleaner.

    It would probably help if you explained exactly what the rules are you are trying to follow with the transformation. If the rules are to copy everything except certain nodes, then your first XSLT is ideal for that. If the rules are copy only explicit nodes, then could you give a bit more detail as to what exactly needs to be copied, and it should be possible to rustle up something much nicer.

    EDIT:

    In response to the clarification, try this XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns">
       <xsl:output omit-xml-declaration="yes" indent="yes"/>
       <xsl:strip-space elements="*"/>
    
       <ns:WhiteList>
          <name>amount</name>
          <name>estructure</name>
       </ns:WhiteList>
    
       <xsl:variable name="whistList" select="document('')/*/ns:WhiteList" />
    
       <xsl:template match="@*">
          <xsl:copy/>
       </xsl:template>   
    
       <xsl:template match="*">
          <xsl:if test="descendant-or-self::*[name()=$whistList/*]|ancestor::*[name()=$whistList/*]">
             <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
             </xsl:copy>
          </xsl:if>
       </xsl:template>      
    </xsl:stylesheet>
    

    I have tried to tidy it up a bit by using a variable to hold the document look-up. Note the reason for using the xsl:if is that you can’t use variables in the template match.

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

Sidebar

Related Questions

I have this XML at http://localhost/file.xml : <?xml version=1.0 encoding=utf-8?> <val:Root xmlns:val=http://www.hw-group.com/XMLSchema/ste/values.xsd> <Agent> <Version>2.0.3</Version>
I have this XML : <?xml version=1.0 encoding=utf-8 ?> <IMPORT mode=FULL> .... </IMPORT> I'm
I have this XML: <?xml version=1.0 encoding=utf-8?> <ConfiguraCanale ID_Comando=1> <canaleDigitalOUTPUT ID_Canale=1 > <stato>0</stato> </canaleDigitalOUTPUT>
I have this xml file <?xml version=1.0 encoding=UTF-8?> <bo:C837ClaimParent xsi:type=bo:C837ClaimParent xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:bo=http://somelongpathHere/process/bo> <claimAux> ...
I have a main activity with this layout file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
I have this html doc: <?xml version=1.0 encoding=utf-8?><!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.1//EN http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd>
I have this JSF table: <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
From this XML source : <?xml version=1.0 encoding=utf-8 ?> <ROOT> <STRUCT> <COL order=1 nodeName=FOO/BAR
I have the following html page: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
I have the following JSF table: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE html PUBLIC -//W3C//DTD XHTML

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.