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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:20:54+00:00 2026-06-17T23:20:54+00:00

I have a file.xml <?xml version=1.0?> <Report> <row> <field1>test1</field1> <field2>test2</field2> <field3>test3</field3> </row> <row> <field1>test4</field1>

  • 0

I have a file.xml

<?xml version="1.0"?>
<Report>
      <row>
            <field1>test1</field1>
            <field2>test2</field2>
            <field3>test3</field3>
      </row>
      <row>
            <field1>test4</field1>
            <field2>test5</field2>
            <field3>test6</field3>
      </row>
</Report>

And a lookup.xml

<?xml version="1.0"?>
<lookup>
      <field1>fieldA</field1>
      <field2>fieldB</field2>
      <field3>fieldC</field3>
</lookup>

I am trying to get following output

<?xml version="1.0"?>
<Report>
      <row>
            <fieldA>test1</fieldA>
            <fieldB>test2</fieldB>
            <fieldC>test3</fieldC>
      </row>
      <row>
            <fieldA>test4</fieldA>
            <fieldB>test5</fieldB>
            <fieldC>test6</fieldC>
      </row>
</Report>

So far I came up with following transform.xsl

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>



  <xsl:variable name="lookupDoc" select="document('lookup.xml')"/>

  <xsl:template match="Report">
    <Items>
      <xsl:apply-templates/>
    </Items>
  </xsl:template>

  <xsl:template match="row">
    <Item>
      <xsl:apply-templates/>
    </Item>
  </xsl:template>

  <xsl:template match="row/*">
    <xsl:variable name="this" select="."/>
    <xsl:variable name="lookup">
      <xsl:for-each select="$lookupDoc">
        <xsl:key name="k1" match="local-name()" use="text()"/>
        <xsl:value-of select="key('k1', local-name($this))"/>
      </xsl:for-each>
    </xsl:variable>
    <fieldName name="{$lookup}">
      <xsl:value-of select="."/>
    </fieldName>
  </xsl:template>

</xsl:stylesheet>

New to Xsl hence mot sure why getting compiler error

  • 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-17T23:20:55+00:00Added an answer on June 17, 2026 at 11:20 pm

    It looks like you had the right idea (and a novel one at that), but there were some places that needed fixing. Please try this:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:key name="k1" match="*" use="local-name()"/>
    
      <xsl:variable name="lookupDoc" select="document('lookup.xml')"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="row/*">
        <xsl:variable name="newName">
          <xsl:apply-templates select="$lookupDoc/lookup">
            <xsl:with-param name="nameToMatch" select="local-name()" />
          </xsl:apply-templates>
        </xsl:variable>
        <xsl:element name="{$newName}">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="lookup">
        <xsl:param name="nameToMatch" />
        <xsl:value-of select="string(key('k1', $nameToMatch))"/>
      </xsl:template>
    </xsl:stylesheet>
    

    In order for key() to locate values in the $lookupDoc DOM, key() needs to be used in the context of that DOM, and that’s what the last template is for. When this is run on your sample inputs the result is your requested output:

    <Report>
      <row>
        <fieldA>test1</fieldA>
        <fieldB>test2</fieldB>
        <fieldC>test3</fieldC>
      </row>
      <row>
        <fieldA>test4</fieldA>
        <fieldB>test5</fieldB>
        <fieldC>test6</fieldC>
      </row>
    </Report> 
    

    With some modification, it would have been also possible to use the for-each approach you were trying to use, since that’s another way to get inside the $lookupDoc DOM. The following XSLT should have the same result as the one above and is more similar to your original atttempt:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:key name="k1" match="*" use="local-name()"/>
    
      <xsl:variable name="lookupDoc" select="document('lookup.xml')"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="row/*">
        <xsl:variable name="this" select="." />
        <xsl:variable name="lookup">
          <xsl:for-each select="$lookupDoc">
            <xsl:value-of select="key('k1', local-name($this))"/>
          </xsl:for-each>
        </xsl:variable>
        <xsl:element name="{$lookup}">
          <xsl:value-of select="."/>
        </xsl:element>
      </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 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 am trying to transform an XML file into an updated version. I have
I have an XML attribute representing the version numbering of a file. The file
I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
i have a file Accelerator.xml <?xml version=1.0 encoding=UTF-8?> <os:openServiceDescription xmlns:os=http://www.microsoft.com/schemas/openservicedescription/1.0> <os:homepageUrl>http://www.sapo.pt</os:homepageUrl> <os:display> <os:name>Find on
I have a xml file in this format: <?xml version=1.0 encoding=ISO-8859-1?> <adp_report name=Average Draft
I have XML file which is looks like: <Report> <Total> <RecordValues> <Record> <FieldValue fieldName=index
I have an XML File <?xml version=1.0 encoding=UTF-8?> <data> <contact-information> <full-name>Peter John</full-name> <address_line_1>some place</address_line_1>
I have a file xml as this: <data> <first> <city> city </city> <people> 400
I have xml file with such structure: ... <outer> ... <inner/> ... </outer> ...

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.