Skip to content

Instantly share code, notes, and snippets.

@JDziurlaj
Last active February 15, 2019 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JDziurlaj/04f13d8c7df1e33b9e371d5a56fcfc34 to your computer and use it in GitHub Desktop.
Save JDziurlaj/04f13d8c7df1e33b9e371d5a56fcfc34 to your computer and use it in GitHub Desktop.
XFA Sticher and Stylesheet defererencing
<?xml version="1.0"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xdp="http://ns.adobe.com/xdp/" xmlns:t="http://www.xfa.org/schema/xfa-template/3.3/" xmlns:xe="http://www.altova.com/xslt-extensions" extension-element-prefixes="xe">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!--Identity template,
provides default behavior that copies all content into the output -->
<xsl:template match="@* | node()">
<xsl:choose>
<xsl:when test="@usehref">
<xsl:call-template name="flattenRef">
<xsl:with-param name="usehref" select="@usehref"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- flattens references -->
<xsl:template name="flattenRef">
<xsl:param name="usehref"/>
<!-- current document-uri for resolving relative references -->
<xsl:param name="uriContext" tunnel="yes"/>
<!-- we first must classify the useref object to be any of the following forms:
- #ID, an XML IDREF
- URI#ID, a remote XML IDREF
- #som(expression), a SOM
- URI#som(expression), a remote SOM
- URI, an unqualified remote uri
-->
<xsl:choose>
<xsl:when test="contains($usehref,'xfs')">
<!-- we don't support style sheets, they use the proto section which appears to be transparent in template DOM,
which we don't have routines to hadnle -->
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:when>
<!-- matches URI#som(expression) -->
<xsl:when test="matches($usehref, '\w+[^\s]#som\(\w+[^\s]+\)')">
<xsl:call-template name="flattenRef2">
<xsl:with-param name="uriPath" select="replace(tokenize($usehref, '#som')[1],'\\','/')"/>
<xsl:with-param name="somExpression" select="tokenize($usehref, '#som\(')[2]"/>
<xsl:with-param name="uriContext" select="$uriContext"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="matches($usehref, '#som\(\w+[^\s]+\)')">
<xsl:call-template name="flattenRef2">
<xsl:with-param name="uriPath" select="''"/>
<xsl:with-param name="somExpression" select="tokenize($usehref, '#som\(')[2]"/>
<xsl:with-param name="uriContext" select="$uriContext"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>Unrecognized href <xsl:value-of select="$usehref"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- needs a better name -->
<xsl:template name="flattenRef2">
<xsl:param name="somExpression"/>
<xsl:param name="uriContext"/>
<xsl:param name="uriPath"/>
<!-- need to run in same directory as source of transform -->
<xsl:variable name="xpathExpression">
<xsl:call-template name="som2xpath">
<xsl:with-param name="somExpression" select="$somExpression"/>
</xsl:call-template>
</xsl:variable>
<!-- now we have xpath expression, so we can pull in the document -->
<xsl:variable name="fqUri">
<!-- resolve-uri is tempremental, so we can only call it when we need to -->
<xsl:choose>
<xsl:when test="$uriContext">
<xsl:value-of select="resolve-uri($uriPath,$uriContext)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$uriPath"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Pull in document -->
<xsl:variable name="refDocument" select="document($fqUri)"/>
<!-- Run XPath on document -->
<xsl:message select="xpathExpression/string()"/>
<xsl:variable name="refDocumentFragment">
<xsl:evaluate context-item="$refDocument" xpath="$xpathExpression/string()"/>
</xsl:variable>
<!-- finally recursively run templates on child document -->
<xsl:apply-templates select="$refDocumentFragment">
<!-- used for resolving relative uris -->
<xsl:with-param name="uriContext" tunnel="yes" select="$refDocument/base-uri()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template name="som2xpath">
<xsl:param name="somExpression"/>
<xsl:variable name="somExpressionParts" select="tokenize(substring-before($somExpression, ')'), '\.')"/>
<xsl:variable name="xpathExpr">
<xsl:for-each select="$somExpressionParts">
<xsl:choose>
<xsl:when test=". = '$template'">/xdp:xdp/t:template</xsl:when>
<xsl:when test="starts-with(., '#')">/(t:<xsl:value-of select="substring-after(., '#')"/>|t:proto/t:<xsl:value-of select="substring-after(., '#')"/>)
</xsl:when>
<xsl:otherwise>/(node()[@name='<xsl:value-of select="."/>']|t:proto/node()[@name='<xsl:value-of select="."/>'])</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$xpathExpr"/>
<!--|<xsl:value-of select="replace($xpathExpr,'/(\w+[^/\s]+)$','/t:proto/$1')" />-->
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="AdobeLiveCycleDesigner_V11.0.1.20160728.2.925958" APIVersion="3.6.16210.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<template xmlns="http://www.xfa.org/schema/xfa-template/3.6/">
<subform name="StyleSheet" locale="en_US">
<pageSet>
<pageArea name="Page1" id="Page1">
<contentArea x="0.25in" y="0.25in" w="8in" h="10.5in"/>
<medium stock="letter" short="8.5in" long="11in"/>
</pageArea>
</pageSet>
<subform w="8in" h="10.5in"/>
<proto>
<subform name="designer__stylesheet">
<draw name="TextStyle1" w="29.2864mm" h="5.2331mm">
<ui>
<textEdit/>
</ui>
<font size="12pt" typeface="Myriad Pro"/>
<margin topInset="0.5mm" bottomInset="0.5mm" leftInset="0.5mm" rightInset="0.5mm"/>
<border>
<edge presence="hidden"/>
</border>
</draw>
</subform>
</proto>
</subform>
<?templateDesigner DefaultCaptionFontSettings face:Myriad Pro;size:10;weight:normal;style:normal?>
<?templateDesigner DefaultValueFontSettings face:Myriad Pro;size:10;weight:normal;style:normal?></template></xdp:xdp>
<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="AdobeLiveCycleDesigner_V11.0.1.20160728.2.925958" APIVersion="3.6.16210.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2019-02-15T19:53:55Z" uuid="0d1b01ae-486d-4b3e-b054-20657a64553e">
<template xmlns="http://www.xfa.org/schema/xfa-template/3.3/">
<?formServer defaultPDFRenderFormat acrobat10.0dynamic?>
<subform name="form1" layout="tb" locale="en_US" restoreState="auto">
<pageSet>
<pageArea name="Page1" id="Page1">
<contentArea x="0.25in" y="0.25in" w="8in" h="10.5in"/>
<medium stock="letter" short="8.5in" long="11in"/>
<?templateDesigner expand 1?></pageArea>
<?templateDesigner expand 1?></pageSet>
<subform w="8in" h="10.5in">
<?templateDesigner expand 1?>
<draw name="Text1" vAlign="top" usehref=".\mydumbstyles.xfs#som($template.#subform.designer__stylesheet.TextStyle1)">
<ui>
<textEdit multiLine="1"/>
</ui>
<value>
<text>This is some text</text>
</value>
<font weight="bold"/>
</draw>
</subform>
<proto/>
<desc>
<text name="version">11.0.1.20160728.2.925958.899636</text>
</desc>
<?templateDesigner expand 1?></subform>
<?templateDesigner DefaultPreviewDynamic 1?>
<?templateDesigner DefaultRunAt client?>
<?templateDesigner Grid show:1, snap:1, units:0, color:ff8080, origin:(0,0), interval:(125000,125000)?>
<?templateDesigner WidowOrphanControl 0?>
<?templateDesigner DefaultCaptionFontSettings face:Myriad Pro;size:10;weight:normal;style:normal?>
<?templateDesigner DefaultValueFontSettings face:Myriad Pro;size:10;weight:normal;style:normal?>
<?templateDesigner DefaultLanguage JavaScript?>
<?acrobat JavaScript strictScoping?>
<?templateDesigner SaveTaggedPDF 1?>
<?templateDesigner SavePDFWithEmbeddedFonts 1?>
<?templateDesigner Stylesheet .\mydumbstyles.xfs?>
<?templateDesigner FormTargetVersion 33?>
<?templateDesigner Zoom 68?>
<?templateDesigner Rulers horizontal:1, vertical:1, guidelines:1, crosshairs:0?></template>
<config xmlns="http://www.xfa.org/schema/xci/3.0/">
<agent name="designer">
<!-- [0..n] -->
<destination>pdf</destination>
<pdf>
<!-- [0..n] -->
<fontInfo/>
</pdf>
</agent>
<present>
<!-- [0..n] -->
<pdf>
<!-- [0..n] -->
<version>1.7</version>
<adobeExtensionLevel>8</adobeExtensionLevel>
</pdf>
<xdp>
<packets>*</packets>
</xdp>
<common/>
<script>
<runScripts>server</runScripts>
</script>
<output/>
</present>
</config>
<localeSet xmlns="http://www.xfa.org/schema/xfa-locale-set/2.7/">
<locale name="en_US" desc="English (United States)">
<calendarSymbols name="gregorian">
<monthNames>
<month>January</month>
<month>February</month>
<month>March</month>
<month>April</month>
<month>May</month>
<month>June</month>
<month>July</month>
<month>August</month>
<month>September</month>
<month>October</month>
<month>November</month>
<month>December</month>
</monthNames>
<monthNames abbr="1">
<month>Jan</month>
<month>Feb</month>
<month>Mar</month>
<month>Apr</month>
<month>May</month>
<month>Jun</month>
<month>Jul</month>
<month>Aug</month>
<month>Sep</month>
<month>Oct</month>
<month>Nov</month>
<month>Dec</month>
</monthNames>
<dayNames>
<day>Sunday</day>
<day>Monday</day>
<day>Tuesday</day>
<day>Wednesday</day>
<day>Thursday</day>
<day>Friday</day>
<day>Saturday</day>
</dayNames>
<dayNames abbr="1">
<day>Sun</day>
<day>Mon</day>
<day>Tue</day>
<day>Wed</day>
<day>Thu</day>
<day>Fri</day>
<day>Sat</day>
</dayNames>
<meridiemNames>
<meridiem>AM</meridiem>
<meridiem>PM</meridiem>
</meridiemNames>
<eraNames>
<era>BC</era>
<era>AD</era>
</eraNames>
</calendarSymbols>
<datePatterns>
<datePattern name="full">EEEE, MMMM D, YYYY</datePattern>
<datePattern name="long">MMMM D, YYYY</datePattern>
<datePattern name="med">MMM D, YYYY</datePattern>
<datePattern name="short">M/D/YY</datePattern>
</datePatterns>
<timePatterns>
<timePattern name="full">h:MM:SS A Z</timePattern>
<timePattern name="long">h:MM:SS A Z</timePattern>
<timePattern name="med">h:MM:SS A</timePattern>
<timePattern name="short">h:MM A</timePattern>
</timePatterns>
<dateTimeSymbols>GyMdkHmsSEDFwWahKzZ</dateTimeSymbols>
<numberPatterns>
<numberPattern name="numeric">z,zz9.zzz</numberPattern>
<numberPattern name="currency">$z,zz9.99|($z,zz9.99)</numberPattern>
<numberPattern name="percent">z,zz9%</numberPattern>
</numberPatterns>
<numberSymbols>
<numberSymbol name="decimal">.</numberSymbol>
<numberSymbol name="grouping">,</numberSymbol>
<numberSymbol name="percent">%</numberSymbol>
<numberSymbol name="minus">-</numberSymbol>
<numberSymbol name="zero">0</numberSymbol>
</numberSymbols>
<currencySymbols>
<currencySymbol name="symbol">$</currencySymbol>
<currencySymbol name="isoname">USD</currencySymbol>
<currencySymbol name="decimal">.</currencySymbol>
</currencySymbols>
<typefaces>
<typeface name="Myriad Pro"/>
<typeface name="Minion Pro"/>
<typeface name="Courier Std"/>
<typeface name="Adobe Pi Std"/>
<typeface name="Adobe Hebrew"/>
<typeface name="Adobe Arabic"/>
<typeface name="Adobe Thai"/>
<typeface name="Kozuka Gothic Pro-VI M"/>
<typeface name="Kozuka Mincho Pro-VI R"/>
<typeface name="Adobe Ming Std L"/>
<typeface name="Adobe Song Std L"/>
<typeface name="Adobe Myungjo Std M"/>
</typefaces>
</locale>
</localeSet>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.2-c001 63.156067, 2014/03/04-12:38:26 ">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/" rdf:about="">
<xmp:MetadataDate>2019-02-15T19:53:55Z</xmp:MetadataDate>
<xmp:CreatorTool>Adobe LiveCycle Designer 11.0</xmp:CreatorTool>
<xmp:ModifyDate>2019-02-15T13:50:46-05:00</xmp:ModifyDate>
<xmp:CreateDate>2019-02-15T13:50:46-05:00</xmp:CreateDate>
</rdf:Description>
<rdf:Description xmlns:pdf="http://ns.adobe.com/pdf/1.3/" rdf:about="">
<pdf:Producer>Adobe LiveCycle Designer 11.0</pdf:Producer>
</rdf:Description>
<rdf:Description xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" rdf:about="">
<xmpMM:DocumentID>uuid:0d1b01ae-486d-4b3e-b054-20657a64553e</xmpMM:DocumentID>
<xmpMM:InstanceID>uuid:f808c2bc-2334-4e23-9f9d-17c2282a67cc</xmpMM:InstanceID>
</rdf:Description>
<rdf:Description xmlns:dc="http://purl.org/dc/elements/1.1/" rdf:about="">
<dc:format>application/pdf</dc:format>
</rdf:Description>
<rdf:Description xmlns:desc="http://ns.adobe.com/xfa/promoted-desc/" rdf:about="">
<desc:version rdf:parseType="Resource">
<rdf:value>11.0.1.20160728.2.925958.899636</rdf:value>
<desc:ref>/template/subform[1]</desc:ref>
</desc:version>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<annots/>
</xfdf></xdp:xdp>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment