如何删除一些标签而不删除 xslt 中的内容并将整个内容 XML 作为输入传递给另一个模板 [英] How to remove some tags without removing the content in xslt and pass entire content XML as input to another template

查看:14
本文介绍了如何删除一些标签而不删除 xslt 中的内容并将整个内容 XML 作为输入传递给另一个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 XSLT.

源 XML:

 <?xml version="1.0" encoding="iso-8859-1"?><内容><alertHeader><ol xmlns="http://www.w3.org/1999/xhtml"><li><strong>评论</strong>您当前的可用余额.可以每周 7 天、每天 24 小时通过<a href="/ALL_UNDER_123">帐户活动</a>任何 123 ATM 或致电<a id="dynamicvariable" href="#" name="Customercare">[Customercare]</a>在<a id="dynamicvariable" href="#" name="contactNo">[contactNo]</a>.<li><strong>考虑</strong><ul><li>请与我们联系</li><li>请将此视为重要信息</li><li><strong>当前</strong>声明不符合要求<a id="dynamicvariable" href="#" name="实际账户">[实际账户]</a>,加<a id="dynamicvariable" href="#" name="totalcharges">[totalcharges]</a>使你的反应是积极的.</ol><p xmlns="http://www.w3.org/1999/xhtml"></p><div xmlns="http://www.w3.org/1999/xshtml"></div><span xmlns="http://www.w3.org/1999/xshtml"></span></alertHeader></内容>

我想编写一个 XSLT 来将标记 alertHeader 中的全部内容作为值传递给另一个模板.

我想修改这段代码如下.

 1.去掉标签

,和

;和<a></a>.我只想删除标签而不是标签的值.它应该在那里.2.将包含标签的内容传递给处理"模板.

需要输出:

 <文本><ol xmlns="http://www.w3.org/1999/xhtml"><li><strong>评论</strong>您当前的可用余额.可以每周 7 天、每天 24 小时通过<dynamicvariable name="Customercare"/>在<动态变量名称="contactNo"/>.<li><strong>考虑</strong><ul><li>请与我们联系</li><li>请将此视为重要信息</li><li><strong>当前</strong>声明不符合要求</ol></aaa>

当前 XSLT:

 <?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" indent="yes"/><xsl:template match="alertHeader"><xsl:call-template name="process"><xsl:with-param name="text" select="."/></xsl:call-template></xsl:模板><xsl:模板名称=进程"><xsl:param name="text"/><xsl:variable name="head" select="substring-before($text, '[')"/><xsl:variable name="tag" select="substring-before(substring-after($text, '['), ']')"/><xsl:variable name="tail" select="substring-after($text, ']')"/><xsl:when test="$head != '' and $tag !=''"><xsl:value-of select="$head"/><dynamicVariable name="{$tag}"/><!-- 递归步骤:处理字符串的剩余部分--><xsl:call-template name="process"><xsl:with-param name="text" select="$tail"/></xsl:call-template></xsl:when><xsl:否则><xsl:value-of select="$text"/></xsl:否则></xsl:选择></xsl:模板></xsl:stylesheet>

谁能说出我的代码需要进行哪些更改.

谢谢.

解决方案

xslt-1.0 的问题是你无法访问结果节点或节点集,因为没有 XPath 或函数或任何可以引用模板的转换结果.

按原样调用 process 是否必不可少?我要处理的方法是使 process 成为一个模板,该模板只转换一个节点,然后从 alertHeader 下的每个节点调用它,例如:

<!-- 切换模式,所以我们知道我们需要调用 process --><xsl:apply-templates mode="callproc"/></xsl:模板><xsl:template match="*" mode="callproc"><!-- 在这个节点上调用进程--><xsl:call-template name="process"><xsl:with-param name="text" select="."/></xsl:call-template><!-- 下降--><xsl:apply-templates mode="callproc"/></xsl:模板><!-- 所有需要剥离的东西--><xsl:template match="p|div|span|a" mode="callproc"><!-- 对 text() 部分的调用过程--><xsl:call-template name="process"><xsl:with-param name="text" select="text()"/></xsl:call-template><!-- 这里没有血统--></xsl:模板>

更新:没有流程模板

