49445

How do I display all the date of birth from a node?

Question:

I have xml document with 3 dates of birth, <strong>current</strong>, <strong>approximate</strong> and <strong>just date</strong>. I want to display the 3 dates as output.

<strong>My xml code</strong>

<Party ID="76" InternalPartyID="18"> <Gender Word="F ">Female</Gender> <ApproximateDOB>03/4/1956</ApproximateDOB> <DateOfBirth Current="true">05/21/1956</DateOfBirth> <DateOfBirth>04/21/1956</DateOfBirth> </Party>

<strong>My XSLT code</strong>

<!--Respondent --> <xsl:for-each select="RespondentPartyID"> <xsl:for-each select="//CaseParty[(@InternalPartyID=current()/@InternalPartyID) and (Connection[(@Word='RSP') ])]"> <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]"> <xsl:call-template name="Respondent"> <xsl:with-param name="pProtectionOrderID"> <xsl:value-of select="$vProtectionOrderID"/> </xsl:with-param> </xsl:call-template> </xsl:for-each> </xsl:for-each> </xsl:for-each> <!--Respondent Template--> <xsl:template name="Respondent"> <xsl:param name="pProtectionOrderID"/> <ext:Respondent> <!--Guardian --> <xsl:for-each select="//CaseParty[(Connection[(@Word='GRD')])][1]"> <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]"> <xsl:call-template name="Guardian"/> </xsl:for-each> </xsl:for-each> <ext:PersonBirthDate> <xsl:choose> <xsl:when test="DateOfBirth[@Current='true']"> <xsl:attribute name="ext:ApproximateDateIndicator">false</xsl:attribute> <xsl:attribute name="ext:CurrentIndicator">true</xsl:attribute> <xsl:value-of select="mscef:formatDate(string(DateOfBirth[@Current='true']))"/> </xsl:when> <xsl:when test="ApproximateDOB"> <xsl:attribute name="ext:ApproximateDateIndicator">true</xsl:attribute> <xsl:attribute name="ext:CurrentIndicator">true</xsl:attribute> <xsl:value-of select="mscef:formatDate(string(ApproximateDOB))"/> </xsl:when> </xsl:choose> </ext:PersonBirthDate> </ext:Respondent>

<strong>How do I change my xslt code so that the output looks like the one below. Right now I my xslt returns only current date of birth:</strong>

<ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="true">1956-05-21</ext:PersonBirthDate> <ext:PersonBirthDate ext:approximateDateIndicator="true" ext:currentIndicator="false">1956-03-04</ext:PersonBirthDate> <ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="false">1956-04-21</ext:PersonBirthDate>

Answer1:

Consider having a template match for each possible 'Date of Birth' element. Something like this:

<xsl:template match="DateOfBirth"> <ext:PersonBirthDate> <xsl:attribute name="approximateDateIndicator">false</xsl:attribute> <xsl:attribute name="currentIndicator">false</xsl:attribute> <xsl:value-of select="."/> </ext:PersonBirthDate> </xsl:template> <xsl:template match="DateOfBirth[@Current='true']"> <ext:PersonBirthDate> <xsl:attribute name="approximateDateIndicator">false</xsl:attribute> <xsl:attribute name="currentIndicator">true</xsl:attribute> <xsl:value-of select="."/> </ext:PersonBirthDate> </xsl:template> <xsl:template match="ApproximateDOB"> <ext:PersonBirthDate> <xsl:attribute name="approximateDateIndicator">true</xsl:attribute> <xsl:attribute name="currentIndicator">false</xsl:attribute> <xsl:value-of select="."/> </ext:PersonBirthDate> </xsl:template>

Then you can change your existing XSLT code to this, which should then output all three dates.

<xsl:apply-templates select="ApproximateDOB|DateOfBirth" />

It is clearly quite repetitive to have so many templates, but it should be no trouble to combine the templates into one.

