【问题标题】:XSLT Replace Empty Node with DummyXSLT 用 Dummy 替换空节点
【发布时间】:2016-12-15 17:41:34
【问题描述】:

我的要求如下 1) 用 %20 替换空格 2) 用 %2F 替换 / 3) 值为 dummy 的空元素

示例输入:

<?xml version="1.0" encoding="UTF-8" ?>
  <process xmlns="http://xmlns.oracle.com/CCS/Project5/BPELProcess1">
     <Sender>Sender 1</Sender>
     <TransactionId>TransactionId/2</TransactionId>
     <TransactionType>TransactionType5</TransactionType>
     <Status>Status6</Status>
     <Limit>70.73</Limit>
     <Remarks>Remarks8</Remarks>
     <Result>GlobalResult9</Result>
     <Type>DecisionType10</Type>
     <DecidedBy>DecidedBy11</DecidedBy>
       <AddRequest1/>
     <AddRequest2>RAMA</AddRequest2>
     </process>

所需输出:

<process xmlns="http://xmlns.oracle.com/CCS/Project5/BPELProcess1">
<Sender>Sender%201</Sender>
<TransactionId>TransactionId%2F2</TransactionId>
<TransactionType>TransactionType5</TransactionType>
<Status>Status6</Status>
<Limit>70.73</Limit>
<Remarks>Remarks8</Remarks>
<Result>GlobalResult9</Result>
<Type>DecisionType10</Type>
<DecidedBy>DecidedBy11</DecidedBy>
<AddRequest1>DUMMY</AddRequest1>
<AddRequest2>RAMA</AddRequest2>
</process>

我在 XSLT 下尝试过,但找不到 wat 将空节点替换为 Dummy 例如

<AddRequest1></AddRequest1> is not coverted to          <AddRequest1>DUMMY</AddRequest1>

尝试过的XSLT如下

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
</xsl:copy>
  </xsl:template>
  <xsl:template match="//text()">
    <xsl:call-template name="rep_SPLChar">
  <xsl:with-param name="text" select="."/>
    </xsl:call-template>
      </xsl:template>

  <xsl:template name="rep_SPLChar">
    <xsl:param name="text"/>
    <xsl:variable name="temp_space" select="'%20'"/>
    <xsl:variable name="temp_backslash" select="'%2F'"/>
    <xsl:if test="normalize-space($text) != ''">
      <xsl:choose>
        <xsl:when test="contains($text,' ')">
          <xsl:call-template name="rep_SPLChar">
            <xsl:with-param name="text"
           select="concat((concat(substring-before($text,'          '),$temp_space)),substring-after($text,' '))"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:choose>
        <xsl:when test="contains($text,'/')">
          <xsl:call-template name="rep_SPLChar">
            <xsl:with-param name="text"
                            select="concat((concat(substring-    before($text,'/'),$temp_backslash)),substring-after($text,'/'))"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$text"/>
        </xsl:otherwise>
      </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
    </xsl:stylesheet>

【问题讨论】:

    标签: xslt xslt-1.0


    【解决方案1】:

    尝试将此模板添加到您的 XSLT 以匹配“空”元素

    <xsl:template match="*[not(*)][not(normalize-space())]">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:text>DUMMY</xsl:text>
      </xsl:copy>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多