|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Question about isolating records
At 2015-09-06 06:13 +0000, Mark Wilson pubs@xxxxxxxxxxxx wrote:
That is, the key() is finding the right <Item> in FILE2 (single-crawford-docs.xml), but is returning the entire contents of that <Item>.
How can it be limited to <PDF>? Simply by addressing only the PDF child of the Item element. I cannot understand where I tell the stylesheet I am only interested in the contents of <PDF>. Here is the entire stylesheet which I think reproduces your code with the exception that the <Tag> data is hard coded. You don't need to strip the space once you address the information you need. doc('single-crawford-docs.xml'))"/> Change the above to address the PDF child of the Item element returned: <xsl:attribute name="pdf-number"
select="key('pdf-key', ., doc('single-crawford-docs.xml'))/PDF"/>
<xsl:key name="pdf-key" match="Item" use="Shelfmark"/> <xsl:template match="@* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template> <xsl:template match="Tag[.='852']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="@crawford-number">
<xsl:attribute name="pdf-number"
select="key('pdf-key', ., doc('single-crawford-docs.xml'))/PDF"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>In the above, a <Tag> element with any other value will use the identity template, so you can focus the attention of someone maintaining this stylesheet only on what happens when the tag has a value of '852'. Also, by using the <xsl:apply-templates/> to copy the text node, a future version of this stylesheet could match on multiple values such as: <xsl:template match="Tag[.=('852','963','471')]"> ... and nothing else in the template needs to change. And if you want the code to work on all <Tag> elements that have @crawford-number, then just match="Tag" and any <Tag> without the attribute is preserved as is. Or you could change it as follows: <xsl:template match="Tag[@crawford-number]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="pdf-number"
select="key('pdf-key',@crawford-number,
doc('single-crawford-docs.xml'))/PDF"/
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>You have to keep trying to think of XML as nodes, not strings, when writing your XSLT and you will end up with a stylesheet that is more robust and, in my opinion, easier to maintain. I hope this is helpful. . . . . . . . . Ken -- Check our site for free XML, XSLT, XSL-FO and UBL developer resources | Free 5-hour lecture: http://www.CraneSoftwrights.com/links/video.htm | Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ | G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx | Google+ profile: http://plus.google.com/+GKenHolman-Crane/about | Legal business disclaimers: http://www.CraneSoftwrights.com/legal |
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








