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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:34:52+00:00 2026-06-01T02:34:52+00:00

Hope find a guru’s help to figure out the next problem. I have two

  • 0

Hope find a guru’s help to figure out the next problem.

I have two xml files. Firts one here (text.xml):

<text>
<ref>Author1, Title1, Date1</ref>
<ref>Author75, Title75, Date2</ref>
<ref>Author2, Title2, Date2</ref>
<ref>Author3, Title3, Date3</ref>
<text>

And the second one like this (list.xml):

<list>
<bibl xml:id="1"><author>Author1</author><date>Date1</date></bibl>
<bibl xml:id="2"><author>Author2</author><date>Date2</date></bibl>
<bibl xml:id="3"><author>Author3</author><date>Date3</date></bibl>
</list>

I want to query text.xml and check against list.xml to add @xml:id (from list.xml) to <ref> (from text.xml) wich contain same Author and Date. If not, then just copy original <ref>.

So I want to obtain:

<ref xml:id="1">Author1, Title1, Date1</ref> 
<ref>Author75, Title75, Date2</ref>
<ref xml:id="2>Author2, Title2, Date2</ref>
etc.

My XSLT identify well all correpondence:

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

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ref">
        <xsl:variable name="ref" select="."/>
        <xsl:for-each select="document('list.xml')//bibl">
            <xsl:variable name="bibl" select="."/>
            <xsl:variable name="author" select="author"/>
            <xsl:variable name="date" select="date"/>
            <xsl:choose>
                <xsl:when test="contains($ref, $author) and contains($ref, $date)">
                    <ref>
                        <xsl:attribute name="xml:id">
                            <xsl:value-of select="$bibl/@xml:id"/>
                        </xsl:attribute>
                        <xsl:value-of select="$ref"/>
                    </ref>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="$ref"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

But, then there aren’t correpondence it’s not just copy right <ref>, but copy all <ref> the number of time I have <bibl> nodes in the second file.

So problem is in <xsl:otherwise><xsl:copy-of select="$ref"/></xsl:otherwise>.
Any ideas how I can obtain only this distinct value I need? I know it’s must be very simple actually and I try key, generate-id, for-each-group, distinct-values, but can’t figure it out.

  • 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-01T02:34:53+00:00Added an answer on June 1, 2026 at 2:34 am

    The problem is that you are creating a ref element for each iteration of the for-each loop whether there is a match or not.

    What you need to do in this case is create the ref element outside of the for-each and then only create the id attribute for the matching element inside the loop

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="ref">
          <xsl:variable name="ref" select="."/>
          <ref>
             <xsl:apply-templates select="@* "/>
             <xsl:for-each select="document('list.xml')//bibl">
                <xsl:variable name="bibl" select="."/>
                <xsl:variable name="author" select="author"/>
                <xsl:variable name="date" select="date"/>
                <xsl:choose>
                   <xsl:when test="contains($ref, $author) and contains($ref, $date)">
                      <xsl:attribute name="xml:id">
                         <xsl:value-of select="$bibl/@xml:id"/>
                      </xsl:attribute>
                   </xsl:when>
                </xsl:choose>
             </xsl:for-each>
             <xsl:apply-templates select="node()"/>
          </ref>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output

    <text>
       <ref xml:id="1">Author1, Title1, Date1</ref>
       <ref>Author75, Title75, Date2</ref>
       <ref xml:id="2">Author2, Title2, Date2</ref>
       <ref xml:id="3">Author3, Title3, Date3</ref>
    </text>
    

    However, your current method is not very efficient, as for each ref element you are iterating over all bibl elements. Another approach would be to extract the author and date from the ref elements, and then look up the bibl element directly

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="ref">
          <xsl:variable name="author" select="normalize-space(substring-before(., ','))"/>
          <xsl:variable name="date" select="normalize-space(substring-after(substring-after(., ','), ','))"/>
          <ref>
             <xsl:apply-templates select="@* "/>
             <xsl:apply-templates select="document('list.xml')//bibl[author=$author][date=$date]"/>
             <xsl:apply-templates select="node()"/>
          </ref>
       </xsl:template>
    
       <xsl:template match="bibl">
          <xsl:attribute name="xml:id">
             <xsl:value-of select="@xml:id"/>
          </xsl:attribute>
       </xsl:template>
    </xsl:stylesheet>
    

    This should also give the same results.

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

Sidebar

Related Questions

I hope I will find a help here. I have a quite complex MVC
I really hope some SQL guru out there can assist with this one (and
This is a challenge question / problem. Hope you find it interesing. Scenario: You
I hope someone can help me with this, as I'm unable to find the
I hope to find a way to get the value in the Nth column
Originaly posted a while back on a different forum, hope I can find a
I hope this question is not a duplicate but I didn't find anything equivalent
Hope I'm asking this correctly: I have a project Projects.Client I have my class
I'm monitoring a process with strace / ltrace in the hope to find and
I'd hope to find an example code to do a deep copying of objects

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.