XSLT中子字符串的复杂使用

时间:2018-12-21 17:48:35

标签: xml xslt

我有一段XML代码可实现有关家庭的属性:

<?xml version="1.0" encoding="UTF-8"?>    
<family>
    <familySurname>Smith</familySurname>
        <biography firstName="John">
            <family position="dad">1</family>
            <family information="name">Smith_John (123)</family>
        </biography>
        <biography firstName="Jane">
            <family position="mom">2</family>
            <family information="name">Smith_Jane (456)</family>
        </biography>
        <biography firstName="Kelsey">
            <family position="daughter">3</family>
            <family information="name">Smith_Kelsey (789)</family>
        </biography>
</family>

我希望编写一个XSL代码,该代码将输出以下内容:

Smith 123 John
Smith 456 Jane
Smith 789 Kelsey

我将固定宽度的位记下来(为此编写了一个函数),所以我很高兴去那里。但是,我一直在等待如何使familySurname元素保持不变,同时遍历每个family @information属性并将其子字符串化。

有人可以帮助我在XSL中进行逻辑处理吗?我是这门语言的新手。

2 个答案:

答案 0 :(得分:0)

您可以使用这三个模板。第一个跳过familySurname输出,第二个跳过family元素,第三个格式化输出。

<xsl:template match="familySurname" />

<xsl:template match="family">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="biography">
    <xsl:variable name="str"       select="family[@information='name']" />
    <xsl:variable name="lastName"  select="substring-before($str,'_')" />
    <xsl:variable name="firstName" select="substring-before(substring-after($str,'_'),' ')" />
    <xsl:variable name="index"     select="substring-before(substring-after($str,'('),')')" />
    <xsl:value-of select="concat($lastName,' ',$index,' ',$firstName)" />
</xsl:template>

输出为:

Smith 123 John
Smith 456 Jane
Smith 789 Kelsey

答案 1 :(得分:0)

当您可以从它们的适当位置提取除括号中的所有属性之外的所有属性时,我不明白为什么需要“对每个@information属性进行遍历和子字符串化”:

XAUTHORITY