Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串中每个字母的XSLT_Xslt_Xpath_Foreach - Fatal编程技术网

字符串中每个字母的XSLT

字符串中每个字母的XSLT,xslt,xpath,foreach,Xslt,Xpath,Foreach,我正在编写XSLT转换(针对XSL-FO),需要为字符串值中的每个字母重复一些内容,例如: 如果字符串存储在MyData/MyValuestring(例如MyData.MyValue=“something”)中,我需要一个类似于以下内容的字符串: <xsl:for-each select="MyData/MyValue"> <!-- What goes here to iterate through letters? --> <someTags>

我正在编写XSLT转换(针对XSL-FO),需要为字符串值中的每个字母重复一些内容,例如:

如果字符串存储在
MyData/MyValue
string(例如MyData.MyValue=“something”)中,我需要一个类似于以下内容的字符串:

<xsl:for-each select="MyData/MyValue"> <!-- What goes here to iterate through letters? -->
  <someTags>
    <xsl:value-of select="Letter" /> <!-- What goes here to output current letter? -->
  </someTags>
</xsl:for-each>


有什么想法吗?

你可以尝试一下这个肮脏丑陋的黑客,它已经被多次证明是有效的:

<xsl:for-each select="//*[position() &lt;= string-length(MyData/MyValue)]">
  <someTags>
    <xsl:value-of select="substring(MyData/MyValue, position(), 1)"/>
  </someTags>
</xsl:for-each>

如果
/*
匹配的节点数大于字符串中的字符数,则此操作将有效。。。当然,这也值得可怜的家伙在事后阅读你的代码时发表一行奇怪的评论…;-)

注意:我知道有XSLT纯粹主义者。但是,当您需要完成工作并且不太关心XSLT的超冗长时,有时这些技巧非常棒!国际海事组织


注意:我在这里提出了一个性能问题,以查看迭代或递归是否执行得更好:

您可以使用调用模板并传递参数,然后使用递归调用模板,直到没有字符为止

下面添加的示例

在这个xml上

<?xml version="1.0" encoding="utf-8"?>
<data>
        <node>something</node>
</data>

某物
这是xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="data/node">
            <xsl:call-template name="for-each-character">                    
                    <xsl:with-param name="data" select="."/>
            </xsl:call-template>
    </xsl:template>

        <xsl:template name="for-each-character">                
                <xsl:param name="data"/>
                <xsl:if test="string-length($data) &gt; 0">
                        <someTags>                            
                                <xsl:value-of select="substring($data,1,1)"/>
                        </someTags>
                        <xsl:call-template name="for-each-character">
                                <xsl:with-param name="data" select="substring($data,2)"/>
                        </xsl:call-template>
                </xsl:if>
        </xsl:template>
</xsl:stylesheet>

然后,您将能够在if语句中进行操作,以执行进一步的操作

您可以使用递归:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
    <xsl:output method="xml" indent="yes"/>


    <xsl:template match="/">

        <xsl:call-template name="get-letters">
            <xsl:with-param name="input">something</xsl:with-param>
        </xsl:call-template>

    </xsl:template>

    <xsl:template name="get-letters">
        <xsl:param name="input"/>
        <xsl:if test="string-length($input)">
            <xsl:value-of select="substring($input, 1, 1)"/>
            <xsl:text>&#xA0;</xsl:text>
            <xsl:call-template name="get-letters">
                <xsl:with-param name="input" select="substring($input, 2)"/>
            </xsl:call-template>
        </xsl:if>


    </xsl:template>
</xsl:stylesheet>

某物
 ;
输出:
s o m e t h i n g

我不确定迭代的可行性。您可以明显地使用递归,如其他答案所示。这是我的建议(除了使用模板匹配模式和未命名模板之外,与其他建议没有太大区别):


已经发布了几个XSLT1.0解决方案(因为这正是原始海报所需要的)。不过,为了进行比较,以下是如何在XSLT 2.0中使用:


问得好,+1

这就是模板/函数的用途:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:f="http://fxsl.sf.net/" xmlns:testmap="testmap"
 exclude-result-prefixes="xsl f testmap">
   <xsl:import href="str-dvc-map.xsl"/>

   <!-- to be applied on any xml source -->

   <testmap:testmap/>

   <xsl:output method="text"/>

   <xsl:template match="/">
     <xsl:variable name="vFunTripple" select="document('')/*/testmap:*[1]"/>

     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vFunTripple"/>
       <xsl:with-param name="pStr" select="'something'"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template name="trippleChar" match="testmap:*" mode="f:FXSL">
      <xsl:param name="arg1"/>

      <xsl:value-of select="concat($arg1,$arg1,$arg1)"/>
    </xsl:template>
</xsl:stylesheet>
sssooommmeeettthhhiiinnnggg

我认为XSLT版本指示是相关的。@empo-是的,你完全正确,我是在.net framework中做的,所以实际上,这是重复的:@Lukas,问题是重复的,而不是答案:)。我认为@veljkoz应该接受他最终将要使用的那个。哇,这里非常友好的xslt社区,所以:)非常感谢大家,每个答案都很有帮助,但这是我将要使用的答案,所以我选中了这个作为答案。。。但你们都从我这里得到+1;)再次感谢!感谢您链接到同一个问题-虽然这里有不同的答案,但有更多答案也不错:)-1因为答案不是通用的,可能会引起混乱并阻碍新手学习!第一个纯粹主义者:-)顺便说一句:我发现递归解更令人困惑…我不理解反对意见。我喜欢这个解决方案。@BC:有很好的理由反对。这不是一个很好的解决方案,它是一个黑客。它大部分时间都能工作,但不能保证速度快或可靠。我想这个问题已经足够好了。但总的来说,请继续阅读这里:和这里:。我希望XSLT能够真正支持循环,但是,哦,好吧……谢谢你——我正在竭尽全力尝试将
tokenize()
与各种用于“empty”的正则表达式一起使用,但结果一无所获。
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:f="http://fxsl.sf.net/" xmlns:testmap="testmap"
 exclude-result-prefixes="xsl f testmap">
   <xsl:import href="str-dvc-map.xsl"/>

   <!-- to be applied on any xml source -->

   <testmap:testmap/>

   <xsl:output method="text"/>

   <xsl:template match="/">
     <xsl:variable name="vFunTripple" select="document('')/*/testmap:*[1]"/>

     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vFunTripple"/>
       <xsl:with-param name="pStr" select="'something'"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template name="trippleChar" match="testmap:*" mode="f:FXSL">
      <xsl:param name="arg1"/>

      <xsl:value-of select="concat($arg1,$arg1,$arg1)"/>
    </xsl:template>
</xsl:stylesheet>
sssooommmeeettthhhiiinnnggg