[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Output common IDs

Subject: Re: Output common IDs
From: Brandon Ibach <brandon.ibach@xxxxxxxxxxxxxxxxxxx>
Date: Wed, 26 Jan 2011 04:32:06 -0500
Re:  Output common IDs
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.