[英]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.