【问题标题】:create the heading title into CSV将标题标题创建为 CSV
【发布时间】:2020-02-14 05:57:41
【问题描述】:

我的输入 xml 如下所示:

<sect1>
   <title>Intro</title>
   <sect2>
      <title>Purpose</title>
         <sect3>
             <title>Scope</title>
         </sect3>
   </sect2>   
   <sect2>
      <title>Take</title>
   <table><title>Table 1</title></table>
   </sect2>
</sect1>

我已经为上面的 xml 创建了 XSL:

<xsl:variable name="xmlpath" select="/files/path"/>
    <xsl:variable name="rootLangLoc" select="/files/@xml:lang"/>

    <xsl:variable name="newline"><xsl:text>&#13;&#10;</xsl:text></xsl:variable>

    <xsl:variable name="linebreak"><xsl:text>
    </xsl:text>
    </xsl:variable>

    <xsl:template match="/">
    <xsl:text>Top Heading,Sub Heading</xsl:text>
        <xsl:value-of select="$linebreak"/>
        <xsl:for-each select="files/file">
            <xsl:variable name="FullName" select="concat($xmlpath, ., $rootLangLoc, '.xml')"/>
            <xsl:apply-templates select="document($FullName)" mode="csvprocess"/>
        </xsl:for-each>

<xsl:for-each select="/sect1/title">
            <xsl:apply-templates select="."/>

        <xsl:for-each select="/sect1/sect2/title">
            <xsl:value-of select="$newline"/>
            <xsl:text>,</xsl:text>
            <xsl:apply-templates select="/sect1/sect2/title"/>


            <xsl:for-each select="/sect1/sect2/sect3/title">
                <xsl:value-of select="$newline"/>
                <xsl:text>,</xsl:text>
                <xsl:text>,</xsl:text>
                <xsl:apply-templates select="/sect1/sect2/sect3/title"/>
            </xsl:for-each>

        </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

我得到的 CSV 输出如下:

Intro
,Purpose
,Take
,,Scope

例外的输出是:

Intro
,Purpose
,,Scope
,Take

我希望所有部分的标题都按照输入 xml 的正确顺序排列。我得到的输出类似于第 1 节标题的第一个和第 2 节标题的下一个。

【问题讨论】:

    标签: xml csv xslt xslt-2.0


    【解决方案1】:

    如果我猜对了您在这里尝试应用的逻辑,您可以简单地做:

    XSLT 2.0

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>
    
    <xsl:template match="/">
        <xsl:text>Top Heading,Sub Heading&#10;</xsl:text>
        <xsl:for-each select="//title ">
            <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>
    
    </xsl:stylesheet>
    

    演示https://xsltfiddle.liberty-development.net/bwdwrS


    获取当前title 级别的另一种方法是:

    substring-after(name(..), 'sect')
    

    【讨论】:

    • 一切正常。但我只想要部分的标题(sect1,sect2 ...)。它也采用表格标题。你能编辑一下吗?
    • 恐怕我不跟。什么是“表标题”?您是否使用其他输入?
    • 我已经编辑了有问题的表格标题。请看?
    • 尝试将&lt;xsl:for-each select="//title["&gt; 更改为&lt;xsl:for-each select="//title[starts-with(name(..), 'sect')] "&gt;。同样,我从示例中猜测逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2023-03-17
    • 2013-09-17
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    相关资源
    最近更新 更多