[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Output common IDs


Subject: Re: [xsl] Output common IDs
From: Brandon Ibach <brandon.ibach@xxxxxxxxxxxxxxxxxxx>
Date: Wed, 26 Jan 2011 04:32:06 -0500

You don't really need to save the result of the first tokenize() call
to a variable, but you do need to tokenize on '\n', rather than '\t+',
in order to get each line in turn.  You'll also need to save the value
of each item in the outer for-each so you can access it in the inner
for-each.

Using normalize-space() before passing $FilteredPageIds to tokenize()
will get rid of the "blank" results caused by newlines at the start
and end of the text inside the "id" element.  This change will also
pretty much eliminate the need for the $CurrentPageId variable.
Finally, add a newline after printing each match.

    <xsl:template match="/AllIds">
        <xsl:for-each select="tokenize(text(),'\n')">
            <xsl:variable name="CurrentId" select="current()"/>
            <xsl:for-each
select="tokenize(normalize-space($FilteredPageIds),' ')">
                <xsl:if test="current() =
substring-before(normalize-space($CurrentId),' ')">
                    <xsl:value-of select="concat($CurrentId,'&#10;')"/>
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

Taking advantage of XSLT's abilities to match against sets and filter
the results of tokenize() with a predicate, we have the following
version.

    <xsl:template match="/AllIds">
        <xsl:variable name="PageIds"
select="tokenize(normalize-space($FilteredPageIds),' ')"/>
        <xsl:for-each
select="tokenize(text(),'\n')[substring-before(normalize-space(.),' ')
= $PageIds]">
            <xsl:value-of select="concat(.,'&#10;')"/>
        </xsl:for-each>
    </xsl:template>

-Brandon :)


On Wed, Jan 26, 2011 at 1:11 AM, Suresh <suresh.chinta@xxxxxxxxx> wrote:
> Hi,
>
> I have a xml file (PageIDs.xml) which contains several IDs. I have
> another file which contains ID and URI separated by a tab(AllIDs.xml)
>
> The question is, For each tokenized ID contained in PageIDs.xml file I
> want to test all IDs in AllIDs.xml and if ID test matches output
> matching line as text. This is simple for members in the list, I tried
> but I could not solve this using XSLT 2.0
>
>  PageIDs.xml
> -------------------
> <id>
> BD6131A5-527C-11DF-A29F-00144F3EA4A4
> DCA7D4CA-312D-11DF-A385-00144F3EA4A4
> DD762167-312D-11DF-A385-00144F3EA4A4
> DDB79742-312D-11DF-A385-00144F3EA4A4
> 3D74A11A-839A-11DF-82F6-00144F3EA4A4
> ...
> </id>
>
> AllIDs.xml
> ---------------
> <AllIds>
> DCA7D4CA-312D-11DF-A385-00144F3EA4A4    /accounts-overview/dkadoemfew
> 8ED29EEA-6460-11DF-8508-00144F3EA4A4    redirect.go?target=payxsxs
> EC2152EB-4593-11DF-BCB6-00144F3EA4A4  
 /accounts-overview/redirect.go?target=feljfweij
> 997A1170-474A-11DF-BCB6-00144F3EA4A4    redirect.go?target=woqiepoqie
> DD4AF2B0-312D-11DF-A385-00144F3EA4A4    /accounts-overview/e32i2je2
> BD6131A5-527C-11DF-A29F-00144F3EA4A4    /contactus/about-contact
> ...
> </AllIds>
>
> This is the XSLT I've written...
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>    xmlns:xs="http://www.w3.org/2001/XMLSchema"
> exclude-result-prefixes="xs" version="2.0">
>    <xsl:output method="text"/>
>    <xsl:variable name="FilteredPageIds" select="doc('pageIDs.xml')/id"/>
>
>    <xsl:template match="/AllIds">
>        <xsl:variable name="AllListedIds" select="tokenize(text(),'\t+')"/>
>        <xsl:for-each select="$AllListedIds">
>            <xsl:for-each select="tokenize($FilteredPageIds,'\n')">
>                <xsl:variable name="CurrentPageId"
> select="normalize-space(current())"/>
>                <xsl:if test="contains($CurrentPageId, $AllListedIds)">
>                    <xsl:value-of select="$AllListedIds"/>
>                </xsl:if>
>            </xsl:for-each>
>        </xsl:for-each>
>    </xsl:template>
> </xsl:stylesheet>
>
> This fails at contains because the item is a collection and I am not
> sure how to handle them.
>
> Expected output for IDs matching in PageIDs.xml,
>
> BD6131A5-527C-11DF-A29F-00144F3EA4A4    /contactus/about-contact
>
> Thanks.
> --
> Suresh


Current Thread
Keywords