使用 xslt 将 xml 转换为 csv 文件

发布于 2024-12-14 19:20:36 字数 706 浏览 3 评论 0原文

我遇到了 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="/*" ,但是当我这样做时,输出会变得混乱。如果我注释掉对“标题”模板的调用,则正文模板可以正常工作,反之亦然,但是从主模板调用这两个模板(如上例所示)会使它们表现奇怪。标题和正文模板需要选择文档的不同部分,这就是我选择使用 w0th-param 的原因,但我认为它不起作用。

我应该使用 apply-templates 吗?

I'm having an issue with xsl:templates and xsl:call-template tags. Perhaps it's a lack of understand , but here's what I'm trying to do...

If I have a template that's matching on "/*", and I need to call other templates from within the enclosing template that require other document contexts, what is the most efficient method of doing this?

<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>

I'm using xsl:with-param when calling the header and body templates so that I can override the match="/*" from the enclosing template, but when I do this the output gets messed up. If I comment out the call to the "header" template, the body template works properly, and vicee versa, but calling both from the main template, as you see in the above example, makes them behave strangely. The header and body templates require a selection to different parts of the document, that's why I chose to use w0th-param, but I don't think it's even working.

Should I be using apply-templates instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2024-12-21 19:20:36

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]”

XSL was designed to be event-based. So, typically, you'll want to use template matching more than explicitly specifying which descendants to process.

<!-- 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>

Also, it's good practice to specify a nodeTest before a predicate. So, for example, instead of writing "./[1]" you could specify * after the slash. "./*[1]" You also don't need to use "./" either. It's implied by xpath. So really, it's "*[1]"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文