Home > Blockchain >  XSLT document(): can't use xpath in the document
XSLT document(): can't use xpath in the document

Time:07-19

I use document() to fetch metadata from an xml file to complete an ALTO file, here are my alto file (without the layout):

<?xml version="1.0" encoding="UTF-8"?>
<alto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.loc.gov/standards/alto/ns-v4#"
      xsi:schemaLocation="http://www.loc.gov/standards/alto/ns-v4# http://www.loc.gov/standards/alto/v4/alto-4-2.xsd">
  <Description>
    <MeasurementUnit>pixel</MeasurementUnit>
    <sourceImageInformation>
      <fileName>B_168_15-10-3_0001.jpg</fileName>
    </sourceImageInformation>
  </Description>
</alto>

and the xml file who contains the metadata:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <row>
        <Heading0>img</Heading0>
        <Heading1>Cote</Heading1>
        <Heading2>Institution</Heading2>
        <Heading3>Collection</Heading3>
        <Heading4>Document</Heading4>
    </row>
    <row>
        <Heading0>A_bcd_ef-g-h_0001.jpg</Heading0>
        <Heading1>A_bcd_ef-g-h_0001 </Heading1>
        <Heading2>A bcd/ef-g.h</Heading2>
        <Heading3>coll1</Heading3>
    </row>
    <row>
        <Heading0>A_bcd_ef-g-h_0002.jpg</Heading0>
        <Heading1>A_bcd_ef-g-h_0002 </Heading1>
        <Heading2>A bcd/ef-g.h</Heading2>
        <Heading3>coll1</Heading3>
    </row>

I need to add a <documentIdentifier> in my alto file who should have the same value as the <Heading2>, so I try to use the document() function, but it only return <documentIdentifier xmlns=""/>. It seems that I can't select elements in the path of the file.

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:alto="http://www.loc.gov/standards/alto/ns-v4#"
    xpath-default-namespace="http://www.loc.gov/standards/alto/ns-v4#" version="2.0"
    exclude-result-prefixes="xs alto">
    <xsl:output encoding="UTF-8" method="xml" indent="yes" 
        omit-xml-declaration="yes"/>
   
    <xsl:template match="@*|node()">
        
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="sourceImageInformation">
            <xsl:apply-templates/>
            
            <!-- Creating a variable with a information similar in booth documents *.jpeg -->
            <xsl:variable name="filename">
                <xsl:value-of select="//fileName"/>
            </xsl:variable>
    
            <!-- Creating a variable for the information to copy -->
            <xsl:variable name="cote">
                <xsl:copy-of select= "document('recolement.xml')/root/row[Heading0=$filename]/Heading2"/>    
            </xsl:variable>

            <documentIdentifier>
                <xsl:value-of select="$cote"/>
            </documentIdentifier>
                 
    </xsl:template>    
    
</xsl:stylesheet>

The path is good, because if I call

<documentIdentifier>
    <xsl:value-of select="$cote"/>
</documentIdentifier>

It return the content of my file...

What am I doing wrong? Thank's for the help!

CodePudding user response:

Make <xsl:copy-of select= "document('recolement.xml')/root/row[Heading0=$filename]/Heading2"/> into <xsl:copy-of select= "document('recolement.xml')/root/row[Heading0=$filename]/Heading2" xpath-default-namespace=""/>. In the end it would be more efficient to use a key for the cross-reference: <xsl:key name="row-by-hdng0" match="row" use="Heading0" xpath-default-namespace=""/>, then use e.g. <xsl:copy-of select="key('row-by-hdng0', $filename, document('recolement.xml'))/Heading2" xpath-default-namespace=""/>.

CodePudding user response:

I suspect your problem is the effect of the xpath-default-namespace attribute on the stylesheet element. https://www.w3.org/TR/xslt20/#unprefixed-qnames

That default namespace will also be in effect when the XPath expression document('recolement.xml')/root/row[Heading0=$filename]/Heading2 is evaluated, although the recolement.xml document does not, of course, use that namespace (or any namespace). You might want to drop the xpath-default-namespace and use an explicit alto: namespace prefix for expressions which need it.

  • Related