Need to remove unwanted space element

时间:2017-08-04 12:27:55

标签: xml xslt-2.0

I want to remove the unwanted space element using XSL.

XML I'm testing:

<Body>
   <h1>abc</h1>
   <h1>efg</h1>
   <p>efgh</p>
   <h1> </h1>
</Body>

XSL I used:

<xsl:template match="Body">
   <xsl:copy>
      <xsl:for-each-group select="*" group-adjacent="boolean(self::h1)">
         <xsl:choose>
            <xsl:when test="current-grouping-key()">
               <h1>
                  <xsl:apply-templates select="current-group()/node()"/>
               </h1>
            </xsl:when>
            <xsl:otherwise>
               <xsl:apply-templates select="current-group()"/>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:for-each-group>
   </xsl:copy>
</xsl:template>

Output I get:

<Body>
   <h1>abcefg</h1>
   <p>efgh</p>
   <h1> </h1>
</Body>

Output I would like:

<Body>
   <h1>abcefg</h1>
   <p>efgh</p>
</Body>

I need to remove the element having the space value. Please advise. Thanks in advance

1 个答案:

答案 0 :(得分:0)

按照您编写XSL文档的方式,我假设您只想合并{ "documents": [ { "documentBase64":"<BASE64STREAM>", "documentId":"3", "fileExtension":"docx", "name":"10001000_20170803_FILE" } ], "emailSubject": "TEST - Group Audit - 10001000", "templateId": "TEMPLATE_ID", "templateRoles" : [ { "email": "JDOE@email.com", "name": "JOHN DOE", "roleName": "signer1", "tabs": { "textTabs": [ { "documentId": "3", "recipientId": "1", "tabLabel": "groupname", "value": "TEST GROUP ONE" }, { "documentId": "3", "recipientId": "1", "tabLabel": "groupnumber", "value": "10001000" }, { "documentId": "3", "recipientId": "1", "tabLabel": "txt", "value": "my@email.com" }, { "documentId": "3", "recipientId": "1", "tabLabel": "fein", "value": "870142380" }, { "documentId": "3", "recipientId": "1", "tabLabel": "physicaladdress", "value": "1 STREET WAY, , MY CITY, CA, 98001" } ] } } ], "status":"sent" } 元素。我进一步假设您希望删除&#34;空白&#34; h1元素。 (如果这些假设中的任何一个都不正确,那么这是一个非常简单的修改。)

考虑到这一点,这是实现所需输出的一种方法:

h1

使用扩展的XML输入来展示我的假设:

<xsl:strip-space elements="h1" />
<xsl:template match="Body">
  <xsl:copy>
    <xsl:for-each-group select="*" group-adjacent="boolean(self::h1)">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <xsl:if test="string-length(current-group()) > 0">
            <xsl:copy>
              <xsl:apply-templates select="current-group()"/>
            </xsl:copy>
          </xsl:if>
        </xsl:when>
        <xsl:otherwise>
          <xsl:for-each select="current-group()">
            <xsl:copy>
              <xsl:apply-templates/>
            </xsl:copy>
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

输出是:

<Body>
  <h1>abc</h1>
  <h1>efg</h1>
  <p>efgh</p>
  <p>ijkl</p>
  <h2>mnop</h2>
  <h1> </h1>
  <p> </p>
</Body>

解决方案的关键是<Body> <h1>abcefg</h1> <p>efgh</p> <p>ijkl</p> <h2>mnop</h2> <p> </p> </Body> 元素与xsl:strip-space函数相结合。这实际上导致每个string-length()元素的任何数量,并且仅删除了空格。

我还修复了第二个h1元素的主要错误,这会导致每个连续的非xsl:apply-templates元素序列被裸体合并在一起。 (您需要遍历h1节点集中的所有节点以避免这种情况。)