Project

Profile

Help

Question about attribute node handling in ExtensionInstructions

Added by Anna Benton almost 10 years ago

Hello,

I have a question about how attribute nodes are handled in ExtensionInstructions.

First the set up:

Here's my example stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:http="http://www.highwire.org/saxon/http" extension-element-prefixes="http" exclude-result-prefixes="xs" version="2.0">

<xsl:template match="/"> <xsl:variable name="href" as="xs:anyURI" select="resolve-uri('http://test.highwire.org')"/> <xsl:variable name="headers" as="attribute()"> <xsl:attribute name="Slug" select="'foo'"/> </xsl:variable> <xsl:variable name="entity" as="element()"> </xsl:variable> <http:post href="{$href}" debug="true"> <xsl:sequence select="$headers"/> <xsl:sequence select="$entity"/> </http:post> </xsl:template>

</xsl:stylesheet>

When I get the attribute list in prepareAttributes() "Slug" is not included (only href and debug are). Right now I'm creating an expression in compile() for the contents of http:post and then in call() I'm iterating over the resulting sequence and determining what to do with each item like so:

    while(true) {
        Item item = iterator.next();
        if(item == null) {
            break;
        }
        
        if(item instanceof Orphan) {
            if(((Orphan) item).getNodeKind() == Type.ATTRIBUTE) {
               // Standalone attribute found, treat as header
            } 
        } else {
            // treat as entity, serialize out to string
        }
    }

My question is, is there a better way to handle standalone attributes? Ideally I'd handle them in prepareAttributes instead of processing them in call().

Thanks!


Replies (2)

RE: Question about attribute node handling in ExtensionInstructions - Added by Anna Benton almost 10 years ago

I should have wrapped the template in a pre, doing that here to make it more readable since I can't seem to edit the original post:



  
  
    
    
      
    
    
      
    
    
      
      
    
  
  

And the code snippet I included:

while(true) {
    Item item = iterator.next();
    if(item null) {
        break;
    }

    if(item instanceof Orphan) {
        if(((Orphan) item).getNodeKind()  Type.ATTRIBUTE) {
           // Standalone attribute found, treat as header
        } 
    } else {
        // treat as entity, serialize out to string
    }
}

RE: Question about attribute node handling in ExtensionInstructions - Added by Michael Kay almost 10 years ago

It took me a while to understand the question...

The prepareAttribute() and compile() methods are static compile-time methods that essentially take the source XML representation of the stylesheet as input. In the source representation, the http:post element has two attributes, href and debug, and two child xsl:sequence instructions. It's only at run-time, when you execute these xsl:sequence instructions, that it becomes apparent that the value of $headers, and hence of the first xsl:sequence instruction, is a free-standing attribute node. So you can only process this attribute node in a run-time method, specifically in call().

    (1-2/2)

    Please register to reply