XSL 示例编码

发布于 2024-10-09 03:42:41 字数 1832 浏览 0 评论 0原文

我不是 XSL 专家,正在努力解决简单的逻辑:

我有一个值为“A、B、C”的 XSL 变量,想要将其拆分并将各个值存储到三个不同的 XSL 变量中,例如

X= A
Y = B
Z= C

但有时它可能只有一个/两个值或没有值...

如果它只有一个值,那么变量值应该类似于

X= A
Y =
Z=

如果它没有任何值,则

X=
Y =
Z=

请帮助我使用相同的 XSL 代码

可以说:

标签的值为“测试,演示,样本”,然后我想像这样分割,

<xsl:choose>
    <xsl:when test="contains($Tags,',')">
        <xsl:variable name="Tags1">
            <xsl:value-of select="substring-before($Tags,',')" />
        </xsl:variable>
        <xsl:variable name="ATag1">
            <xsl:value-of select="substring-after($Tags,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags1"/>
        <xsl:variable name="ATags1"/>
    </xsl:otherwise>
</xsl:choose>

<xsl:choose>
    <xsl:when test="contains($ATags1,',')">
        <xsl:variable name="Tags2">
            <xsl:value-of select="substring-before($ATags1,',')" />
        </xsl:variable>
        <xsl:variable name="ATag2">
            <xsl:value-of select="substring-after($ATags1,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags2"/>
        <xsl:variable name="ATags2"/>
    </xsl:otherwise>
</xsl:choose>



<xsl:choose>
    <xsl:when test="contains($ATags2,',')">
        <xsl:variable name="Tags3">
            <xsl:value-of select="substring-before($ATags2,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags3"/>
    </xsl:otherwise>
</xsl:choose>

但它对我不起作用......

I am not a XSL expert and struggling with the simple logic:

I have a XSL variable with the value of "A,B,C", want to split it and store the individual values into three different XSL variable like

X= A
Y= B
Z= C

but sometimes either it may have only one/two value or no values...

if it has only one value then the variable values should be like

X= A
Y=
Z=

if it does not have any value then

X=
Y=
Z=

Kindly help me with the XSL code for the same

Lets say:

Tags has the value of "Test,Demo,Sample" then i would like to split like this

<xsl:choose>
    <xsl:when test="contains($Tags,',')">
        <xsl:variable name="Tags1">
            <xsl:value-of select="substring-before($Tags,',')" />
        </xsl:variable>
        <xsl:variable name="ATag1">
            <xsl:value-of select="substring-after($Tags,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags1"/>
        <xsl:variable name="ATags1"/>
    </xsl:otherwise>
</xsl:choose>

<xsl:choose>
    <xsl:when test="contains($ATags1,',')">
        <xsl:variable name="Tags2">
            <xsl:value-of select="substring-before($ATags1,',')" />
        </xsl:variable>
        <xsl:variable name="ATag2">
            <xsl:value-of select="substring-after($ATags1,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags2"/>
        <xsl:variable name="ATags2"/>
    </xsl:otherwise>
</xsl:choose>



<xsl:choose>
    <xsl:when test="contains($ATags2,',')">
        <xsl:variable name="Tags3">
            <xsl:value-of select="substring-before($ATags2,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags3"/>
    </xsl:otherwise>
</xsl:choose>

however it is not working for me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2024-10-16 03:42:41

这是一个 XSLT 2.0 解决方案

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()">
  <xsl:variable name="vSeq" select="tokenize(.,',')"/>

  <xsl:variable name="X" select="$vSeq[1]"/>
  <xsl:variable name="Y" select="$vSeq[2]"/>
  <xsl:variable name="Z" select="$vSeq[3]"/>

  <xsl:value-of select=
   "concat('X = ',$X, '
',
           'Y = ',$Y, '
',
           'Z = ',$Z, '
'
           )"
   />
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时

<t>A,B,C</t>

生成所需的正确结果

X = A
Y = B
Z = C

XSLT 1.0 解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()">
  <xsl:variable name="X" select="substring-before(.,',')"/>
  <xsl:variable name="Y" select=
   "substring-before(substring-after(.,','),',')"/>
  <xsl:variable name="Z" select=
   "substring-before(substring-after(.,','),',')"/>

  <xsl:value-of select=
   "concat('X = ',$X, '
',
           'Y = ',$Y, '
',
           'Z = ',$Z, '
'
           )"
   />
 </xsl:template>
</xsl:stylesheet>

当此转换应用于同一个 XML 文档(如上所述)时,就会产生所需的正确结果

X = A
Y = B
Z = B

Here is an XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()">
  <xsl:variable name="vSeq" select="tokenize(.,',')"/>

  <xsl:variable name="X" select="$vSeq[1]"/>
  <xsl:variable name="Y" select="$vSeq[2]"/>
  <xsl:variable name="Z" select="$vSeq[3]"/>

  <xsl:value-of select=
   "concat('X = ',$X, '
',
           'Y = ',$Y, '
',
           'Z = ',$Z, '
'
           )"
   />
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t>A,B,C</t>

the wanted, correct result is produced:

X = A
Y = B
Z = C

XSLT 1.0 solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()">
  <xsl:variable name="X" select="substring-before(.,',')"/>
  <xsl:variable name="Y" select=
   "substring-before(substring-after(.,','),',')"/>
  <xsl:variable name="Z" select=
   "substring-before(substring-after(.,','),',')"/>

  <xsl:value-of select=
   "concat('X = ',$X, '
',
           'Y = ',$Y, '
',
           'Z = ',$Z, '
'
           )"
   />
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the same XML document (as above), the wanted, correct result is produced:

X = A
Y = B
Z = B
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文