<xsl:output method="xml" indent="yes"/><xsl:template match="内容"><xsl:apply-templates/></xsl:模板><xsl:template match="alertHeader"><xsl:元素名称="aaa"><xsl:元素名称="文本"><!-- 切换模式,所以我们知道我们需要调用 process --><xsl:apply-templates/></xsl:element></xsl:element></xsl:模板><xsl:template match="*"><xsl:copy><!-- 下降--><xsl:apply-templates/></xsl:copy></xsl:模板><!-- 所有需要剥离的东西--><xsl:template match="xh:p|xh:div|xh:span|xsh:div|xsh:span"><xsl:apply-templates/></xsl:模板><!-- 是进程--><xsl:template match="xh:a[@name]"><xsl:element name="dynamicvariable" namespace="http://www.w3.org/1999/xhtml"><xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute></xsl:element></xsl:模板><xsl:template match="xh:a"/><xsl:template match="text()"><xsl:value-of select="."/></xsl:模板></xsl:stylesheet>

输出:

 <ol xmlns="http://www.w3.org/1999/xhtml"><li><strong>评论</strong>您当前的可用余额.可以每周 7 天、每天 24 小时通过任何 Wells Fargo ATM 或致电<动态变量名称="Call_Center_Name"/>在<动态变量名称="电话号码"/>.<li><strong>考虑</strong><ul><li>您的待处理交易以及尚未从您的可用余额中扣除的任何其他交易,例如您已开立的支票或即将进行的预定自动付款.</li><li>由于当时您的帐户中没有足够的资金而被退回的任何交易;收到您付款的个人或一方可能会重新提交它们以供付款<li><strong>存款</strong>足够的资金来建立和维持正账户余额.至少存款<动态变量名称="Absolute_Available_Balance"/>,加<动态变量名称="Total_Fee"/>在费用方面,在我们发送此通知时,您的帐户余额将被要求为正.</ol></aaa>

I am working on XSLT.

Source XML:

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


                <ol xmlns="http://www.w3.org/1999/xhtml">
                  <li>
                    <strong>Review</strong>
                    your current available balance. It can be obtained 24 hours a day, 7 days a week through
                    <a href="/ALL_UNDER_123">Account Activity</a>
                    any 123 ATM or by calling
                    <a id="dynamicvariable" href="#" name="Customercare">[Customercare]</a>
                    at
                    <a id="dynamicvariable" href="#" name="contactNo">[contactNo]</a>
                    .
                  </li>
                  <li>
                    <strong>Take into consideration</strong>


                    <ul>
                      <li>Please get in touch with us</li>
                      <li>Please consider this as important info</li>
                    </ul>


                  </li>
                  <li>
                    <strong>Current</strong>
                    Statementt doesnot match the requirement
                    <a id="dynamicvariable" href="#" name="Actual acccount">[Actual acccount]</a>
                    ,plus
                    <a id="dynamicvariable" href="#" name="totalcharges">[totalcharges]</a>
                    Make u r response as positive.
                  </li>
                </ol>
                <p xmlns="http://www.w3.org/1999/xhtml"></p>
                <div xmlns="http://www.w3.org/1999/xshtml"></div>
                <span xmlns="http://www.w3.org/1999/xshtml"></span>



              </alertHeader>
            </Content>

I want to write a XSLT to pass entire content in the tag alertHeader as value to another template.

I want to modify this code as follows.

                   1.Remove the tags   <p></p>, and <div></div>,<span></span> and <a></a>. I want to remove only tags but not the value of the tags. It should be there as it is.
                   2.Pass the content including tags to "Process" template.

Output required:

          <aaa>
            <text>

            <ol xmlns="http://www.w3.org/1999/xhtml">
              <li>
                    <strong>Review</strong>
                    your current available balance. It can be obtained 24 hours a day, 7 days a week through
                <dynamicvariable name="Customercare"/>

                at
                <dynamicvariable name="contactNo"/>

                .
              </li>
              <li>
                    <strong>Take into consideration</strong>


                    <ul>
                      <li>Please get in touch with us</li>
                      <li>Please consider this as important info</li>
                    </ul>


                  </li>
              <li>
                <strong>Current</strong>
                    Statementt doesnot match the requirement
              </li>
            </ol>
            </text>
          </aaa>

