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

  • Thread starter Dhurandhar Bhatvadekar
  • Start date
D

Dhurandhar Bhatvadekar

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
 
J

Joe Kesselman

- 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.
 
D

Dhurandhar Bhatvadekar

Joe said:
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.
 
D

Dhurandhar Bhatvadekar

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>
 
J

Joe Kesselman

Dhurandhar said:
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.
 
J

Joe Kesselman

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top