【问题标题】:Need to take the label attribute into the title需要将label属性带入title
【发布时间】:2020-02-15 06:02:26
【问题描述】:

我的 xml 是这样的:

<sect1 label="Chapter 1:">
   <title>Pass</title>
   <sect2 label="1.1">
      <title>Pass 1</title>
      <sect3 label="1.1.1">
         <title>Pass 2</title>
         <sect4 label="1.1.1.1">
            <title>Pass 3</title>
         </sect4>
      </sect3>
    </sect2>
</sect1>

XSL 尝试如下:

<xsl:template match="/">
    <xsl:text>Heading,,,,,Sub Heading</xsl:text>
        <xsl:for-each select="//title[starts-with(name(..), 'sect')] ">
        <xsl:value-of select="for $i in 1 to count(ancestor::*) - 1 return ','" separator=""/>
        <xsl:value-of select="."/>
            <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

得到的输出是:

Heading,,,,,Sub Heading
Pass
,Pass 1
,,Pass 2
,,,Pass 3

预期的输出是:

Heading,,,,,Sub Heading
Chapter 1: Pass
,1.1 Pass 1
,,1.1.1 Pass 2
,,,1.1.1.1 Pass 3

我需要标签属性在标题之前。

【问题讨论】:

    标签: xml csv xslt xslt-2.0


    【解决方案1】:

    我认为你需要做的就是改变这个......

    <xsl:value-of select="."/>
    

    为此,要包含父级的label 属性

    <xsl:value-of select="../@label, ."/>
    

    试试这个块(我已经稍微简化了xsl:for-each,并在标题行之后添加了一个换行符

    <xsl:template match="/">
        <xsl:text>Heading,,,,,Sub Heading&#10;</xsl:text>
        <xsl:for-each select="//*[starts-with(name(), 'sect')]/title">
            <xsl:value-of select="for $i in 1 to count(ancestor::*) - 1 return ','" separator=""/>
            <xsl:value-of select="../@label, ."/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 2015-03-13
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多