This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Serializing external XML documents


Hi Pedro,

> 1) I am using "document()" function (maybe abusing of it) to talk
> HTTP with an external application: Tamino DB.
>
> Question: Is there any other XSLT standard way of talking HTTP from
> inside a stylesheet ?

No. I'm interested about what you're doing here, because it
illustrates that xsl:message isn't the only thing that makes XSLT fall
short of the "no side-effects" ideal that's often touted as one of its
advantages. Presumably the _process URL causes information to be
inserted in the database; do you also have a document() function
*reading* from the same database?

> 2) One of the possible calls to Tamino is a "_process" function for
> content updating purposes. The syntax for this function is:
>
> DB-URL?_process=<XML object> (XML object stands for a text
> serialized XML fragment)
>
> Question: What is the most practical way of reading an external
> XML-file with the desired target XML information and serialize it as
> non-escaped XML markup text and insert it into the "document()"
> function to perform the HTTP call ?

If you're using MSXML, then you're lucky because you can create a
function that uses MSXML's own serialisation routines, something like:

<msxsl:script language="javascript" implements-prefix="my">
  function serialize(nodeSet) {
    return nodeSet.item(0).xml;
  }
</msxsl:script>

which you can then include in your document() call with:

  document(concat('DB-URL?_process=', my:serialize($source)))

If you're not using MSXML, then you need to create a string
serialisation with a set of templates, e.g. for elements you need
something along the lines of:

<xsl:template match="*" mode="serialize">
  <xsl:text />&lt;<xsl:value-of select="name()" />
  <xsl:apply-templates select="@*" mode="serialize" />
  <xsl:choose>
    <xsl:when test="node()">
      <xsl:text>></xsl:text>
      <xsl:apply-templates mode="serialize" />
      <xsl:value-of select="concat('&lt;/', name(), '>')" />
    </xsl:when>
  </xsl:choose>
</xsl:template>

For attributes you need something like:

<xsl:template match="@*">
  <xsl:value-of select="concat(' ', name(), '=&quot;')" />
  <xsl:call-template name="escapeXML">
    <xsl:with-param name="string" select="." />
  </xsl:call-template>
  <xsl:text>"</xsl:text>
</xsl:template>

where 'escapeXML' is a template that takes a string and replaces all
'&' with '&amp;', '<' with '&lt;' and so on. You need a similar thing
for text nodes:

<xsl:template match="text()">
  <xsl:call-template name="escapeXML">
    <xsl:with-param name="string" select="." />
  </xsl:call-template>
</xsl:template>

These are the simple templates - if you use namespaces, you also have
to worry about those within the template for serialising the elements;
if you have comments and processing instructions that you want to keep
then you have to have templates to serialise those as well, of course.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]