${...} values in attributes of an imported XML in XSL ...

Discussion in 'XML' started by Dhurandhar Bhatvadekar, Nov 8, 2006.

  1. I am a bit puzzled and don't know where to look for.
    Here is a stylesheet:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE stylesheet [
    <!ENTITY foo SYSTEM "foo.xml">
    ]>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml"
    indent="yes">
    </xsl:eek:utput>

    <xsl:template match="/">
    <xsl:apply-templates select="node()|@*"/>
    </xsl:template>

    <xsl:template match="node()|@*">
    <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="/foohere">
    &foo;
    </xsl:template>
    </xsl:stylesheet>


    Now, everything happens appropriately. The first two template rules do
    the identity transformation,
    then when "/foohere" is found, the foo.xml is added to the result tree
    having resolved the entity. All is well.

    But in the result tree, I see that there are some weird things that
    happen:
    - for the portion that comes from foo.xml all occurrences of "${xyz}"
    are replaced by "$".

    How can I make it take the foo.xml "verbatim"? I have already tried
    <xsl:text disable-output-escaping="yes"> &foo; </xsl:text>, but it does
    not work.

    Thanks for help.

    DB
     
    Dhurandhar Bhatvadekar, Nov 8, 2006
    #1
    1. Advertising

  2. > - for the portion that comes from foo.xml all occurrences of "${xyz}"
    > are replaced by "$".


    You're inserting the contents of foo.xml into the stylesheet. That means
    its contents are going to be interpreted as XSLT. Depending on where
    these are occurring, they may be interpreted as Attribute Value Templates.

    You may want to copy this using the document() function instead.


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Nov 8, 2006
    #2
    1. Advertising

  3. Joe Kesselman wrote:
    > > - for the portion that comes from foo.xml all occurrences of "${xyz}"
    > > are replaced by "$".

    >
    > You're inserting the contents of foo.xml into the stylesheet. That means
    > its contents are going to be interpreted as XSLT. Depending on where
    > these are occurring, they may be interpreted as Attribute Value Templates.
    >
    > You may want to copy this using the document() function instead.


    That's a great advice, thank you. I did not realize that the entity
    resolution occurs *for the XSLT* and hence in effect, the XSLT is
    modified. Let me take a look at the document() function and report
    back.

    Thank you, again.
    >
    >
    > --
    > () ASCII Ribbon Campaign | Joe Kesselman
    > /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Dhurandhar Bhatvadekar, Nov 8, 2006
    #3
  4. Yes, document() function works, thank you.

    Can you also tell me how to write out (template rule such that) an xml
    element as-is, except that all the attributes should be sorted
    lexically?

    For the benefit of others:

    Here is the working version that deals with use of document:

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="foo" select="'foo.html'"/>
    <xsl:eek:utput method="xml"
    indent="yes">
    </xsl:eek:utput>

    <xsl:template match="/">
    <xsl:apply-templates select="node()|@*"/>
    </xsl:template>

    <xsl:template match="node()|@*">
    <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="/foohere">
    <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    <xsl:copy-of select="document($foo)//"/>
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

    Dhurandhar Bhatvadekar wrote:
    > Joe Kesselman wrote:
    > > > - for the portion that comes from foo.xml all occurrences of "${xyz}"
    > > > are replaced by "$".

    > >
    > > You're inserting the contents of foo.xml into the stylesheet. That means
    > > its contents are going to be interpreted as XSLT. Depending on where
    > > these are occurring, they may be interpreted as Attribute Value Templates.
    > >
    > > You may want to copy this using the document() function instead.

    >
    > That's a great advice, thank you. I did not realize that the entity
    > resolution occurs *for the XSLT* and hence in effect, the XSLT is
    > modified. Let me take a look at the document() function and report
    > back.
    >
    > Thank you, again.
    > >
    > >
    > > --
    > > () ASCII Ribbon Campaign | Joe Kesselman
    > > /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Dhurandhar Bhatvadekar, Nov 8, 2006
    #4
  5. Dhurandhar Bhatvadekar wrote:
    > Can you also tell me how to write out (template rule such that) an xml
    > element as-is, except that all the attributes should be sorted
    > lexically?


    The order of attributes is explicitly NOT MEANINGFUL in XML, and XML
    tools make absolutely no promises that order will be preserved. If order
    is semantically important, you shoud use child elements rather than
    attributes.

    If the order matters for non-semantic reasons (for example, if you're
    trying to do a simple text comparison between two XML files), you can
    try looking for a tool that converts the XML into Canonical Form
    (http://www.w3.org/TR/xml-c14n), and apply that after the stylesheet has
    run. Or you could write your own XML serializer that outputs in
    canonical form. Or you could find a solution that doesn't require
    canonicalization, eg using an XML-aware file comparison tool.




    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Nov 8, 2006
    #5
  6. By the way... If you really, absolutely, insist upon trying to order the
    attributes using XSLT you could probably do it, but you'd have to
    reimplement the XML serializer in XSLT and output in text mode. Very
    emphatically NOT a recommended solution.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Nov 8, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 5tein
    Replies:
    1
    Views:
    473
    Patrick TJ McPhee
    Apr 30, 2004
  2. Replies:
    1
    Views:
    3,612
    A. Bolmarcich
    May 27, 2005
  3. Dun Peal
    Replies:
    10
    Views:
    459
    Chris Rebert
    May 3, 2011
  4. Andrew Berg
    Replies:
    3
    Views:
    103
    Steven D'Aprano
    Oct 2, 2011
  5. Volker Nicolai
    Replies:
    9
    Views:
    964
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page