Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 9th, 2013, 03:42 AM
Registered User
 
Join Date: Aug 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generate counters matching attribute values and chile element with "Dr."

Hello all,

I am trying to generate to two different counters for <id> while matching its
Code:
@idref
value to its corresponding @id in <contact> element. Below is sample XML (kept it that way for brevity) for more clarity. Conditions I am trying to look into are:

1. Match value of @idref in <id> element to its corresponding @id value in <contact>.
2. If matches and <contact> has <title> with value “Dr.” Generate counter (<xsl:number/> is what I have used).
3. <id> elements with same @idref value, if repeated should have the number (check idref=”0001”) below and should not be incremented.
4. If matches and <contact> do not have child <title> with value “Dr.” Initiate separate counter (<xsl:number/> is what I have used).

I have tried using grouping <xsl:for-each-group> along with distint-vlaues()/generate-id() but I am sure I am simply missing some basic logic here.

Any direction in this regard will be highly appreciated!

Input XML
==========
Code:
<?xml version="1.0" encoding="UTF-8"?>
<records>
    <contact id="0001">
        <title>Dr</title>
        <forename>John</forename>
        <surname>Smith</surname>
    </contact>
    <id idref="0002"/>
    <contact id="0002">
        <title>Ms</title>
        <forename>Amy</forename>
        <surname>Jones</surname>
    </contact>
    <id idref="0001"/>
    <contact id="0003">
        <title>Dr</title>
        <forename>XXXX</forename>
        <surname>Jones</surname>
    </contact>
    <id idref="0001"/>
    <contact id="0004">
        <!-- No title -->
        <forename>YYYY</forename>
        <surname>Jones</surname>
    </contact>
    <p>This is dummy para! 
        <id idref="0001"/>
        and <id idref="0002"/>
        also <id idref="0003"/>
        other
        <id idref="0004"/>
    </p>
</records>
Output Required
Code:
<?xml version="1.0" encoding="UTF-8"?>
<records>
    <contact id="0001">
        <title>Dr</title>
        <forename>John</forename>
        <surname>Smith</surname>
    </contact>
    <id idref="0002">1</id>
    <contact id="0002">
        <title>Ms</title>
        <forename>Amy</forename>
        <surname>Jones</surname>
    </contact>
    <id idref="0001" w_title="yes">1</id>
    <contact id="0003">
        <title>Dr</title>
        <forename>XXXX</forename>
        <surname>Jones</surname>
    </contact>
    <id idref="0001" w_title="yes">1</id>
    <contact id="0004">
        <!-- No title -->
        <forename>YYYY</forename>
        <surname>Jones</surname>
    </contact>
    <p>This is dummy para! 
        <id idref="0001" w_title="yes">1</id>
        and <id idref="0002">1</id>
        also <id idref="0003" w_title="yes">1</id>
        other
        <id idref="0004"/>
    </p>
</records>
XSLT tried
Code:
<?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 encoding="UTF-8" indent="yes" media-type="text/xml" method="xml"/>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
<xsl:template match="id">
    <xsl:for-each select=".">
    <xsl:variable name="id_ref" select="@idref"/>
        <xsl:choose>
            <xsl:when test="//contact[@id=$id_ref]/title[.='Dr']">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:attribute name="w_title">yes</xsl:attribute>
                    <xsl:number/>
                    <xsl:apply-templates/>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="." mode="other"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:template>

<xsl:template match="//id" mode="other">
    <xsl:variable name="id_ref" select="@idref"/>
    <xsl:choose>
        <xsl:when test="//contact[@id=$id_ref]/title[.!='Dr']">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates/>
                <xsl:number/>
            </xsl:copy>    
        </xsl:when>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>
Resulting Wrong output
Code:
<?xml version="1.0" encoding="UTF-8"?>
<records>
    <contact id="0001">
        <title>Dr</title>
        <forename>John</forename>
        <surname>Smith</surname>
    </contact>
    <id idref="0002">1</id>
    <contact id="0002">
        <title>Ms</title>
        <forename>Amy</forename>
        <surname>Jones</surname>
    </contact>
    <id idref="0001" w_title="yes">2</id>
    <contact id="0003">
        <title>Dr</title>
        <forename>XXXX</forename>
        <surname>Jones</surname>
    </contact>
    <id idref="0001" w_title="yes">3</id>
    <contact id="0004">
        <!-- No title -->
        <forename>YYYY</forename>
        <surname>Jones</surname>
    </contact>
    <p>This is dummy para! 
        <id idref="0001" w_title="yes">1</id>
        and <id idref="0002">2</id>
        also <id idref="0003" w_title="yes">3</id>
        other
        
    </p>
</records>
TIA,
Pankaj





Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting "margin-top" Attribute of <fo:region-body> ritagr XSLT 0 January 18th, 2013 05:37 PM
"type" attribute in <script> tag Latinsteps BOOK: Beginning JavaScript 4th Edition 2 September 6th, 2011 01:29 AM
Chapter 18: Getting "Failed to generate a user instance of SQL Server" error HungryCaterpillar BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 November 11th, 2009 01:56 PM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.