如何更新xslt中的变量值?

2023-12-26

我已在 .xsl 文件中声明了一个变量。现在我想用新值更新旧值。例如:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">

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

  <xsl:template match="/">

    <Document>
      <xsl:variable name="topLevelHeadings" select = "//w:body/w:p[w:pPr[w:pStyle[@w:val='Heading1']]]"/>
      <xsl:variable name="beforeHeading" select="false()"/>


      <xsl:choose>

       <xsl:when test="$beforeHeading">
          <xsl:apply-templates select="//w:body/w:p">
            <xsl:with-param name="scope" select="count(//w:body/child::*)-1"/>
          </xsl:apply-templates>
       </xsl:when> 

        <xsl:when test="$topLevelHeadings">
          <xsl:variable name="beforeHeading" select="true()"/>
          <xsl:apply-templates select="$topLevelHeadings">
               <xsl:with-param name="scope" select="count(//w:body/child::*)-1"/>
          </xsl:apply-templates>
        </xsl:when>

        <xsl:otherwise>
          <xsl:apply-templates select="//w:body/w:p[w:r[w:t]]">
               <xsl:with-param name="scope" select="count(//w:body/child::*)-1"/>
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </Document>
  </xsl:template>

  <xsl:template match="w:body/w:p">
    <xsl:param name = "scope"/>
    <xsl:variable name ="index" select="count(preceding-sibling::*)"/>
    <xsl:if test = "$index &lt;= $scope">
      <Paragraph>
        <xsl:attribute name="index">
          <xsl:value-of select="$index" />
        </xsl:attribute>
        <xsl:apply-templates select=".//w:r/w:t"/>
      </Paragraph>
    </xsl:if>
  </xsl:template>



    <xsl:template match="w:t">
        <xsl:value-of select="."/>
    </xsl:template>

   <xsl:template match="w:body/w:p">
    <xsl:param name = "scope"/>
    <xsl:variable name ="index" select="count(preceding-sibling::*)"/>
    <xsl:if test = "$index &lt;= $scope">
      <Paragraph>
        <xsl:attribute name="index">
          <xsl:value-of select="$index" />
        </xsl:attribute>
        <xsl:apply-templates select=".//w:r/w:t"/>
      </Paragraph>
    </xsl:if>
  </xsl:template>

    <xsl:template name="get-para-index">
        <xsl:param name="node"/>
        <xsl:value-of select="count($node/preceding-sibling::*)"/>
    </xsl:template>

    <xsl:template match="//w:body/w:p[w:pPr[w:pStyle]]">
    <xsl:param name = "scope"/>

        <xsl:variable name="currIndex" select="count(preceding-sibling::*)"/>            

        <xsl:if test="$currIndex &lt;= $scope"> 

            <!-- Get current heading value -->
            <xsl:variable name="currHeading" select="./w:pPr/w:pStyle/@w:val"/>

            <!-- Heading tag -->  
            <xsl:element name="{$currHeading}">

            <!-- Get heading text -->
            <Title>
              <xsl:attribute name ="index">
                <xsl:value-of select="$currIndex"/>
              </xsl:attribute>
             <xsl:apply-templates select=".//w:r/w:t"/> 
            </Title> 

            <!-- Get the scope of paragraphs inside this heading -->
            <xsl:variable name="nextHeading" select="following-sibling::w:p[w:pPr[w:pStyle[@w:val]]][1]"/>

            <xsl:variable name="paraScope">
                <xsl:choose>
                    <xsl:when test="$nextHeading">
                        <xsl:call-template name="get-para-index"> 
                            <xsl:with-param name="node" select="$nextHeading"/> 
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                         <xsl:value-of select="count(//w:body/child::*)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>                       

            <!-- Handle paragraphs under this heading -->                    
            <xsl:apply-templates select="following-sibling::w:p[//w:r and not(w:pPr[w:pStyle])]">
                <xsl:with-param name="scope" select="$paraScope"/>
            </xsl:apply-templates>

            <!-- Get the first heading after current node at the same level -->
            <xsl:variable name="nextSibling" select="following-sibling::w:p[w:pPr[w:pStyle[@w:val=$currHeading]]][1]"/>

            <!-- Get its index -->
            <xsl:variable name="nextSiblingIndex">
                <xsl:choose>
                <xsl:when test="$nextSibling">                
                    <xsl:call-template name="get-para-index">
                        <xsl:with-param name="node" select="$nextSibling"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                <xsl:value-of select="$scope"/>
                </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <!-- Set the scope of this node - this will be the smaller of nextSiblingIndex and current scope -->
            <xsl:variable name="currScope">
                <xsl:choose>
                    <xsl:when test="$nextSiblingIndex &lt; $scope">
                        <xsl:value-of select="$nextSiblingIndex"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$scope"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <xsl:variable name="nextHead" select="concat('Heading', number(substring-after($currHeading, 'Heading'))+1)"/>            

            <!-- Get a list of child nodes (headings) for the current node -->
            <xsl:variable name="nextLevelHeadings" select="following-sibling::w:p[w:pPr[w:pStyle[@w:val=$nextHead]]]"/>            

            <!-- Apply recursively for next level headings within the scope -->
            <xsl:apply-templates select="$nextLevelHeadings">
                <xsl:with-param name="scope" select="$currScope"/> 
            </xsl:apply-templates>

            <!-- Close heading tag -->
            </xsl:element> 
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

