Re: [xsl] Question about isolating records

Subject: Re: [xsl] Question about isolating records
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 6 Sep 2015 12:44:31 -0000
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>.

Yes, I acknowledge that wasn't a complete solution for you (I mentioned it wasn't tested).

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.

 <xsl:output method="xml"/>
    <xsl:strip-space elements="*"/>

You don't need to strip the space once you address the information you need.


    <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">
        <xsl:choose>
            <xsl:when test=". eq '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'))"/>

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:value-of select="'852'"/>
                    </xsl:for-each>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:template>
----------------
FILE2:
<List>
    <Item>
        <PDF>016678286</PDF>
        <Shelfmark>Crawford 2411.</Shelfmark>
        <Title>General-Anzeiger f|r Philatelie.</Title>
    </Item>
</List>

My approach to make your stylesheet hard-coded would be as follows, which may help you understand a bit more about the matching:


<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 |


--- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus

Current Thread