xml 如何更新xslt中的变量值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6436272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update the variable value in xslt?
提问by Saravanan
I have declared a variable in my .xsl file. Now I want to update the older value with new value. For example:
我已经在我的 .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 <= $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 <= $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 <= $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 < $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>
采纳答案by Emiliano Poggi
If it happens that you need such a behavior in a transform, it means that probably you have to change the overall "design" of it. It's also hard to get what you are trying to do wihtout showing your input document and the wanted output.
如果碰巧在转换中需要这样的行为,则意味着您可能必须更改它的整体“设计”。如果不显示您的输入文档和想要的输出,也很难得到您想要做的事情。
Because you can't update variables, you have to rethink your code. The pattern (that I'm able to imagine) closest to your request is something like this:
因为您无法更新变量,所以您必须重新考虑您的代码。最接近您的请求的模式(我可以想象)是这样的:
<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>
回答by Ignacio Vazquez-Abrams
You can't. XSLT is a functional programming language so variables cannot be modified. Use recursion to do what you want instead.
你不能。XSLT 是一种函数式编程语言,因此不能修改变量。使用递归来做你想做的事。

