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 January 22nd, 2009, 03:45 PM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Relating Information

In the following example, what would be the best way to render the Applicant and JointApplicant with different templates using xslt?

Code:
<MarriageLicence xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/">
    <LicenceNumber>E0587155</LicenceNumber>
    ...
    <Applicant>11</Applicant>
    <JointApplicant>12</JointApplicant>
    <MarriageLicence xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="Applicant">
        <id>11</id>
        ...
    </MarriageLicence>
    <MarriageLicence xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="Applicant">
        <id>12</id>
    </MarriageLicence>
</MarriageLicence>
I have come up with the following; however, I suspect there is a better method that would match the Applicant/JointApplicant fields with the id field in the Applicant data type.:
Code:
...
<xsl:apply-templates select="dcs:MarriageLicence[@i:type='Applicant']"/>
...

    <xsl:template name="Applicant" match="dcs:MarriageLicence[@i:type='Applicant']">
        <xsl:param name="ApplicantId"></xsl:param>
        <xsl:if test="position()=1">
            ...
        </xsl:if>
        <xsl:if test="position()=2">
            ...
        </xsl:if>
    </xsl:template>
I've had to hit the ground running with xslt and not had much to to RTFM. Thanks very much for the info.
 
Old January 23rd, 2009, 07:46 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't understand what you want to achieve respectively what the problem is. If you want to use two different templates for two different elements then you just need to write to templates matching those two different elements:
Code:
<xsl:template match="Applicant">
   <!-- transform Applicant elements here -->
</xsl:template>
<xsl:template match="JointApplicant">
   <!-- transform JoinApplicant elements here -->
</xsl:template>
then you only need to make sure the elements are processed e.g
<xsl:template match="MarriageLicense">
<xsl:apply-templates/>
</xsl:template>

If you still have problems then you might want to tell us what you want to transform your input XML to.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 23rd, 2009, 10:04 AM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My problem lies in how the data is related. If my xml was structured as such it would be easy:

Code:
<MarriageLicence>
  <Applicant>
    ...
  </Applicant>
  <JointApplicant>
    ...
  </JointApplicant>
</MarriageLicence>
However it is not. So simplify the xml I have it is more like this:
Code:
<MarriageLicence>
  <ApplicantId>11</ApplicantId>
  <JointApplicantId>12</JointApplicantId>
  <Applicant>
    <id>11</id>
    ...
  </Applicant>
  <Applicant>
    <id>12</id>
    ...
  </Applicant>
</MarriageLicence>
The xml is actually generated from serizlizing database objects. I have two database objects MarriageLicence and Applicant. The MarriageLicence has two foreign keys to the Applicant for the Applicant and JointApplicant.

My solution to use the first Applicant as the Applicant and the Second as the JointApplicant works so long as the Applicant/JointApplicant always show up the the correct order.

I have also tried the following; however it didn't work either:
Code:
<xsl:apply-templatesselect="dcs:MarriageLicence[@i:type='Applicant']">
  <xsl:with-paramname="ApplicantId">
    <xsl:value-ofselect="ApplicantId"/>
  </xsl:with-param>
</xsl:apply-templates>
...
<xsl:templatename="Applicant"match="dcs:MarriageLicence[@i:type='Applicant']">
  <xsl:paramname="ApplicantId"></xsl:param>
  <xsl:variablename="id">
    <xsl:value-ofselect="dcs:id"/>
  </xsl:variable>
  <xsl:iftest="$id = ApplicantId">
    ...

  </xsl:if>
</xsl:template>
Thanks,
Evan
 
Old January 23rd, 2009, 10:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Code:
<xsl:template match="Applicant[id = preceding-sibling::ApplicantId]">
  process the first applicant
</xsl:template>

<xsl:template match="Applicant[id = preceding-sibling::JointApplicantId]">
  process the second applicant
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 23rd, 2009, 12:36 PM
Registered User
 
Join Date: Jan 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks mhkay, your post has pointed me in a new direction; however, I still cant seem to get it to work. Perhaps because I used some simplified form of my xml in a previous post. Here is the xml