current XSLT:

              <?xml version="1.0" encoding="utf-8"?>
              <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                <xsl:output method="xml" indent="yes"/>


                <xsl:template match="alertHeader">
                  <xsl:call-template name="process">

                    <xsl:with-param name="text" select="." />


                  </xsl:call-template>

                </xsl:template>
                <xsl:template name="process">
                  <xsl:param name="text" />

                  <xsl:variable name="head" select="substring-before($text, '[')" />
                  <xsl:variable name="tag" select="substring-before(substring-after($text, '['), ']')" />
                  <xsl:variable name="tail" select="substring-after($text, ']')" />

                  <xsl:choose>
                    <xsl:when test="$head != '' and $tag != ''">
                      <xsl:value-of select="$head" />
                      <dynamicVariable name="{$tag}" />
                      <!-- recursive step: process the remainder of the string -->
                      <xsl:call-template name="process">
                        <xsl:with-param name="text" select="$tail" />
                      </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="$text" />
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:template>

              </xsl:stylesheet>

Can any one say what all the changes required for my code.

Thanks.

解决方案

The problem in xslt-1.0 is that you can't access a result node or nodeset, as in there is no XPath or function or anything to refer to the transformation result of a template.

Is it essential that you call process as it is? The way I'd go about this is to make process a template that transforms exactly one node, and then call it from every node below alertHeader, like:

<xsl:template match="alertHeader">
  <!-- switch modes so we know we need to call process -->
  <xsl:apply-templates mode="callproc"/>
</xsl:template>

<xsl:template match="*" mode="callproc">
  <!-- call process on THIS node -->
  <xsl:call-template name="process">
    <xsl:with-param name="text" select="."/>
  </xsl:call-template>
  <!-- descend -->
  <xsl:apply-templates mode="callproc"/>
</xsl:template>

<!-- all the stuff that needs stripping -->
<xsl:template match="p|div|span|a" mode="callproc">
  <!-- call process on the text() portion -->
  <xsl:call-template name="process">
    <xsl:with-param name="text" select="text()"/>
  </xsl:call-template>
  <!-- no descent here -->
</xsl:template>

Update: Without process template

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xh="http://www.w3.org/1999/xhtml"
  xmlns:xsh="http://www.w3.org/1999/xshtml">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="Content">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="alertHeader">
    <xsl:element name="aaa">
      <xsl:element name="text">
        <!-- switch modes so we know we need to call process -->
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <!-- descend -->
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- all the stuff that needs stripping -->
  <xsl:template match="xh:p|xh:div|xh:span|xsh:div|xsh:span">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- was process -->
  <xsl:template match="xh:a[@name]">
    <xsl:element name="dynamicvariable" namespace="http://www.w3.org/1999/xhtml">
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <xsl:template match="xh:a"/>

  <xsl:template match="text()">
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

Output:

                <ol xmlns="http://www.w3.org/1999/xhtml">
            <li>
                <strong>Review</strong>
                your current available balance. It can be obtained 24 hours a day, 7 days a week through

                any Wells Fargo ATM or by calling
                <dynamicvariable name="Call_Center_Name"/>
                at
                <dynamicvariable name="Phone_Number"/>
                .
            </li>
            <li>
                <strong>Take into account</strong>


                <ul>
                    <li>Your pending transactions and any additional transactions that have not yet been deducted from your available balance, such as checks you have written or upcoming scheduled automatic payments.</li>
                    <li>Any transactions that have been returned because you did not have enough money in your account at that time; they may be resubmitted for payment by the person or party who received the payment from you</li>
                </ul>


            </li>
            <li>
                <strong>Deposit</strong>
                enough money to establish and maintain a positive account balance. A deposit of at least
                <dynamicvariable name="Absolute_Available_Balance"/>
                ,plus
                <dynamicvariable name="Total_Fee"/>
                in fees, would have been required to make your account balance positive at the time we sent this notice.
            </li>
                </ol>






    </text>
</aaa>

这篇关于如何删除一些标签而不删除 xslt 中的内容并将整个内容 XML 作为输入传递给另一个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