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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:12:40+00:00 2026-06-10T19:12:40+00:00

Its me again, but this time i have a real problem… I have one

  • 0

Its me again, but this time i have a real problem…
I have one XML, and i have to transform it in another XML by using a filter of another XML

File_in.xml:

    <?xml version="1.0" encoding="ISO-8859-15"?>
    <root>
        <item>
            <server>001023541</server>
            <name>P1</name>
            <desc>Production</desc>
            <status>1</status>
            <ram>1024</ram>
            <hdd>8 To</hdd>
        </item>
        <item>
            <server>201012345</server>
            <name>P2</name>
            <desc>Production</desc>
            <status>4</status>
            <ram>2048</ram>
            <hdd>8 To</hdd>
        </item>
        <item>
            <server>120332416</server>
            <name>P1</name>
            <desc>Production</desc>
            <status>2</status>
            <ram>8196</ram>
            <hdd>8 To</hdd>
        </item>
    </root>

And another XML:

filter.xml

    <?xml version="1.0" encoding="ISO-8859-15"?>
    <Filtre>
        <Bloc5>
            <Part1>
                <EAN>001023541</EAN>
                <EAN>012356549</EAN>
                <EAN>012356559</EAN>
                <EAN>012356569</EAN>
            </Part1>
            <Part2>
                <EAN>201012345</EAN>
                <EAN>201012346</EAN>
                <EAN>201012347</EAN>
            </Part2>
        </Bloc5>
    </Filtre>

If /root/item/server matches with an element of /Filtre/Bloc5/Part1/EAN, i replace

        <item>
            <server>001023541</server>
            <name>P1</name>
            <desc>Production</desc>
            <status>1</status>
            <ram>1024</ram>
            <hdd>8 To</hdd>
        </item>

by

        <item>
            <server>MAIN</server>
            <status>PRODUCTION</status>
        </item>

otherwise if /root/item/server matches with an element of /Filtre/Bloc5/Part2/EAN, i replace

        <item>
            <server>201012345</server>
            <name>P2</name>
            <desc>Production</desc>
            <status>4</status>
            <ram>2048</ram>
            <hdd>8 To</hdd>
        </item>

by

        <item>
            <server>BACKUP</server>
            <status>STOPPED</status>
        </item>

and the other are automatically replaced like:

        <item>
            <server>120332416</server>
            <name>P1</name>
            <desc>Production</desc>
            <status>2</status>
            <ram>8196</ram>
            <hdd>8 To</hdd>
        </item>

by

        <item>
            <name>OFFLINE</name>
            <desc>Production</desc>
        </item>

I get the name of the XML (used to filter) by this way:

            <!-- filter settings -->
            <xsl:param name="filter_xml" />
            <xsl:variable name="filter" select="document('$filter_xml')" />

And there is my code :

        <xsl:for-each select="root/item">
            <xsl:choose>
                <xsl:when test="index-of(($filter/Filtre/Bloc5/Part1/EAN), ./server)">
                <item>
                    <server>MAIN</server>
                    <status>PRODUCTION</status>
                </item>
                </xsl:when>
            <xsl:when test="index-of(($filter/Filtre/Bloc5/Part2/EAN), ./server)" >
                <item>
                    <server>BACKUP</server>
                    <status>STOPPED</status>
                </item>
            </xsl:when >
            <xsl:otherwise>
                <item>
                    <name>OFFLINE</name>
                    <desc>Production</desc>
                </item>
            </xsl:otherwise>
        </xsl:for-each>

But this does not work…

  • 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-10T19:12:41+00:00Added an answer on June 10, 2026 at 7:12 pm

    A much cleaner, simpler and efficient solution is to use template matching instead of tortuous branching. I am assuming XSLT 2.0 because you did not tag your question with the XSLT version number, but if you need XSLT 1.0, then adjustments can easily be made.

    <xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes" />
    <xsl:strip-space elements="*" />
    
    <xsl:variable name="filter" select="document('filter.xml')/*" />
    
    <xsl:template match="/" >
     <root>
       <xsl:apply-templates select="*/item" />
     </root>
    </xsl:template>
    
    <xsl:template match="item[server=$filter/Bloc5/Part1/EAN]" >
      <item>
        <server>MAIN</server>
        <status>PRODUCTION</status>
      </item>
    </xsl:template>
    
    <xsl:template match="item[server=$filter/Bloc5/Part2/EAN]" >
      <item>
        <server>BACKUP</server>
        <status>STOPPED</status>
      </item>
    </xsl:template>
    
    <xsl:template match="item" >
      <item>
        <name>OFFLINE</name>
        <desc>Production</desc>
      </item>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Update: Icing on the cake

    Thanks for your acceptance. As icing on the cake, you could also replace the first two item templates with this ONE more generic and extensible template. But it’s debatable whether it is more simpler or more complex.

    Alternative generic item template (only works for XSLT 2.0) …

    <xsl:template match="item[server=$filter/Bloc5/*/EAN]" >
      <xsl:variable name="part-no"
        select="substring-after(local-name(($filter/Bloc5/*[EAN=current()/server])[1]),
                'Part') cast as xs:integer" />
      <xsl:copy>
        <server><xsl:value-of select="('MAIN'      ,'BACKUP' )[$part-no]" /></server>
        <status><xsl:value-of select="('PRODUCTION','STOPPED')[$part-no]" /></status>
      </xsl:copy>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

its me again with this project. im having a problem reading the text on
OK I'm stuck again, this time it's a problem with the regex... Was searching
Well, this is the exact opposite problem that I normally have with Javascript synchronicity
I am aware that System.Threading.Timer exists, but I already have a Thread. This thread
On our MOSS Enterprise environment, we have a strange problem. From time to time,
I have a stream of real time data from some HW handled by a
I have something weird going on with using UINib, although I suspect the real
It's growing pains time again. Some of our stuff requires FlashBuilder 4 and some
Hello once again stackoverflow folks, this is really hard and it's been torturing my
Its more of a best practices sort of question. Here it goes... I have

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.