Code:
<MarriageLicence xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/">
    <LicenceNumber>E0587155</LicenceNumber>
    <DateIssued>2009-01-21T00:00:00</DateIssued>
    <IssuerId>2</IssuerId>
    <ProposedPlaceOfMarriage>Test City</ProposedPlaceOfMarriage>
    <MunicipalityWhereIssued>North Bay</MunicipalityWhereIssued>
    <SignatureOfLicenceIssuer i:nil="true" />
    <ProposedDateOfMarriage>2009-01-28T00:00:00</ProposedDateOfMarriage>
    <Applicant>11</Applicant>
    <JointApplicant>12</JointApplicant>
    <MarriageLicence xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="Applicant">
        <id>11</id>
        <LastName>Appleton</LastName>
        <FirstAndMiddleName>Albert</FirstAndMiddleName>
        <MaritalStatus>3</MaritalStatus>
        <CourtFileNumber>A1987645</CourtFileNumber>
        <CityDivorceGranted>New Denver</CityDivorceGranted>
        <ReligiousDenomination>Anglican</ReligiousDenomination>
        <DateOfBirth>1964-07-21T00:00:00</DateOfBirth>
        <BirthPlace>Jasper</BirthPlace>
        <FathersLastName>Appleton</FathersLastName>
        <FathersFirstName>Alphonso</FathersFirstName>
        <MothersMaidenName>Applegrove</MothersMaidenName>
        <MothersFirstName>Seedling</MothersFirstName>
        <FathersBirthPlace>Orilloia</FathersBirthPlace>
        <MothersBirthPlace>North Bay</MothersBirthPlace>
        <StreetNumber>778 </StreetNumber>
        <StreetName>SKI CLUB RD</StreetName>
        <Appartment>789</Appartment>
        <CityOrTown>North Bay</CityOrTown>
        <Province>ON</Province>
        <PostalCode>P1P3P4</PostalCode>
        <TelephoneNumber>1231231234</TelephoneNumber>
    </MarriageLicence>
    <MarriageLicence xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="Applicant">
        <id>12</id>
        <LastName>AppleCore</LastName>
        <FirstAndMiddleName>Amanda</FirstAndMiddleName>
        <MaritalStatus>3</MaritalStatus>
        <CourtFileNumber>BB7123</CourtFileNumber>
        <CityDivorceGranted>Alabama</CityDivorceGranted>
        <ReligiousDenomination>Catholic</ReligiousDenomination>
        <DateOfBirth>2009-01-06T00:00:00</DateOfBirth>
        <BirthPlace>Ontario</BirthPlace>
        <FathersLastName>AppleCore</FathersLastName>
        <FathersFirstName>Orangeman</FathersFirstName>
        <MothersMaidenName>Seedling</MothersMaidenName>
        <MothersFirstName>Rind</MothersFirstName>
        <FathersBirthPlace>Ontario</FathersBirthPlace>
        <MothersBirthPlace>British Columbia</MothersBirthPlace>
        <StreetNumber>889</StreetNumber>
        <StreetName>O'BRIEN ST</StreetName>
        <Appartment>2</Appartment>
        <CityOrTown>Vancouver</CityOrTown>
        <Province>AB</Province>
        <PostalCode>P1B2B3</PostalCode>
        <TelephoneNumber>7051234567</TelephoneNumber>
    </MarriageLicence>
    <MarriageLicence xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="Issuer">
        <id>2</id>
        <IssuerName>Fred Said</IssuerName>
        <Deleted>false</Deleted>
        <userId>cnb/fred</userId>
        <Deputy>false</Deputy>
    </MarriageLicence>
