如何在XML文件中的多个位置添加一些复杂的结构

时间:2010-03-30 10:27:45

标签: xslt

我有一个XML文件,其中有许多部分如下所示:

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

我必须为每个<Action/>添加<Operations/>。似乎XSLT应该是解决这个问题的好方法:

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

我的问题是我的<Action/>的内容包含一些xPath表达式。例如:

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

${gel_stepInstanceId}由我的XSLT解释,但我希望它按原样复制。那可能吗?怎么样?

3 个答案:

答案 0 :(得分:4)

The XSLT 1.0 (and also 2.0) spec defines a strict way to escape curly braces在属性值中,因此它们不用于表示AVT的开头/结尾。

引用规范:

  

当属性值模板是   实例化,双左或右   表达式之外的大括号会   被一个大括号替换。

使用

  <sql:param value="${{gel_stepInstanceId}}"/>

答案 1 :(得分:1)

我发现我可以使用:

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

我更容易替换$ {} ...

的所有实例

这个解决方案适用于我使用XMLSpy运行我的XSLT,但是我发现的文档让我认为“禁用输出转义”已被弃用...使用风险自负......

答案 2 :(得分:0)

当然,这很容易:

<sql:param value="{'${gel_stepInstanceId}'}"/>

花括号是“属性值模板”(AVT)语法。在XSLT中没有定义字符串转义,所以你必须解决这个问题 - 最简单的方法是定义一个实际的 AVT并传递一个字符串值来显示。