Try this XSLT for starters:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="ext"> <xsl:output method="xml" indent="yes" /> <xsl:template match="Party"> <ext:Respondent> <xsl:apply-templates select="ApproximateDOB|DateOfBirth" /> </ext:Respondent> </xsl:template> <xsl:template match="ApproximateDOB|DateOfBirth"> <ext:PersonBirthDate approximateDateIndicator="{local-name() = 'ApproximateDOB'}" currentIndicator="{@Current = 'true'}"> <xsl:value-of select="."/> </ext:PersonBirthDate> </xsl:template> </xsl:stylesheet>

Note the use of <a href="http://www.w3.org/TR/xslt#attribute-value-templates" rel="nofollow">Attribute Value Templates</a> to further simplify the code.

When the above XSLT is applied to the following XML

<Party ID="76" InternalPartyID="18"> <Gender Word="F ">Female</Gender> <ApproximateDOB>03/4/1956</ApproximateDOB> <DateOfBirth Current="true">05/21/1956</DateOfBirth> <DateOfBirth>04/21/1956</DateOfBirth> </Party>

The following is output

<ext:Respondent xmlns:ext="ext"> <ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="false">03/4/1956</ext:PersonBirthDate> <ext:PersonBirthDate approximateDateIndicator="false" currentIndicator="true">05/21/1956</ext:PersonBirthDate> <ext:PersonBirthDate approximateDateIndicator="false" currentIndicator="false">04/21/1956</ext:PersonBirthDate> </ext:Respondent>

(I have left out the date formatting, because I don't have the extension function to test locally)

EDIT: To fit this into your XSLT code, you would do this

<!--Respondent --> <xsl:for-each select="RespondentPartyID"> <xsl:for-each select="//CaseParty[(@InternalPartyID=current()/@InternalPartyID) and (Connection[(@Word='RSP') ])]"> <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]"> <xsl:call-template name="Respondent"> <xsl:with-param name="pProtectionOrderID"> <xsl:value-of select="$vProtectionOrderID"/> </xsl:with-param> </xsl:call-template> </xsl:for-each> </xsl:for-each> </xsl:for-each> <!--Respondent Template--> <xsl:template name="Respondent"> <xsl:param name="pProtectionOrderID"/> <ext:Respondent> <!--Guardian --> <xsl:for-each select="//CaseParty[(Connection[(@Word='GRD')])][1]"> <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]"> <xsl:call-template name="Guardian"/> </xsl:for-each> </xsl:for-each> <xsl:apply-templates select="ApproximateDOB|DateOfBirth" /> </ext:Respondent> </xsl:template> <xsl:template match="ApproximateDOB|DateOfBirth"> <ext:PersonBirthDate approximateDateIndicator="{local-name() = 'ApproximateDOB'}" currentIndicator="{@Current = 'true'}"> <xsl:value-of select="mscef:formatDate(string(.))"/> </ext:PersonBirthDate> </xsl:template>

Recommend

  • rbp not allowed as SIB base?
  • Segfault while calling C function (printf) from Assembly
  • Where in memory are return values stored in memory?
  • What's wrong with this statement on x86-64?
  • Duplicate Element x number of times with XSLT
  • how to display   in Mozilla using XSL.
  • XSLT foreach repeating nodes to flat
  • Passing “get” parameters doesn't work, parameter not visible in the link
  • Tell Git to stop prompting me for conflicts when none really exist?
  • Bigquery event streaming and table creation
  • Differences in dis-assembled C code of GCC and Borland?
  • Django invalid literal for int() with base 10
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Query to find the duplicates between the name and number in table
  • Unable to get column index with table.getColumn method using custom table Model
  • Parse a date string in a specific locale (not timezone!)
  • xtable package: Skipping some rows in the output
  • Declaring variable dynamically in VB.net
  • How to define and use opencv mat of user type
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • C++ Partial template specialization - design simplification
  • Django: Count of Group Elements
  • Handling un-mapped Rest path
  • What is the “return” in scheme?
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Submit form in a displaytag pagination
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • How to make Safari send if-modified-since header?
  • How to get next/previous record number?
  • How to pass list parameters for each object using Spring MVC?
  • How do you join a server to an Active Directory (domain)?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • JaxB to read class hierarchy
  • Busy indicator not showing up in wpf window [duplicate]
  • Observable and ngFor in Angular 2
  • Why do underscore prefixed variables exist?