如果您恰好在转换中需要这样的行为,则意味着您可能必须更改它的整体“设计”。如果不显示输入文档和所需的输出,也很难获得您想要执行的操作。

因为您无法更新变量,所以您必须重新考虑您的代码。最接近您的要求的模式(我能想象)是这样的:

    <xsl:template match="/">

        <xsl:variable name="topLevelHeadings" select="//w:body/w:p
                [w:pPr[w:pStyle[@w:val='Heading1']]]"/>

        <xsl:variable name="beforeHeading"> 
            <xsl:choose>
                <xsl:when test="$topLevelHeadings">
                    <xsl:value-of select="true()"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="false()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <!-- your choose staff -->

        <!-- for instance --> 
        <xsl:if test="$beforeHeading='true'">
            <xsl:message>pass</xsl:message>
        </xsl:if>

    </xsl:template>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更新xslt中的变量值? 的相关文章

随机推荐

  • 如何将字符串转换为枚举?

    我正在尝试在 PowerShell 中将字符串转换为枚举值 但在任何地方都找不到它 我得到一个 JSON 结果 我只想使用定义为字符串的 Healthstate enum HealthState Invalid 0 Ok 1 Warning
  • 如何在没有用户名和密码的情况下验证移动应用程序?

    我正在构建一个使用 OpenId 来验证用户身份的 Web 应用程序 就像 Stackoverlfow 所做的那样 也会有一个移动应用程序 例如安卓或iPhone 这些应用程序必须以某种方式进行身份验证或登录 才能访问数据并更新属于用户的内
  • iPhone - 在 UIView 上绘制透明矩形以显示下面的视图

    我目前有两个 UIView 一个是红色背景 另一个是蓝色 蓝色视图是红色视图的子视图 我想做的是能够在蓝色视图上 剪切 出矩形 以便红色视图可见 你打算怎样做呢 您必须覆盖顶视图的drawRect方法 因此 例如 您可以创建一个HoleyV
  • 如何使用 JVMTI 代理重新转换没有进一步调用的执行方法?

    我在运行时出于各种目的检测类文件 为此 我使用了 JVMTI 代理 我检测方法的策略是调用RetransformClasses要调用的函数ClassFileLoadHook 此策略适用于在检测后进行任何进一步调用的所有方法 因为实际检测发生
  • 任意维度的 Numpy 切片

    我想对 numpy 数组进行切片以获得i最后一个维度中的第一个索引 对于 3D 数组 这将是 slice myarray i 但我正在编写一个函数 我可以在其中获取任意维度的数组 因此对于 4D 数组 我需要myarray i 等等 有没有
  • 在 500 毫秒内更改 JButton 颜色

    我的任务是让按钮在按下时每 500 毫秒改变一次颜色 从红色变为黑色 每次按下按钮时都会开始和停止 import java awt import java awt event import javax swing public class
  • 如何将仪表板设置为登录后的第一个屏幕反应本机

    我正在使用react native 其中我的第一个屏幕是欢迎屏幕 我想在用户登录时在我的第一个屏幕上设置仪表板 这是我的代码 componentWillMount var self this AsyncStorage getItem App
  • 我怎么做?在代码中插入图像?

    请在回答之前先检查链接 以便您能明白我想说的 我有检查代码 https github com tstyle EmoView在 github 中的 Xcode 中我发现了类似这样的代码class https github com tstyle
  • 我是否必须对训练数据集和测试数据集分别进行 one-hot 编码? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在研究分类问题 并将数据分为训练集和测试集 我有几个分类列 大约 4 6 我正在考虑使用pd get dummies将我的分类值转
  • 通过 linq 中字符串的一部分来区分

    鉴于这个集合 var list new 1 one 2 two no number 2 duplicate 300 three hundred 4 ignore this 如何获取以数字后跟点开头的项目子集 regex d with dis
  • str_get_html 未加载有效的 html 字符串

    我使用curl 收到一个html 字符串 curl setopt ch CURLOPT RETURNTRANSFER true html string curl exec ch When I echo我看到了一个完美的 html 来满足我的
  • 设置新文本时更改 UITextField 中的光标

    我发现一件事 如果我设置 yes 来运行 textField shouldChangeCharactersInRange replacementString 光标停留在我输入的同一位置 例如 光标 gt Empty type a a gt
  • Java 维基文本解析器 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 对于一个具有易于使用且可配置的 API 的漂亮解析器有什么想法吗 我希望向其提供数据 例如 选择我想要
  • iPad 屏幕不支持自动布局

    I have created view using autolayout it works fine in all iPhone devices but it left some blank space at left right top
  • xcode 6.1 iOS 8.1 NSLocale displayNameForKey NSLocaleIdentifier 返回 nil

    NSString countryNameByCode NSString countryCode NSString identifier NSLocale localeIdentifierFromComponents NSLocaleCoun
  • 从 1 个单词的字符串中提取数字

    在我试图制作的这个程序中 我有一个表达式 例如 I 23mm 或 H 4V 并且我试图从中提取 23 或 4 以便我可以把它变成一个整数 我一直遇到的问题是 由于我试图取出数字的表达式是 1 个单词 所以我不能使用 split 或任何东西
  • C++:Linux 中的计时(使用 Clock())不同步(由于 OpenMP?)

    在程序的顶部和结尾 我使用clock 来计算程序需要多长时间才能完成 不幸的是 它似乎只花费了报告时间的一半 我用 time 命令仔细检查了这一点 我的程序报告 45 86秒完成 时间命令报告 真实0米22 837秒 用户 0m45 735
  • 在 Python TCP 流中使用分隔符

    我正在开发一个使用 TCP 协议从天线收集 ADS B 消息的程序 由于我是Python新手 我使用以下脚本来建立连接 问题是我同时收到多条消息 因为 TCP 是面向流的 例如 我想使用 n 分隔符分隔每条消息 每条消息开头有 结尾有 长度
  • 获取 Woocommerce 中当天的订单总购买金额

    对于 Woocommerce 我在下面编写了代码 试图获取今天的订单总购买金额 function order total woo fahad Get orders from people named John that were paid
  • 如何更新xslt中的变量值?

    我已在 xsl 文件中声明了一个变量 现在我想用新值更新旧值 例如