</MarriageLicence>
The xsl is:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:dcs="http://schemas.datacontract.org/2004/07/"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>
    <xsl:output method="xml"  indent="yes"/>

    <xsl:template match="dcs:MarriageLicence">
        <xsl:copy xmlns="http://schemas.datacontract.org/2004/07/">
            <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
            <f href="MarriageLicenceEn.pdf" />
            <fields>
                <field name="LicenceNumber">
                    <value><xsl:value-of select="dcs:LicenceNumber"/></value>
                </field>
                <field name="DateIssued">
                    <value><xsl:value-of select="dcs:DateIssued"/></value>
                </field>
                <xsl:apply-templates select="dcs:MarriageLicence[@i:type='Issuer']"/>
                <field name="ProposedPlaceOfMarriage">
                    <value><xsl:value-of select="dcs:ProposedPlaceOfMarriage"/></value>
                </field>
                <field name="MunicipalityWhereIssued">
                    <value><xsl:value-of select="dcs:MunicipalityWhereIssued"/></value>
                </field>
                <field name="ProposedDateOfMarriage">
                    <value><xsl:value-of select="dcs:ProposedDateOfMarriage"/></value>
                </field>
                <xsl:apply-templates select="dcs:MarriageLicence[@i:type='Applicant'][id=preceding-sibling::Applicant]"/>
                <xsl:apply-templates select="dcs:MarriageLicence[@i:type='Applicant'][id=preceding-sibling::JointApplicant]"/>
            </fields>
        </xfdf>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="dcs:MarriageLicence[@i:type='Issuer']">
        <field name="Issuer">
            <value>
                <xsl:value-of select="dcs:IssuerName"/>
            </value>
        </field>
    </xsl:template>

    <xsl:template match="dcs:MarriageLicence[@i:type='Applicant'][id=preceding-sibling::Applicant]">
        <field name="LastName">
            <value>
                <xsl:value-of select="dcs:LastName"/>
            </value>
        </field>
        <field name="FirstAndMiddleName">
            <value>
                <xsl:value-of select="dcs:FirstAndMiddleName"/>
            </value>
        </field>
        <field name="MaritalStatus1">
            <value>
                <xsl:if test="dcs:MaritalStatus = 1">Yes</xsl:if>
            </value>
        </field>
        <field name="MaritalStatus2">
            <value>
                <xsl:if test="dcs:MaritalStatus = 2">Yes</xsl:if>
            </value>
        </field>
        <field name="MaritalStatus3">
            <value>
                <xsl:if test="dcs:MaritalStatus = 3">Yes</xsl:if>
            </value>
        </field>
        <field name="CourtFileNumber">
            <value>
                <xsl:value-of select="dcs:CourtFileNumber"/>
            </value>
        </field>
        <field name="CityDivorceGranted">
            <value>
                <xsl:value-of select="dcs:CityDivorceGranted"/>
            </value>
        </field>
        <field name="ReligiousDenomination">
            <value>
                <xsl:value-of select="dcs:ReligiousDenomination"/>
            </value>
        </field>
        <field name="Age">
            <value>
                TBD
            </value>
        </field>
        <field name="DateOfBirth">
            <value>
                <xsl:value-of select="dcs:DateOfBirth"/>
            </value>
        </field>
        <field name="BirthPlace">
            <value>
                <xsl:value-of select="dcs:BirthPlace"/>
            </value>
        </field>
        <field name="FathersName">
            <value>
                <xsl:value-of select="dcs:FathersLastName"/>, <xsl:value-of select="dcs:FathersFirstName"/>
            </value>
        </field>
        <field name="MothersName">
            <value>
                <xsl:value-of select="dcs:MothersMaidenName"/>, <xsl:value-of select="dcs:MothersFirstName"/>
            </value>
        </field>
        <field name="FathersBirthPlace">
            <value>
                <xsl:value-of select="dcs:FathersBirthPlace"/>
            </value>
        </field>
        <field name="MothersBirthPlace">
            <value>
                <xsl:value-of select="dcs:MothersBirthPlace"/>
            </value>
        </field>
        <field name="PresentAddress">
            <value>
                <xsl:value-of select="dcs:StreetNumber"/>nbsp
                <xsl:value-of select="dcs:StreetName"/>
            </value>
        </field>
        <field name="Appartment">
            <value>
                <xsl:value-of select="dcs:Appartment"/>
            </value>
        </field>
        <field name="Province">
            <value>
                <xsl:value-of select="dcs:Province"/>
            </value>
        </field>
        <field name="PostalCode">
            <value>
                <xsl:value-of select="dcs:PostalCode"/>
            </value>
        </field>
        <field name="TelephoneNumber">
            <value>
                <xsl:value-of select="dcs:TelephoneNumber"/>
            </value>
        </field>
        <field name="FullameFirstLast">
            <value>
                <xsl:value-of select="dcs:FirstAndMiddleName"/>nbsp
                <xsl:value-of select="dcs:LastName"/>
            </value>
        </field>
        <field name="SwordAt">
            <value>
                TBD
            </value>
        </field>
        <field name="InThe">
            <value>
                TBD
            </value>
        </field>
        <field name="Of">
            <value>
                TBD
            </value>
        </field>
        <field name="MyOrOur">
            <value>
                TBD
            </value>
        </field>
    </xsl:template>

    <xsl:template match="dcs:MarriageLicence[@i:type='Applicant'][ id = preceding-sibling::JointApplicant]">
        <field name="JLastName">
            <value>
                <xsl:value-of select="dcs:LastName"/>
            </value>
        </field>
        <field name="JFirstAndMiddleName">
            <value>
                <xsl:value-of select="dcs:FirstAndMiddleName"/>
            </value>
        </field>
        <field name="JMaritalStatus1">
            <value>
                <xsl:if test="dcs:MaritalStatus = 1">Yes</xsl:if>
            </value>
        </field>
        <field name="JMaritalStatus2">
            <value>
                <xsl:if test="dcs:MaritalStatus = 2">Yes</xsl:if>
            </value>
        </field>
        <field name="JMaritalStatus3">
            <value>
                <xsl:if test="dcs:MaritalStatus = 3">Yes</xsl:if>
            </value>
        </field>
        <field name="JCourtFileNumber">
            <value>
                <xsl:value-of select="dcs:CourtFileNumber"/>
            </value>
        </field>
        <field name="JCityDivorceGranted">
            <value>
                <xsl:value-of select="dcs:CityDivorceGranted"/>
            </value>
        </field>
        <field name="JReligiousDenomination">
            <value>
                <xsl:value-of select="dcs:ReligiousDenomination"/>
            </value>
        </field>
        <field name="JAge">
            <value>
                TBD
            </value>
        </field>
        <field name="JDateOfBirth">
            <value>
                <xsl:value-of select="dcs:DateOfBirth"/>
            </value>
        </field>
        <field name="JBirthPlace">
            <value>
                <xsl:value-of select="dcs:BirthPlace"/>
            </value>
        </field>
        <field name="JFathersName">
            <value>
                <xsl:value-of select="dcs:FathersLastName"/>, <xsl:value-of select="dcs:FathersFirstName"/>
            </value>
        </field>
        <field name="JMothersName">
            <value>
                <xsl:value-of select="dcs:MothersMaidenName"/>, <xsl:value-of select="dcs:MothersFirstName"/>
            </value>
        </field>
        <field name="JFathersBirthPlace">
            <value>
                <xsl:value-of select="dcs:FathersBirthPlace"/>
            </value>
        </field>
        <field name="JMothersBirthPlace">
            <value>
                <xsl:value-of select="dcs:MothersBirthPlace"/>
            </value>
        </field>
        <field name="JPresentAddress">
            <value>
                <xsl:value-of select="dcs:StreetNumber"/>nbsp
                <xsl:value-of select="dcs:StreetName"/>
            </value>
        </field>
        <field name="JAppartment">
            <value>
                <xsl:value-of select="dcs:Appartment"/>
            </value>
        </field>
        <field name="JProvince">
            <value>
                <xsl:value-of select="dcs:Province"/>
            </value>
        </field>
        <field name="JPostalCode">
            <value>
                <xsl:value-of select="dcs:PostalCode"/>
            </value>
        </field>
        <field name="JTelephoneNumber">
            <value>
                <xsl:value-of select="dcs:TelephoneNumber"/>
            </value>
        </field>
        <field name="JFullameFirstLast">
            <value>
                <xsl:value-of select="dcs:FirstAndMiddleName"/>nbsp
                <xsl:value-of select="dcs:LastName"/>
            </value>
        </field>
    </xsl:template>

</xsl:stylesheet>
Thanks,
Evan
 
Old January 23rd, 2009, 12:42 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your template rule for marriageLicense does <xsl:apply-templates select="marriageLicense"/>; but marriageLicense does not have a child called marriageLicense. You may want to apply-templates select="." in a different mode.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
relating to data inserting? asfakahmed Java Databases 1 June 12th, 2008 07:35 AM
Problem relating asp.net 2.0 rakeshbindal4 ASP.NET 2.0 Professional 0 December 28th, 2006 07:00 AM
Problem relating to ajax rakeshbindal4 ASP.NET 2.0 Professional 3 December 27th, 2006 09:56 AM
Problem relating to Microsoft Visual InterDev 6 mudit VS.NET 2002/2003 3 May 3rd, 2005 10:20 PM
Relating 2 Tables be search for single field value bsimecek SQL Language 4 October 4th, 2004 09:24 AM





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