Hello all,
I am trying to generate to two different counters for <id> while matching its
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