简体   繁体   English

我如何在XSLT中包含一个兄弟姐妹?

[英]How do I contain a sibling as a child in XSLT?

I have xml like so that I cannot change 我有这样的xml,所以我不能更改

<entry>
    <sn>1 a</sn>
    <dt>:to run away often from danger or evil</dt>
    <sn>b</sn>
    <dt>:to hurry toward a place of security</dt>
    <sn>2</sn>
    <dt>:to pass away swiftly</dt>
</entry>

And i'm trying to write xslt to get it to look like this 我正在尝试编写xslt使其看起来像这样

    <div class="entry">
        <div class="def_group">
            <span class="def_group_number">1</span>
            <div class="def_value">
                 <span class="def_value_letter">a</span>
                 <span class="the_def">:to run away often from danger or evil</span>
            </div>

            <div class="def_value">
                 <span class="def_value_letter">b</span>
                 <span class="the_def">:to hurry toward a place of security</span>
            </div>
        </div>

        <div class="def_group">
            <span class="def_group_number">2</span>
            <div class="def_value">
                 <span class="the_def">:to pass away swiftly</span>
            </div>
        </div>
   </div>

When I break it down I have two questions really. 分解时,我确实有两个问题。

How, using XSLT 1.0, do I separate the three cases of < sn > ("letter", "number", "number letter") ? 如何使用XSLT 1.0分隔<sn>的三种情况(“字母”,“数字”,“数字字母”)?

And, in the cases containing a number, how do I write a template with a < div > that will contain its sibling < dt > as a child? 而且,在包含数字的情况下,如何编写带有<div>的模板,该模板将包含其同级元素<dt>作为子元素?

XSLT 1.0 isn't great for string manipulation, but I think this can be done. XSLT 1.0不适用于字符串操作,但是我认为可以做到这一点。 First off, we're going to need to find all the letter sn s that belong to a number one, so let's define a key for that 首先,我们需要找到属于一个数字的所有字母sn ,因此让我们为其定义一个键

<xsl:key name="subsenseByMainSense"
   match="sn[not(number(substring(., 1, 1)) = number(substring(., 1, 1)))]"
   use="generate-id(preceding-sibling::sn[
           number(substring(., 1, 1)) = number(substring(., 1, 1))][1])" />

This looks weird but it's all idioms - the number() = number() is a way to check whether the thing in question (here the first character of the node's string value) is a number. 这看起来很奇怪,但这全是成语-number number() = number()是一种检查所涉及事物(此处为节点字符串值的第一个字符)是否为数字的方法。 If the thing you're checking is a number then the test is checking a number for equality to itself, which is always true; 如果您要检查的一个数字,那么测试就是检查一个数字是否与自己相等,这始终是正确的。 if it's not a number then the test is NaN = NaN which is always false. 如果不是数字,则测试为NaN = NaN ,这始终为假。

Now starting from an entry you want one def_group for each sn that starts with a number: 现在从一个entry开始,您希望每个以数字开头的sn都有一个def_group

<xsl:template match="entry">
  <div class="entry">
    <xsl:apply-templates select="sn[number(substring(., 1, 1))
                                  = number(substring(., 1, 1))]" />
  </div>
</xsl:template>

<xsl:template match="sn">
  <div class="def_group">
    <span class="def_group_number">
      <xsl:choose>
        <xsl:when test="contains(., ' ')">
          <xsl:value-of select="substring-before(., ' ')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="." />
        </xsl:otherwise>
      </xsl:choose>
    </span>

    <!-- do the value(s) of this element and any following letter-only ones -->
    <xsl:apply-templates mode="value"
         select=". | key('subsenseByMainSense', generate-id())" />
  </div>
</xsl:template>

Now for the value bit, if this element has a letter then add a def_value_letter , otherwise just the the_def : 现在对于值位,如果该元素具有字母,则添加def_value_letter ,否则只需the_def

<xsl:template match="sn" mode="value">
  <xsl:variable name="letter"
     select="translate(., translate(., 'abcdefghijklmnopqrstuvwxyz', ''), '')" />
  <div class="def_value">
    <xsl:if test="$letter">
      <span class="def_value_letter">
        <xsl:value-of select="$letter" />
      </span>
    </xsl:if>
    <span class="the_def">
      <xsl:value-of select="following-sibling::dt[1]" />
    </span>
  </div>
</xsl:template>

The double-translate is another idiom - it's a way to remove all characters from a string except those in a specific "whitelist", so in this case to strip out all non-letter characters from the string. double-translate是另一个习惯用法-这是一种删除字符串中特定“白名单”中所有字符以外的所有字符的方法,因此在这种情况下,应从字符串中去除所有非字母字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XSLT 1.0 - 如何排除不包含特定子节点的节点树 - XSLT 1.0 - How to exclude node trees that do not contain a specific child XSLT将XML同级移到子级 - XSLT to move XML sibling to child 如何创建 XSLT 以基于两个 XML 文档之间的兄弟节点文本匹配来合并两个文档? - How do I create an XSLT to merge two XML documents based on a sibling node text matching between the two? 在XSLT中,如何一次访问两个兄弟节点的属性值(访问节点a时,我需要访问节点b)? - In XSLT, how do I access the attribute values of two sibling nodes at once (while accessing node a, I need to access node b)? XSLT:合并兄弟节点的子元素 - XSLT: Merge child elements of sibling nodes 我如何在xslt中访问@ -values-previous-sibling - how can I access @-values of preceding-sibling in xslt 如何使用 XPATH 选择前一个兄弟元素的最后一个子节点? - How do I select last child node of preceding sibling element with XPATH? 如何根据 XML 输入(Active Directory 用户属性)中两个兄弟节点的条件处理来设置 XSLT output 的值? - How do I set the value of an XSLT output based on conditional processing of two sibling nodes in an XML input (Active Directory User attributes)? 如何在XSLT中获得以下兄弟 - How to get the following sibling in XSLT 如何使用XSLT中的apply-template根据另一个兄弟节点的子节点的匹配值来提取兄弟节点的值? - How to extract the value of the sibling node based on matching value of child node of another sibling node using apply-template in XSLT?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM