fn for-each循环:令牌

发布于 2025-02-12 15:07:59 字数 2479 浏览 3 评论 0原文

我想将变量的值,令牌化和循环通过不同的令牌。 我的解决方案无法正常工作。我必须误解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 技术交流群。

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

发布评论

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

评论(3

顾挽 2025-02-19 15:07:59

键学习点在这里:

(1)XSL:构建一个文本节点。您使用tokenize()将字符串分配到令牌中,然后立即使用XSL:值将它们串在一起。

(2)XSL:变量,没有选择或作为属性,构造XML文档树。同样,这将使您的令牌共同。

如果您希望一个变量包含一系列字符串,请

<xsl:variable name="t" select="tokenize(...)" as="xs:string*"/>

从技术上讲属性在这里是多余的,但是包括它通常是很好的练习,因为它可以帮助人类读者和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 no select or as 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

<xsl:variable name="t" select="tokenize(...)" as="xs:string*"/>

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.

旧夏天 2025-02-19 15:07:59

您至少需要&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.

忆梦 2025-02-19 15:07:59

我认为您想要类似的东西:

<xsl:variable name="topicCode">1.2.3.4</xsl:variable>
<xsl:variable name="tokenizedTopicCode" select="tokenize($topicCode,'\.')"/>
<xsl:variable name="names" select="('chapter', 'section', 'subsection', 'topic')"/>
<mdcomplex name="Topic">
    <xsl:for-each select="1 to count ($tokenizedTopicCode)">
        <md name="{$names[current()]}code">
            <xsl:attribute name="value">
                <xsl:value-of select="$tokenizedTopicCode[position() le current()]" separator="."/>
            </xsl:attribute>
        </md>
    </xsl:for-each> 
</mdcomplex>

不确定为什么要在此处使用Dinters-values(); 1.1.1.1不是有效的主题代码吗?

I think you want something like:

<xsl:variable name="topicCode">1.2.3.4</xsl:variable>
<xsl:variable name="tokenizedTopicCode" select="tokenize($topicCode,'\.')"/>
<xsl:variable name="names" select="('chapter', 'section', 'subsection', 'topic')"/>
<mdcomplex name="Topic">
    <xsl:for-each select="1 to count ($tokenizedTopicCode)">
        <md name="{$names[current()]}code">
            <xsl:attribute name="value">
                <xsl:value-of select="$tokenizedTopicCode[position() le current()]" separator="."/>
            </xsl:attribute>
        </md>
    </xsl:for-each> 
</mdcomplex>

Not sure why you would want to use distinct-values() here; isn't 1.1.1.1 a valid topic code?

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