简体   繁体   中英

Counting with variable/unknown values XSLT

I am very new to XSLT and I am trying to create a CSV of the number of publications each professor has had each year, Each entry is a different publication denoted by dm:INTELLCONT and each year is denoted by dm:DTY_PUB I am having trouble figuring out how to do this properly if I do not hard code the year, I want to do this in a loop for every year they've published something. My Current XML file is:

 <?xml version="1.0" encoding="UTF-8"?> <Data xmlns="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" dmd:date="2017-10-16"> <INTELLCONT id="151370213376" dmd:originalSource="MANAGE_DATA" dmd:lastModified="2017-10-03T11:41:47" dmd:startDate="2016-04-15" dmd:endDate="2016-04-15"> <REFEREED>Yes</REFEREED> <CONTYPE>Abstract</CONTYPE> <CONTYPEOTHER/> <STATUS>Published</STATUS> <TITLE>Sample Title</TITLE> <TITLE_SECONDARY/> <DTM_PUB>April (2nd Quarter/Spring)</DTM_PUB> <DTD_PUB>15</DTD_PUB> <DTY_PUB>2016</DTY_PUB> <PUB_START>2016-04-15</PUB_START> <PUB_END>2016-04-15</PUB_END> <USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR> </INTELLCONT> <INTELLCONT id="151368284160" dmd:originalSource="MANAGE_DATA" dmd:lastModified="2017-10-03T10:44:48" dmd:startDate="2017-01-01" dmd:endDate="2017-12-31"> <REFEREED>Yes</REFEREED> <CONTYPE>Journal Article</CONTYPE> <CONTYPEOTHER/> <STATUS>Published</STATUS> <TITLE>Sample Title</TITLE> <TITLE_SECONDARY/> <INTELLCONT_AUTH id="151368284163"> <FACULTY_NAME/> <FNAME>FN</FNAME> <MNAME/> <LNAME>LN</LNAME> <INSTITUTION/> <ROLE>Author</ROLE> <STUDENT_LEVEL/> </INTELLCONT_AUTH> <INTELLCONT_AUTH id="151368284161"> <FACULTY_NAME>1898739</FACULTY_NAME> <FNAME>FN</FNAME> <MNAME>MN</MNAME> <LNAME>LN</LNAME> <INSTITUTION/> <ROLE>Author</ROLE> <STUDENT_LEVEL/> </INTELLCONT_AUTH> <PUBLISHER>Public Health</PUBLISHER> <PUBCTYST/> <PUBCNTRY/> <VOLUME>14</VOLUME> <ISSUE>3</ISSUE> <PAGENUM>265</PAGENUM> <DTY_PUB>2017</DTY_PUB> <PUB_START>2017-01-01</PUB_START> <PUB_END>2017-12-31</PUB_END> <USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR> </INTELLCONT> <INTELLCONT id="151368284160" dmd:originalSource="MANAGE_DATA" dmd:lastModified="2017-10-03T10:44:48" dmd:startDate="2017-01-01" dmd:endDate="2017-12-31"> <REFEREED>Yes</REFEREED> <CONTYPE>Journal Article</CONTYPE> <CONTYPEOTHER/> <STATUS>Published</STATUS> <TITLE>Sample Title</TITLE> <TITLE_SECONDARY/> <INTELLCONT_AUTH id="151368284163"> <FACULTY_NAME/> <FNAME>FN</FNAME> <MNAME/> <LNAME>LN</LNAME> <INSTITUTION/> <ROLE>Author</ROLE> <STUDENT_LEVEL/> </INTELLCONT_AUTH> <PUBLISHER>Public Health</PUBLISHER> <PUBCTYST/> <PUBCNTRY/> <VOLUME>14</VOLUME> <ISSUE>3</ISSUE> <PAGENUM>265</PAGENUM> <DTY_PUB>2017</DTY_PUB> <PUB_START>2017-01-01</PUB_START> <PUB_END>2017-12-31</PUB_END> <USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR> </INTELLCONT> </Data> 

My current XSL File is:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:csv="csv:csv" xmlns="http://www.w3.org/1999/xhtml" xmlns:dm="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="array fn map math xhtml xs"> <xsl:output method="text" encoding="utf-8"/> <xsl:variable name="delimiter" select="','"/> <!-- xmlns:dm is the xmlns attribute in Data.--> <!-- define an array containing the fields we are interested in --> <xsl:variable name="fieldArray"> <field>Journal Articles</field> <field>Books</field> <field>Book Chapters</field> <field>Conference Proceeedings</field> <field>Others</field> </xsl:variable> <xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*" /> <xsl:key name="Year-Published" match="dm:INTELLCONT" use="dm:DTY_PUB"/> <xsl:template match="/dm:Data"> <!--key("Year-Published", '2017')--> <!--<xsl:text>Journal Articles</xsl:text> <xsl:value-of select="$delimiter" /> <xsl:text>Books</xsl:text> <xsl:value-of select="$delimiter" /> <xsl:text>Book Chapters</xsl:text> <xsl:value-of select="$delimiter" /> <xsl:text>Conference Proceedings</xsl:text> <xsl:value-of select="$delimiter" /> <xsl:text>Others</xsl:text> --> <xsl:text>Year,</xsl:text> <!-- tabulating Years as columns --> <xsl:for-each select="dm:Record/dm:INTELLCONT[generate-id()=generate-id(key('Year-Published', dm:DTY_PUB)[1])]"> <xsl:sort select="(dm:DTY_PUB)" order="ascending"/> <xsl:value-of select="(dm:DTY_PUB)"/> <xsl:text>,</xsl:text> </xsl:for-each> <xsl:text>&#xa;</xsl:text> <!-- Tabulating Published Papers Per Year--> <xsl:for-each select="dm:Record/dm:INTELLCONT[generate-id()=generate-id(key('Year-Published', dm:DTY_PUB)[1])]"> <xsl:sort select="(dm:DTY_PUB)" order="ascending"/> <xsl:value-of select="count(dm:Record/dm:INTELLCONT[dm:CONTYPE='Journal Article'][dm:STATUS='Published'][dm:DTY_PUB=key('Year-Published', dm:DTY_PUB)[1])][dm:USER_REFERENCE_CREATOR!='No'])"/> <xsl:value-of select="$delimiter" /> </xsl:for-each> <xsl:text>&#xa;</xsl:text> <!-- output newline --> <xsl:text>&#xa;</xsl:text> </xsl:template> </xsl:stylesheet> 

As you can see the part where I am trying to count each publication per year has the comment Tabulating Published Papers Per Year

I want to loop through every year they published and count how many publications exist that year, any help would be very very much appreciated, thanks in advance!

You seem to have mastered the basic idea of XSLT 1.0 Muenchian Grouping, which is the hard part, and then you've slipped up on a simple issue of context. Here:

<xsl:for-each select="dm:Record/dm:INTELLCONT[...]">
    <xsl:sort select="(dm:DTY_PUB)" order="ascending"/> 
    <xsl:value-of select="count(dm:Record/dm:INTELLCONT[..]/>
</xsl:for-each>

that can't make sense because within the for-each, the context item is an INTELLICONT element, and you're trying to select downwards to a dn:RECORD.

In Muenchian grouping where you want to count the size of the group, the logical structure is

<xsl:for-each select="one item with each key value">
  <xsl:value-of select="count(all items with the same key value)"/>
</xsl:for-each>

so I would expect to see count(key(....)) .

Are you sure you can't use XSLT 2.0 and xsl:for-each-group for this? It makes life so much easier...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM