【问题标题】:remove text from a node in input XML从输入 XML 中的节点中删除文本
【发布时间】:2017-09-07 22:20:09
【问题描述】:

我正在尝试将整个输入 xml 复制到一个字符串中以进行进一步处理,并且我还需要在复制变量之前从特定节点(计划代码)中删除文本。我可以知道如何使用 xslt 实现这一目标

输入 XML:

<CallMember>
    <PlanD>
      <abcpr>you</abcpr>
      <Desd>Protection</Desd>
      <plancode>76789</plancode>
      <plaDesc>goody</plaDesc>
     </PlanD>
   <fType>ONLINE</fType>
</CallMember>

我正在尝试 XSLT:

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt" xmlns:func="http://exslt.org/functions" xmlns:dp="http://www.datapower.com/extensions" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:tglfn="http://test.com/tgl" xmlns:date="http://exslt.org/dates-and-times" exclude-result-prefixes="#all" extension-element-prefixes="dp regexp">

    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
    <xsl:template match="/">

            <xsl:variable name="InputRequest">
                <xsl:copy-of select="."/>                   
            </xsl:variable>

            <xsl:variable name="modifiedRequest">
                <xsl:copy-of select="."/>   
                <plancode></plancode>
            </xsl:variable>
</xsl:template>

我期望在 modifiedRequest 变量中的输出:

<CallMember>
    <PlanD>
      <abcpr>you</abcpr>
      <Desd>Protection</Desd>
      <plancode></plancode>  <!-- this value needs to get emptied -->
      <plaDesc>goody</plaDesc>
     </PlanD>
   <fType>ONLINE</fType>
</CallMember>

【问题讨论】:

    标签: xslt xslt-2.0 ibm-datapower exslt


    【解决方案1】:

    使用身份模板与(几乎)空模板进行过滤,并使用apply-templates

    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:xalan="http://xml.apache.org/xslt" 
        xmlns:func="http://exslt.org/functions" 
        xmlns:dp="http://www.datapower.com/extensions" 
        xmlns:regexp="http://exslt.org/regular-expressions" 
        xmlns:tglfn="http://test.com/tgl" 
        xmlns:date="http://exslt.org/dates-and-times" 
        exclude-result-prefixes="#all" extension-element-prefixes="dp regexp">    
        <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
    
        <!-- identity template -->
        <xsl:template match="node()|@*">
          <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
          </xsl:copy>
        </xsl:template>
    
        <xsl:template match="/">
    
            <xsl:variable name="InputRequest">
                <xsl:copy-of select="."/>
            </xsl:variable>
    
            <!-- copies subtree except matching empty template -->
            <xsl:variable name="modifiedRequest">
              <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
              </xsl:copy>
            </xsl:variable>
    
        </xsl:template>
    
        <!-- (nearly) empty template copies only element without content -->
        <xsl:template match="plancode">
          <xsl:copy />
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-05
      • 2020-01-15
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 2023-03-24
      相关资源
      最近更新 更多