Skip to content

Instantly share code, notes, and snippets.

@fbennett
Created May 8, 2013 11:59
Show Gist options
  • Save fbennett/5539991 to your computer and use it in GitHub Desktop.
Save fbennett/5539991 to your computer and use it in GitHub Desktop.
Test of extended CSL condition syntax
>>===== MODE =====>>
citation
<<===== MODE =====<<
This test supports a proposal for extending CSL's conditional attributes.
The proposal introduces two new nodes and associated schema adjustments:
* A cs:conditions element, as an optional first child of cs:if or cs:else-if,
with a sole match attribute. When cs:conditions is used, its parent (cs:if
or cs:else-if) must have no attributes. Only cs:condition elements are permitted
as children of cs:condition, and there must be at least one such child.
* A cs:condition element, with the same attributes as cs:if or cs:else-if.
The proposal also introduces a new value "anynot" for the match attribute.
Under the proposal, conditional evaluation would take place as
follows:
(1) cs:if and cs:else-if elements with attributes process exactly as under the
current schema and specification.
(2) cs:if and cs:else-if element with no attributes apply the conditions
provided by the immediate cs:conditions child and its cs:condition
children. The cs:condition children are evaluated first, according
to the same logic as cs:if and cs:else-if. The results are then
combined according to the match element on cs:conditions, returning
a single boolean result for the parent cs:fi or cs:else-if element.
This change makes it possible to reduce the bulk of CSL code. As one example,
the construct below is found in several styles in the CSL repository:
<macro name="year-date">
<choose>
<if type="webpage">
<choose>
<if variable="issued">
<date variable="issued">
<date-part name="year"/>
</date>
</if>
<else>
<date variable="accessed">
<date-part name="year"/>
</date>
</else>
</choose>
</if>
<else>
<date variable="issued">
<date-part name="year"/>
</date>
</else>
</choose>
</macro>
What the code does is to print the "accessed" date if the item is a
webpage and has no "issued" date, and otherwise to print the "issued"
date, regardless of item type. A nested cs:choose statement is needed,
because negative and positive conditions cannot be declared together
on the same cs:if or cs:else-if element.
With the proposed syntax, the code sample above can be rewritten as a
single cs:choose statement:
<macro name="year-date">
<choose>
<if>
<conditions match="all">
<condition type="webpage" match="all"/>
<condition variable="issued" match="none"/>
</conditions>
<date variable="accessed">
<date-part name="year"/>
</date>
</if>
<else>
<date variable="issued">
<date-part name="year"/>
</date>
</else>
</choose>
</macro>
>>===== RESULT =====>>
Item One is not an ARTICLE-JOURNAL, and has an EDITION
Item Two is a BOOK, but has no EDITION
Item Three is a CHAPTER, and has an AUTHOR
Item Four is an ARTICLE-JOURNAL with both VOLUME and ISSUE, but one of them is non-numeric
Item Five is an ARTICLE-JOURNAL with both VOLUME and ISSUE, and both of them are numeric
<<===== RESULT =====<<
>>===== CSL =====>>
<style
xmlns="http://purl.org/net/xbiblio/csl"
class="note"
version="1.0">
<info>
<id />
<title />
<updated>2009-08-10T04:49:00+09:00</updated>
</info>
<citation>
<layout delimiter="&#x0A;">
<group delimiter=" ">
<text variable="title"/>
<choose>
<if>
<conditions match="all">
<condition type="article-journal" variable="volume issue" match="all"/>
<condition variable="volume issue" match="all"/>
<condition is-numeric="volume issue" match="anynot"/>
</conditions>
<text value="is an ARTICLE-JOURNAL with both VOLUME and ISSUE, but one of them is non-numeric"/>
</if>
<else-if match="all" type="article-journal" variable="volume issue" is-numeric="volume issue">
<text value="is an ARTICLE-JOURNAL with both VOLUME and ISSUE, and both of them are numeric"/>
</else-if>
<else-if>
<conditions match="all">
<condition type="article-journal" match="none"/>
<condition variable="edition" match="all"/>
</conditions>
<text value="is not an ARTICLE-JOURNAL, and has an EDITION"/>
</else-if>
<else-if>
<conditions match="all">
<condition type="book"/>
<condition variable="edition" match="none"/>
</conditions>
<text value="is a BOOK, but has no EDITION"/>
</else-if>
<else-if type="chapter" variable="author" match="all">
<text value="is a CHAPTER, and has an AUTHOR"/>
</else-if>
</choose>
</group>
</layout>
</citation>
</style>
<<===== CSL =====<<
>>===== INPUT =====>>
[
{
"edition": "5",
"id": "ITEM-1",
"title": "Item One",
"type": "book"
},
{
"id": "ITEM-2",
"title": "Item Two",
"type": "book"
},
{
"id": "ITEM-3",
"title": "Item Three",
"type": "chapter",
"author": [
{
"family": "Snoapes",
"given": "John"
}
]
},
{
"id": "ITEM-4",
"title": "Item Four",
"type": "article-journal",
"volume": "Supplement",
"issue": "1"
},
{
"id": "ITEM-5",
"title": "Item Five",
"type": "article-journal",
"volume": "2",
"issue": "4"
}
]
<<===== INPUT =====<<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment