如何处理".xsl"中的字符串数组文件? [英] how to handle array of strings in ".xsl" file?

查看:81
本文介绍了如何处理".xsl"中的字符串数组文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.xsl文件中有一个字符串数组,现在我必须使用每个空格分隔的字符串.我如何获得琴弦?

I have an array of strings in .xsl file, now I have to use the each string separeted by spaces differently. How I can get the strings?

我有以下字符串数组:

strarray="hw.oh.xml hg.hd.gnl th.ik.lkj"

我必须分别获取"hw.oh.xml","hg.hd.gnl","th.ik.lkj"字符串才能对其执行一些操作.

I have to get "hw.oh.xml" , "hg.hd.gnl" , "th.ik.lkj" strings separetaly to perform some operation on it.

我该怎么做?

推荐答案

有很多方法可以做到这一点:

I.使用XPath substring-before() substring-after()函数:

I. Using the XPath substring-before() and substring-after() functions:

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

 <xsl:variable name="vStrArray" select="'hw.oh.xml hg.hd.gnl th.ik.lkj'"/>

 <xsl:template match="/">
  <xsl:value-of select="substring-before($vStrArray, ' ')"/>
  <xsl:text>&#xA;</xsl:text>

  <xsl:value-of select="substring-before(substring-after($vStrArray, ' '),' ')"/>
  <xsl:text>&#xA;</xsl:text>
  <xsl:value-of select="substring-after(substring-after($vStrArray, ' '),' ')"/>
  <xsl:text>&#xA;</xsl:text>
 </xsl:template>
</xsl:stylesheet>

将此转换应用于任何XML文档(未使用)时,将生成所需结果(数组"中的每个项目):

hw.oh.xml
hg.hd.gnl
th.ik.lkj

这种方法很快会变得非常复杂,除只有2-3个项目的数组"外,不建议使用.

This method can quickly become overwhelmingly complex and is not recommended except for "arrays" of just 2-3 items.

II.在XSLT 1.0中将数组"表示为XML文档:

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

 <my:array>
  <item>hw.oh.xml</item>
  <item>hg.hd.gnl</item>
  <item>th.ik.lkj</item>
 </my:array>

 <xsl:variable name="vStrArray"
      select="document('')/*/my:array/*"/>

 <xsl:template match="/">
  <xsl:value-of select="$vStrArray[1]"/>
  <xsl:text>&#xA;</xsl:text>

  <xsl:value-of select="$vStrArray[2]"/>
  <xsl:text>&#xA;</xsl:text>
  <xsl:value-of select="$vStrArray[3]"/>
  <xsl:text>&#xA;</xsl:text>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于相同的XML文档(任意)时,将产生所需的正确结果:

hw.oh.xml
hg.hd.gnl
th.ik.lkj

我推荐这种表示数组"的方法-用于XSLT 1.0应用程序.

III.XSLT 2.0/XPath 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:variable name="vStrArray"
      select="'hw.oh.xml', 'hg.hd.gnl', 'th.ik.lkj'"/>

 <xsl:template match="/">
  <xsl:sequence select=
   "for $i in 1 to count($vStrArray)
     return
        concat($vStrArray[$i], '&#xA;')
   "/>
 </xsl:template>
</xsl:stylesheet>

结果:

 hw.oh.xml
 hg.hd.gnl
 th.ik.lkj

更新:OP评论说,他坚持使用以空格分隔的值的初始表示形式,该表示形式包含在单个字符串中.

UPDATE: The OP commented that he is stuck with the initial representation of space-separated values, contained in a single string.

IV.将空格分隔的值字符串转换为XML片段,以方便使用.

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

    <xsl:variable name="vStrArray" select="'hw.oh.xml hg.hd.gnl th.ik.lkj'"/>

    <xsl:variable name="vrtfDoc">
      <xsl:call-template name="makeIndex">
        <xsl:with-param name="pText" select="$vStrArray"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="vStrIndex" select="ext:node-set($vrtfDoc)/*"/>

    <xsl:template match="/">
        <xsl:value-of select="$vStrIndex[1]"/>
        <xsl:text>&#xA;</xsl:text>
        <xsl:value-of select="$vStrIndex[2]"/>
        <xsl:text>&#xA;</xsl:text>
        <xsl:value-of select="$vStrIndex[3]"/>
    </xsl:template>

  <xsl:template name="makeIndex">
     <xsl:param name="pText"/>

     <xsl:if test="string-length($pText)>0">
      <item>
       <xsl:value-of select=
         "substring-before(concat($pText,' '), ' ')"/>
      </item>
      <xsl:call-template name="makeIndex">
       <xsl:with-param name="pText" select=
       "substring-after($pText,' ')"/>
      </xsl:call-template>
     </xsl:if>
    </xsl:template>
</xsl:stylesheet>

此转换从字符串创建XML片段,其中每个< item> 元素仅包含一个字符串值.那么它的使用就好像它是一个数组一样.

This transformation creates an XML fragment from the string in which every <item> element contains just one of the string values. Then its use is just as if it were an array.

这篇关于如何处理".xsl"中的字符串数组文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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