Search code examples
xmlxpathxpath-2.0

XPATH for combining multiple identical entries


Here is simple XML data. I am trying to get the LastName under Author tag.

Since there are multiple authors I am not aware of how many authors there will be in each entry. So I am looking for a way to concatenate all the entries with the tag "LastName" under the "Authors" section, no matter how many there are. Preferably separated by a delimiter.

<?xml version="1.0" encoding="UTF-8"?>
<d>
    <Author />
    <Conference />
    <Domain />
    <Journal />
    <Keyword />
    <Organization />
    <Publication>
        <EndIdx>1</EndIdx>
        <StartIdx>1</StartIdx>
        <TotalItem>112686</TotalItem>
        <Result>
            <Abstract />
            <Author>
                <Affiliation />
                <CitationCount>0</CitationCount>
                <DisplayPhotoURL />
                <FirstName>Jiawei</FirstName>
                <GIndex>0</GIndex>
                <HIndex>0</HIndex>
                <HomepageURL />
                <ID>594572</ID>
                <LastName>Han</LastName>
                <MiddleName />
                <NativeName />
                <PublicationCount>0</PublicationCount>
                <ResearchInterestDomain />
            </Author>
            <Author>
                <Affiliation />
                <CitationCount>0</CitationCount>
                <DisplayPhotoURL />
                <FirstName>Micheline</FirstName>
                <GIndex>0</GIndex>
                <HIndex>0</HIndex>
                <HomepageURL />
                <ID>2331044</ID>
                <LastName>Kamber</LastName>
                <MiddleName />
                <NativeName />
                <PublicationCount>0</PublicationCount>
                <ResearchInterestDomain />
            </Author>
            <CitationContext />
            <CitationCount>5979</CitationCount>
            <Conference />
            <DOI />
            <FullVersionURL />
            <ID>694978</ID>
            <Journal />
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>9033</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>9972</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>22078</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>35009</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>36239</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>38375</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>40483</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>41259</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <Keyword>
                <CitationCount>0</CitationCount>
                <ID>73998</ID>
                <Name />
                <PublicationCount>0</PublicationCount>
            </Keyword>
            <ReferenceCount>160</ReferenceCount>
            <Title>Data Mining: Concepts and Techniques</Title>
            <Type>1</Type>
            <Year>2000</Year>
        </Result>
    </Publication>
    <ResultCode>0</ResultCode>
    <Trend />
    <Version>1.1</Version>
</d>

Solution

  • XPath 2.0:

    string-join(.//Author/LastName, ', ')
    

    run it in the right context, for example per <Publication>.