XSLT external lookup table correct use - key () function?

I have searched and found several manuals for the xsl: key and key () function, but somehow I still don't see the understanding.

I need to do an XML-XML transformation that includes about 10 fields in which I have to take string values ​​from the original XML code, find the corresponding numeric codes from the corresponding lookup tables (provided), and put those codes in the XML result.

I have a working version of this xsl: for-each command for a lookup table, but I suspect it is suboptimal and would like to know if I should use select = "key ('CR-Lookup', $ CR)" Instead somehow.

So what I want to do (deep part of the tree):

<Contributor>
<ContributorRole>producer</ContributorRole>
<ContributorName>Anglet, J.</ContributorName>
</Contributor>

      

converts to something like this:

<Contributor>
<ContributorRole id="7" code="818"/>
<Value id="Name">Anglet, J.</Value>
</Contributor>

      

The files I made are for example:

Lookup_ContributorRole.xml lookup table file :

<lookup id="ContributorRole">
<row>
  <id>7</id>
  <parentid>NULL</parentid>
  <valueMember>1</valueMember>
  <displayMember>producer</displayMember>
  <code>818</code>
  <externalId>NULL</externalId>
  <description>NULL</description>
</row>
<!-- more <row>s...-->
</lookup>

      

Modify the XSLT file where I am trying to map:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
    xmlns:foxml="info:fedora/fedora-system:def/foxml#" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:rel="info:fedora/fedora-system:def/relations-external#"
    xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema" 
    xmlns:audit="info:fedora/fedora-system:def/audit#" 
    xmlns:fedoraxsi="http://www.w3.org/2001/XMLSchema-instance"

    exclude-result-prefixes="xsl foxml rdf rel oai_dc dc xsi audit fedoraxsi"
>
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />

<xsl:key name="CR-lookup" match="row" use="displayMember"/>
<xsl:variable name="CRTable" select="document('lookup_ContributorRole.xml')/lookup/row"/>

<xsl:template match="Contributor">
    <Contributor>
    <xsl:variable name="CR"><xsl:value-of select="ContributorRole"/></xsl:variable>
    <ContributorRole>
    <xsl:for-each select="$CRTable">
        <xsl:if test="displayMember=$CR">
            <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute>
            <xsl:attribute name="code"><xsl:value-of select="code"/></xsl:attribute>
        </xsl:if>
    </xsl:for-each>
    </ContributorRole>
    <Value id="Name"><xsl:value-of select="ContributorName"/></Value>
    </Contributor>
</xsl:template>

    <xsl:template match="/">
    <DigitalObject>
        <Core>
            <xsl:for-each select="/foxml:digitalObject/foxml:datastream[@ID='DigitalObjectLL']/foxml:datastreamVersion">
             <xsl:sort select="@CREATED" order="descending"/>
             <xsl:if test="position() = 1">
                <xsl:for-each select="./foxml:xmlContent/lnbdo">
                    <xsl:apply-templates select="Contributor"/>
                </xsl:for-each>
             </xsl:if>
           </xsl:for-each>
        </Core>
    </DigitalObject>
    </xsl:template>
</xsl:stylesheet>

      

0


a source to share


2 answers


Before using the key, you will switch the context document:

<xsl:variable name="CRTable" select="document('lookup_ContributorRole.xml')"/>

<xsl:template match="Contributor">
  <Contributor>
    <xsl:variable name="CR" select="ContributorRole"/>
    <ContributorRole>
      <xsl:for-each select="$CRTable"><!-- change context document -->
        <xsl:for-each select="key('CR-lookup', $CR)">
          <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute>
          <xsl:attribute name="code"><xsl:value-of select="code"/></xsl:attribute>
          ...

      



With XSLT 2.0, you could do

<xsl:for-each select="key('CR-lookup', $CR, $CRTable)">

      

+4


a source


I think you need to know the best way to use the Xslt key function .



-1


a source







All Articles