document() returning empty string

Here should go questions about transforming XML with XSLT and FOP.
Richard_Wood
Posts: 12

document() returning empty string

Fri Mar 03, 2017 1:58 am

I use the document() function successfully and often in my Schematron work.
I am now trying to use it in an xsl:stylesheet to obtain a value from an external XML file, hosted on a REST web interface.
I have created a simple example to use in this post so that I can hopefully demonstrate all of the components of the process.
The actual use is significantly more complex, but I can't even get it to work in this simple case.
It appears that I am getting an empty string returned.

I associate a variable with the connect string for the REST API of the XML file.

Code: Select all

        <xsl:variable name="url">
            <xsl:value-of select="'http://my_company.com:8180/RegistryService/registry/XML/metaAttributeGroups'"/>
        </xsl:variable>

Then I create a variable to consume the XML using the document() function

Code: Select all

        <xsl:variable name="xmlDocument">
            <xsl:value-of select="document($url)"/>
        </xsl:variable>

Then I try to apply XPATH to the XML containing variable. This is what I do successfully in Schematron.

Code: Select all

        <xsl:variable name="firstProperty-1">
            <xsl:value-of select="$xmlDocument//entry[1]/item[1]/@value"/>
        </xsl:variable>

I read that the document() function tries to use a baseUrl. I suspect that has something to do with it but I don't know how to deal with that.
In an effort, I have tried one other format of the document() function which you will also see.

Code: Select all

        <xsl:variable name="firstProperty-2">
            <xsl:value-of select="document(//entry[1]/item[1]/@value,$url)"/>
        </xsl:variable>


I am including a clip of the XML file from which I am trying to obtain a value:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<registry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://my_company.com/assets/registry/schema/registry.xsd"
schemaVersion="1.1" identity="metaAttributeGroups"
modified="2017-02-24T20:22:00Z">
  <entry status="stable" modified="2015-07-07T17:30:00Z" >
    <item key="rc" value="Y" />
    <item key="display" value="Preliminary" />
    <item key="property" value="cat1004:documentStatus" />
    <item key="description" value="Delivery Documents - Document Status" />
    <item key="label" value="Document Status (category-specific)" />
    <item key="supersededBy" value="" />
    <item key="content" value="Preliminary" />
  </entry>

The XPATH as applied in OxygenXML Editor:

Code: Select all

//entry[1]/item[1]/@value


And the XML formatted output returned in OxygenXML Editor, indicating that "Y" is what is returned from the XPATH:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.oxygenxml.com/ns/report">
    <incident>
   
        <description>Y</description>

        <xpath_location>/registry[1]/entry[1]/item[1]/@value</xpath_location>

        <systemID>http://my_company.com:8180/RegistryService/registry/XML/metaAttributeGroups</systemID>
        <location>
            <start>
                <line>6</line>
                <column>20</column>
            </start>
            <end>
                <line>6</line>
                <column>29</column>
            </end>
        </location>
    </incident>
</report>



I had to obfuscate a few things (company name for example) for privacy issues.

Here is the XML document, upon which I perform the XSL Transformation:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
   <status>test</status>
   <packageTimestamp>2016-05-05T20:45:00Z</packageTimestamp>
   <issued>2016-01-01T00:00:00Z</issued>
   <availableDate>2016-01-01T00:00:00Z</availableDate>
   <title>My Test Document</title>
</Document>



Here is the XSL Stylesheet that is used for the Transformation.
I output the URL for the external XML file, then two attempts to obtain an attribute from within the XML:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="xs File" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:File="java:java.io.File">
    <xsl:output encoding="UTF-8" indent="yes" method="html" omit-xml-declaration="yes" xml:space="preserve"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="/Document">
        <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html&gt;</xsl:text>
        <html>
            <head>
                <meta charset="utf-8"/>
                <style type="text/css">
                    body{
                        font-family: 'Arial Unicode MS', Arial, Helvetica, sans-serif;
                        color: black;
                        background: white;
                        font-size: 75%;
                        line-height: 1.2;
                        max-width: 75em;
                    }
                </style>
            </head>
            <body>
                    <div id="head">
                        <h1>
                            <xsl:apply-templates select="title"/>
                        </h1>
                    </div>
            </body>
        </html>
    </xsl:template>



    <xsl:template match="title">
        <xsl:variable name="url">
            <xsl:value-of select="'http://my_company.com:8180/RegistryService/registry/XML/metaAttributeGroups'"/>
        </xsl:variable>
        <xsl:variable name="xmlDocument">
            <xsl:value-of select="document($url)"/>
        </xsl:variable>
        <xsl:variable name="firstProperty-1">
            <xsl:value-of select="$xmlDocument//entry[1]/item[1]/@value"/>
        </xsl:variable>
        <xsl:variable name="firstProperty-2">
            <xsl:value-of select="document(//entry[1]/item[1]/@value,$url)"/>
        </xsl:variable>
        <p>
            Here is the url value:
            <xsl:value-of select="$url"/>
            : that was the url value.
        </p>
        <p>
            Here is the first property value:
            <xsl:value-of select="$firstProperty-1"/>
            : that was the first property value.
        </p>
        <p>
            Here is the first property value:
            <xsl:value-of select="$firstProperty-2"/>
            : that was the first property value.
        </p>
    </xsl:template>


</xsl:stylesheet>



Here is the resulting HTML
As you can see, the URL displays fine, but neither of the attempts to display the attribute result in the expected display of: Y

Code: Select all

<!DOCTYPE html><html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <meta charset="utf-8"><style type="text/css">
                    body{
                        font-family: 'Arial Unicode MS', Arial, Helvetica, sans-serif;
                        color: black;
                        background: white;
                        font-size: 75%;
                        line-height: 1.2;
                        max-width: 75em;
                    }
                </style></head>
   <body>
      <div id="head">
         <h1>
            <p>
               Here is the url value:
               http://my_company.com:8180/RegistryService/registry/XML/metaAttributeGroups
               : that was the url value.
               
            </p>
            <p>
               Here is the first property value:
               
               : that was the first property value.
               
            </p>
            <p>
               Here is the first property value:
               
               : that was the first property value.
               
            </p>
         </h1>
      </div>
   </body>
</html>
adrian
Posts: 2326

Re: document() returning empty string

Fri Mar 03, 2017 3:21 pm

Hello,

Have you tried using?

Code: Select all

<xsl:variable name="xmlDocument" select="document($url)"/>


To clarify, xsl:value-of will return a single text node:
The xsl:value-of instruction is evaluated to construct a new text node; the result of the instruction is the newly constructed text node.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Richard_Wood
Posts: 12

Re: document() returning empty string

Fri Mar 03, 2017 7:25 pm

Adrian,

Thank-you for the reply and assistance :D
Not sure how I got that messed up now that I look back at my Schematron files with your tip in mind.

Code: Select all

<sch:let name="registryXmlFilename" value="document($path)"/>


Clearly I was doing a different and wrong thing in my stylesheet:

Code: Select all

        <xsl:variable name="xmlDocument">
            <xsl:value-of select="document($url)"/>
        </xsl:variable>


I guess I got wrapped up in the structure of setting a variable and was unable to see the difference.

Thanks very much, it corrected my issue.

Best Regards,

Rich Wood

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests