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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:27:39+00:00 2026-05-23T18:27:39+00:00

I have a source xml that contains the addresses in spot and need to

  • 0

I have a source xml that contains the addresses in spot and need to transform into an xml that holds all addresses into a single element and references each one.
I am using Saxon 9.1 processor and stylesheet version 1.0.
Thank you for helping.

Source Code:

<?xml version="1.0" encoding="utf-8"?>
<ContactDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <AddressDetails StartDate="1992-04-03" Type="Previous">
        <Address>
            <City City="Wien" />
            <Postcode Postcode="LSP-123" />
        </Address>
    </AddressDetails>
    <AddressDetails StartDate="1982-09-19" Type="Current">
        <Address>
            <City City="Toronto" />
            <Postcode Postcode="LKT-947" />
        </Address>
    </AddressDetails>
    <AddressDetails StartDate="1977-05-27" Type="Mailing">
        <Address>
            <City City="Sydney" />
            <Postcode Postcode="OKU-846" />
        </Address>
    </AddressDetails>
</ContactDetails>

Target Code:

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ContactDetails>
        <AddressDetails StartDate="1992-04-03" Type="Previous">
            <AddressRef ReferedID="Prev_1" />
        </AddressDetails>
        <AddressDetails StartDate="1982-09-19" Type="Current">
            <AddressRef ReferedID="Curr_2" />
        </AddressDetails>
        <AddressDetails StartDate="1977-05-27" Type="Mailing">
            <AddressRef ReferedID="Mail_3" />
        </AddressDetails>
    </ContactDetails>
    <AddressSegment>
            <Address>
                <ID ID="Prev_1" />
                <City City="Wien" />
                <Postcode Postcode="LSP-123" />
            </Address>
            <Address>
                <ID UniqueID="Curr_2" />
                <City City="Toronto" />
                <Postcode Postcode="LKT-947" />
            </Address>        
            <Address>
                <ID UniqueID="Mail_3" />
                <City City="Sydney" />
                <Postcode Postcode="OKU-846" />
            </Address>        
    </AddressSegment>
</Application>

Have played with key and generate-id as I was trying to Generate the ID’s first and copy them in the address. Here is my last trial of the xslt (best result I got was to have the UniqueID’s empty so I have no idea how far off this solution is 🙂 )

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="ID_key" match="*[@ReferedID]" use="@ReferedID"/>
<xsl:template match="/">
    <Application>
    <ContactDetails>
        <xsl:for-each select="Application/Person/ContactDetails/AddressDetails">
        <AddressDetails>
            <xsl:attribute name="StartDate">
            <xsl:value-of select="@StartDate"/>
            </xsl:attribute>
                        <xsl:attribute name="Type">
                            <xsl:value-of select="@Type" />
            </xsl:attribute>
                <AddressRef>
            <xsl:attribute name="ReferedID">
                <xsl:value-of select="generate-id()"/>
            </xsl:attribute>
            </AddressRef>
        </AddressDetails>
        </xsl:for-each>
    </ContactDetails>
    <AddressSegment>
        <xsl:for-each select="Application/Person/ContactDetails/AddressDetails">
        <Address>
            <ID>
            <xsl:attribute name="UniqueID">
                <xsl:value-of select="Address/ID[generate-id()=generate-id(key('ID_key',@UniqueID))]" />
            </xsl:attribute>
            </ID>
            <City>
            <xsl:attribute name="City">
                <xsl:value-of select="Address/City/@City"/>
            </xsl:attribute>
            </City>
                        <Postcode>
                                    <sl:attribute name="Postcode">
                    <xsl:value-of select="Address/Postcode/@Postcode"/>
                    </xsl:attribute>
            </Postcode>
        </Address>
            </xsl:for-each>
     </AddressSegment>
   </Application>
   </xsl:template>
</xsl:stylesheet>
  • 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-23T18:27:40+00:00Added an answer on May 23, 2026 at 6:27 pm

    To give you an example of how you could use generate-id and modes, the sample stylesheet

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:template match="ContactDetails">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <ContactDetails>
            <xsl:apply-templates select="AddressDetails/Address" mode="det"/>
          </ContactDetails>
          <AddressSegment>
            <xsl:apply-templates select="AddressDetails/Address"/>
          </AddressSegment>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="Address" mode="det">
        <AddressDetails StartDate="{../@StartDate}" Type="{../@Type}">
          <AddressRef ReferedID="{generate-id()}"/>
        </AddressDetails>
      </xsl:template>
    
      <xsl:template match="Address">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <ID ID="{generate-id()}"/>
          <xsl:copy-of select="*"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    transforms the input

    <?xml version="1.0" encoding="utf-8"?>
    <ContactDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <AddressDetails StartDate="1992-04-03" Type="Previous">
            <Address>
                <City City="Wien" />
                <Postcode Postcode="LSP-123" />
            </Address>
        </AddressDetails>
        <AddressDetails StartDate="1982-09-19" Type="Current">
            <Address>
                <City City="Toronto" />
                <Postcode Postcode="LKT-947" />
            </Address>
        </AddressDetails>
        <AddressDetails StartDate="1977-05-27" Type="Mailing">
            <Address>
                <City City="Sydney" />
                <Postcode Postcode="OKU-846" />
            </Address>
        </AddressDetails>
    </ContactDetails>
    

    with Saxon 6.5.5 into the output

    <ContactDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <ContactDetails>
          <AddressDetails StartDate="1992-04-03" Type="Previous">
             <AddressRef ReferedID="d0e3"/>
          </AddressDetails>
          <AddressDetails StartDate="1982-09-19" Type="Current">
             <AddressRef ReferedID="d0e7"/>
          </AddressDetails>
          <AddressDetails StartDate="1977-05-27" Type="Mailing">
             <AddressRef ReferedID="d0e11"/>
          </AddressDetails>
       </ContactDetails>
       <AddressSegment>
          <Address>
             <ID ID="d0e3"/>
             <City City="Wien"/>
             <Postcode Postcode="LSP-123"/>
          </Address>
          <Address>
             <ID ID="d0e7"/>
             <City City="Toronto"/>
             <Postcode Postcode="LKT-947"/>
          </Address>
          <Address>
             <ID ID="d0e11"/>
             <City City="Sydney"/>
             <Postcode Postcode="OKU-846"/>
          </Address>
       </AddressSegment>
    </ContactDetails>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a source xml document that uses namespace containing prefixes and a default namespace.
I have a source XML file that is used to create C# files that
I have a primary build file that is managed through source control. It contains
I have a WIX XML document that contains 2,000+ file tags. I am trying
I have a web application, that contains a configuration xml file for one of
Currently, I have a String object that contains XML elements: String carsInGarage = garage.getCars();
I have a class which expects a stream that contains an XML file. I
I have a csv file that contains XML entries. Imagine that each XML entry
I have a class that unmarshals xml from a 3rd party source (I have
I have the following XML source structure: <turnovers> <turnover repid=1 amount=500 rate=0.1/> <turnover repid=5

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.