繁体   English   中英

使用xslt将xml转换为CSV文件

[英]Transform xml using xslt to csv file

我在使用xsl:templates和xsl:call-template标签时遇到问题。 也许是缺乏了解,但这就是我想要做的...

如果我有一个与“ / *”匹配的模板,并且需要从需要其他文档上下文的封闭模板中调用其他模板,那么最有效的方法是什么?

<xsl:template match="/*">

<xsl:call-template name="header">
  <xsl:with-param name="headerContext" select="./[1]"/>
</xsl:call-template>

<xsl:call-template name="body">
  <xsl:with-param name="bodyContext" select="*/*/[1]"/>
</xsl:call-template>

<xsl:template>

我在调用标头和正文模板时使用xsl:with-param,以便可以从封闭的模板中覆盖match =“ / *”,但是当我这样做时,输出会变得混乱。 如果我将对“ header”模板的调用注释掉,则主体模板可以正常工作,反之亦然,但是从主模板中调用它们(如在上例中看到的那样)会使它们的行为异常。 页眉和正文模板需要对文档的不同部分进行选择,这就是为什么我选择使用w0th-param的原因,但我认为它甚至都没有用。

我应该改用apply-templates吗?

XSL被设计为基于事件的。 因此,通常,您将需要使用模板匹配,而不是显式指定要处理的后代。

<!-- Identity Template will copy every node to the output. -->
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<!-- You listed ./[1] as your xpath, but you might want to add more information 
   to make it more specific.  i.e. element names, not just * and position. -->
<xsl:template match="/*/header">
   <someOutputHeader><xsl:apply-templates /></someOutputHeader>
</xsl:template>

<xsl:template match="/something/*/body">
   <newBody><xsl:apply-templates /></newBody>
</xsl:template>

另外,最好在谓词之前指定nodeTest。 因此,例如,您可以在斜杠后指定*,而不是写“ ./[1]”。 “ ./*[1]”您也不需要使用“ ./”。 xpath暗示了这一点。 所以真的是“ * [1]”

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM