fn for-each循环:令牌
我想将变量的值,令牌化和循环通过不同的令牌。 我的解决方案无法正常工作。我必须误解tokenize()实际上做什么。
<xsl:variable name="topicCode">1.2.3.4</xsl:variable>
<xsl:variable name="tokenizedTopicCode"><xsl:value-of select="tokenize($topicCode,'\.')"/></xsl:variable>
<mdcomplex name="Topic">
<xsl:for-each select="distinct-values($tokenizedTopicCode)">
<xsl:if test="position()=1">
<md name="chaptercode">
<xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute>
</md>
</xsl:if>
<xsl:if test="position()=2">
<md name="sectioncode">
<xsl:attribute name="value"><xsl:value-of select="concat($tokenizedTopicCode[position()=1],'.',.)"/></xsl:attribute>
</md>
</xsl:if>
<xsl:if test="position()=3">
<md name="subsectioncode">
<xsl:attribute name="value"><xsl:value-of select="concat($tokenizedTopicCode[position()=1],'.',$tokenizedTopicCode[position()=2],'.',.)"/></xsl:attribute>
</md>
</xsl:if>
<xsl:if test="position()=4">
<md name="topiccode">
<xsl:attribute name="value"><xsl:value-of select="concat($tokenizedTopicCode[position()=1],'.',$tokenizedTopicCode[position()=2],'.',$tokenizedTopicCode[position()=3],.)"/></xsl:attribute>
</md>
</xsl:if>
</xsl:for-each>
</mdcomplex>
预期:
<mdcomplex name="Topic">
<md name="chaptercode" value="1"/>
<md name="sectioncode" value="1.2"/>
<md name="subsectioncode" value="1.2.3"/>
<md name="topiccode" value="1.2.3.4"/>
</mdcomplex>
实际:
<mdcomplex name="Topic">
<md name="chaptercode" value="1 2 3 4"/>
</mdcomplex>
在&lt; xsl的开始之后
<xsl:for-each select="distinct-values($tokenizedTopicCode)">
<xsl:message><xsl:value-of select="."/></xsl:message>
我还添加了&lt; xsl:Message&gt;
不同令牌的值(1、2、3、4)。相反,它一次输出所有令牌:“ 1 2 3 4”。
如何将变量分为不同的令牌并循环通过它们? 我在氧气上使用撒克逊9.9.1.7。
I want to take the value of variable, tokenize it, and loop through the different tokens.
My solutions does not work as expected. I must be misunderstanding what tokenize() actually does.
<xsl:variable name="topicCode">1.2.3.4</xsl:variable>
<xsl:variable name="tokenizedTopicCode"><xsl:value-of select="tokenize($topicCode,'\.')"/></xsl:variable>
<mdcomplex name="Topic">
<xsl:for-each select="distinct-values($tokenizedTopicCode)">
<xsl:if test="position()=1">
<md name="chaptercode">
<xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute>
</md>
</xsl:if>
<xsl:if test="position()=2">
<md name="sectioncode">
<xsl:attribute name="value"><xsl:value-of select="concat($tokenizedTopicCode[position()=1],'.',.)"/></xsl:attribute>
</md>
</xsl:if>
<xsl:if test="position()=3">
<md name="subsectioncode">
<xsl:attribute name="value"><xsl:value-of select="concat($tokenizedTopicCode[position()=1],'.',$tokenizedTopicCode[position()=2],'.',.)"/></xsl:attribute>
</md>
</xsl:if>
<xsl:if test="position()=4">
<md name="topiccode">
<xsl:attribute name="value"><xsl:value-of select="concat($tokenizedTopicCode[position()=1],'.',$tokenizedTopicCode[position()=2],'.',$tokenizedTopicCode[position()=3],.)"/></xsl:attribute>
</md>
</xsl:if>
</xsl:for-each>
</mdcomplex>
Expected:
<mdcomplex name="Topic">
<md name="chaptercode" value="1"/>
<md name="sectioncode" value="1.2"/>
<md name="subsectioncode" value="1.2.3"/>
<md name="topiccode" value="1.2.3.4"/>
</mdcomplex>
Actual:
<mdcomplex name="Topic">
<md name="chaptercode" value="1 2 3 4"/>
</mdcomplex>
I also added a <xsl:message>
right after the start of the <xsl:for-each>
loop:
<xsl:for-each select="distinct-values($tokenizedTopicCode)">
<xsl:message><xsl:value-of select="."/></xsl:message>
I expected it to output the value of the different tokens (1, 2, 3, 4). Instead, it outputs all tokens in one go: "1 2 3 4".
How can I split the variable into the different tokens and loop through them?
I am using Saxon 9.9.1.7 on Oxygen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
键学习点在这里:
(1)
XSL:
构建一个文本节点。您使用tokenize()将字符串分配到令牌中,然后立即使用XSL:值将它们串在一起。(2)
XSL:变量
,没有选择
或作为属性,构造XML文档树。同样,这将使您的令牌共同。如果您希望一个变量包含一系列字符串,请
从技术上讲属性在这里是多余的,但是包括它通常是很好的练习,因为它可以帮助人类读者和XSLT编译器spot erich aincom代码中的错误。
Key learning points here:
(1)
xsl:value-of
constructs a single text node. You're splitting a string into tokens using tokenize(), and then you're immediately stringing them back together using xsl:value-of.(2)
xsl:variable
, with noselect
oras
attribute, constructs an XML document tree. Again that's going to munge your tokens together.If you want a variable to contain a sequence of strings, do
Technically the
as
attribute here is redundant, but it's generally good practice to include it, because it helps both the human reader and the XSLT compiler spot any mistakes in your code.您至少需要
&lt; XSL:变量name =“ tokenizedTopicCode” select =“ tokenize($ topic code,'\。')”/&gt;
而不是&lt; xsl:xsl:xsl:xsl:varible name = varible name = “ TokenizedTopicCode”&gt;&lt; xsl:值select =“ tokenize($ topic code,'\。')”/&gt;&lt;/xsl:变量&gt;
。但是,我看不到您期望使用位置谓词在Eg
$ topic Code
中选择内容,该变量不是项目的序列。You at least need
<xsl:variable name="tokenizedTopicCode" select="tokenize($topicCode,'\.')"/>
instead of<xsl:variable name="tokenizedTopicCode"><xsl:value-of select="tokenize($topicCode,'\.')"/></xsl:variable>
.I don't see, however, how you expect to select stuff in e.g.
$topicCode
with a positional predicate, that variable is not a sequence of items.我认为您想要类似的东西:
不确定为什么要在此处使用
Dinters-values()
;1.1.1.1
不是有效的主题代码吗?I think you want something like:
Not sure why you would want to use
distinct-values()
here; isn't1.1.1.1
a valid topic code?