如果需要帮助,请 XSL [英] XSL if help needed please
问题描述
我正在将 html 表单转换为 xml 序列,我正在使用递归函数来实现这一点
i am converting an html form to xml sequence, i am using a recursive function to achieve this
因此参数list"的输入将采用以下形式
so the input to param "list" will be of the form
name=value&name=value&name=value
下面的模板可以很好地执行此操作并返回如下的 xml 序列
The template below does this fine and returns an xml sequence as follows
<name>value</name><name>value</name><name>value</name>
好的,问题来了一些名称值对是特殊的,我想为它们添加一个属性,以便输出是
Ok so the problem some of the name value pairs are special and i would like to add a attribute to them so the output would be
<name>value</name><name attr="special">value</name><name>value</name>
为此,我有一个外部 xml 文件,其中包含一个特殊名称列表,如下所示
so to do this i have an external xml file with a list of special names as follows
<settings><google><option from="color"/><option from="size"/></google></settings>
因此,如果我们假设我有一个 xsl 变量 $SETTINGS 连接到上面的外部文档
So if we assume i have a xsl variable $SETTINGS connected to this external document above
<xsl:for-each select="$SETTINGS/google/option"></xsl:for-each>
应该是有 2 个子节点 1 称为颜色和另一个大小的节点
Should be node with 2 children 1 called color and the other size
我想要做的是,如果这些孩子名字中的 1 个 = $name 添加属性
wot i want to do is if 1 of these children names = $name add the attribute
类似<xsl:if test="$name = $SETTINGS/google/option/ckild name">
<xsl:template name="tokenize">
<xsl:param name="list"/>
<xsl:variable name="seperator" select="'&'"/>
<xsl:variable name="first" select="substring-before(concat($list, $seperator), $seperator)"/>
<xsl:variable name="butfirst" select="substring-after($list, $seperator)"/>
<xsl:variable name="name" select="normalize-space(substring-before($first, '='))"/>
<xsl:variable name="value" select="normalize-space(substring-after($first, '='))"/>
<xsl:if test="string-length($name)>0 and string-length($value)>0">
<xsl:element name="{$name}">
<xsl:for-each select="$SETTINGS/google/option">
-----> <xsl:if test="$name = $SETTINGS/google/option/ckild name">
<xsl:attribute name="option"/>
</xsl:if>
</xsl:for-each>
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:if>
<xsl:if test="$butfirst">
<xsl:call-template name="tokenize">
<xsl:with-param name="list" select="$butfirst"/>
<xsl:with-param name="seperator" select="$seperator"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
非常感谢蒂姆·道奇森
推荐答案
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="SETTINGS" select="/settings"/>
<xsl:template match="/">
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="'color=blue&name=value&size=big'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="pString"/>
<xsl:param name="pSeperator" select="'&'"/>
<xsl:choose>
<xsl:when test="not($pString)"/>
<xsl:when test="contains($pString,$pSeperator)">
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="substring-before($pString, $pSeperator)"/>
<xsl:with-param name="pSeperator" select="$pSeperator"/>
</xsl:call-template>
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="substring-after($pString, $pSeperator)"/>
<xsl:with-param name="pSeperator" select="$pSeperator"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vName"
select="normalize-space(substring-before($pString,'='))"/>
<xsl:variable name="vValue"
select="normalize-space(substring-after($pString,'='))"/>
<xsl:if test="$vName and $vValue">
<xsl:element name="{$vName}">
<xsl:if test="$vName = $SETTINGS/google/option/@from">
<xsl:attribute name="attr">special</xsl:attribute>
</xsl:if>
<xsl:value-of select="$vValue"/>
</xsl:element>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
使用此输入:
<settings>
<google>
<option from="color"/>
<option from="size"/>
</google>
</settings>
输出:
<color attr="special">blue</color>
<name>value</name>
<size attr="special">big</size>
注意:节点集对比
这篇关于如果需要帮助,请 XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!