Вопрос

(all code here is simplified from it's original version)

My company has a function used in XSLT (1.0) which returns the content from a file from our file system. I need to be able to parse the return of the function using apply-templates. Consider the following example:

Main XML File:

<exhibit>
    <exhibitTitle>Exhibit</exhibitTitle>
    <linkedAsset href="path/to/file.xml" />
</exhibit>

External XML File:

<externalAsset editable="true" id="U10250926378W6C">
    <img src="path/to/image.png" />
    <caption>Some default image</caption>
<externalAsset>

I tried applying the following XSLT to the main XML file:

XSLT:

<xsl:template match="linkedAsset">
    <xsl:apply-templates select="cus:getFileByUri(./@href)" />
</xsl:template>

<xsl:template match="img">
    <xsl:text>|-- Begin Image Source --|</xsl:text>
    <xsl:value-of select="./src" />
    </xsl:text>|-- End Image Source --|</xsl:text>
</xsl:template>

The result was simply "Some default image".

To make sure I was getting an XML structure, and not just the value of all the nodes (or something) I tried:

<xsl:template match="linkedAsset">
    <xsl:copy-of select="cus:getFileByUri(./@href)" />
</xsl:template>

Which returned the original external XML file structure:

<externalAsset editable="true" id="U10250926378W6C">
    <img src="path/to/image.png" />
    <caption>Some default image</caption>
<externalAsset>

I also tried:

<xsl:template match="linkedAsset">
    <xsl:value-of select="cus:getFileByUri(./@href)//img/@src" />
</xsl:template>

Which returned "path/to/image.png" as expected.

Finally, based on answers from this question, I tried the following XSLT:

<xsl:template match="linkedAsset">
    <xsl:call-template name="renderExternal">
        <xsl:with-param name="asset" select="cus:getFileByUri(./@href)" />
    </xsl:call-template>
</xsl:template>

<xsl:template name="renderExternal">
    <xsl:param name="asset" select="." />
    <xsl:apply-templates select="$asset" />
</xsl:template>

The output was identical to the original apply-template.

Is there any way to apply apply-templates to a value returned from a function? I can clearly send the string to copy-of, value-of, and even perform xpaths on it; can I simply not use it with apply-templates?


EXPLANATION OF CHOSEN ANSWER

As it turns out, the solution to my problem was pretty specific (I was applying a template to a node that matched that same template, and that wouldn't be clear from the simplified versions of the code I provided). I really earned my -1 on this one. ANYWAY, I feel that keshlam's answer will be the most helpful to people visiting this question in the future, as it answers what I THOUGHT my problem was.

Это было полезно?

Решение

To execute apply-templates against it, your extension has to return the XML to the stylesheet in already-parsed form (typically a DOM tree or NodeIterator) In XSLT 1.0, you may then have to apply the exslt:node-set() extension function, or your processor's equivalent of that, to handle the impedence mismatch between Temporary Trees and Node Sets (xsl:apply-templates operates on nodesets).

Details of writing and registering extensions may vary from processor to processor. If you're using Xalan-J, for example, see https://xml.apache.org/xalan-j/extensions.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top