Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add some complex structure in multiple places in an XML file

Tags:

xslt

I have an XML file which has many section like the one below:

<Operations>
  <Action [some attributes ...]>
    [some complex content ...]
  </Action>
  <Action [some attributes ...]>
    [some complex content ...]
  </Action>
</Operations>

I have to add an <Action/> to every <Operations/>. It seems that an XSLT should be a good solution to this problem:

<xsl:template match="Operations/Action[last()]">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
  <Action>[some complex content ...]</Action>
</xsl:template>

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

My problem is that the content of my <Action/> contains some xPath expressions. For example:

<Action code="p_histo01">
  <customScript languageCode="gel">
    <gel:script
          xmlns:core="jelly:core"
          xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
          xmlns:soap="jelly:com.niku.union.gel.SOAPTagLibrary"
          xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"
          xmlns:sql="jelly:sql"
          xmlns:x="jelly:xml"
          xmlns:xog="http://www.niku.com/xog"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <sql:param value="${gel_stepInstanceId}"/>
    </gel:script>
  </customScript>
</Action>

The ${gel_stepInstanceId} is interpreted by my XSLT but I would like it to be copied as-is. Is that possible? How?

like image 483
Guillaume Avatar asked Dec 20 '25 08:12

Guillaume


2 Answers

The XSLT 1.0 (and also 2.0) spec defines a strict way to escape curly braces inside an attribute value, so that they are not use to denote the start/end of an AVT.

To quote from the spec:

When an attribute value template is instantiated, a double left or right curly brace outside an expression will be replaced by a single curly brace.

Use:

  <sql:param value="${{gel_stepInstanceId}}"/>
like image 141
Dimitre Novatchev Avatar answered Dec 24 '25 12:12

Dimitre Novatchev


I found that I can use :

<xsl:text disable-output-escaping="yes">
  <[CDATA[
    <Action>[...]</Action>
  ]]>
</xsl:text>

It is easier for me than to replace all instances of ${} ...

This solution works for me by running my XSLT with XMLSpy, but the documentation I found let me think that "disable-output-escaping" is deprecated ... Use at your own risk ...

like image 32
Guillaume Avatar answered Dec 24 '25 10:12

Guillaume



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!