gpt4 book ai didi

xml - 在同一级别为同一 XML 元素呈现不同的模板

转载 作者:数据小太阳 更新时间:2023-10-29 02:49:19 25 4
gpt4 key购买 nike

XML:

<Root>
<Elements>
<Element>el1</Element>
<Element>el2</Element>
</Elements>

<Elements>
<Element>el1</Element>
<Element>el2</Element>
</Elements>
</Root>

尝试生成为同一元素应用两个不同的模板。

主模板:

<xsl:stylesheet version="1.0">
<xsl:template match="/Root">
At root level
<xsl:apply-templates select="Elements">

<h1>Render something more</h1>

<xsl:apply-templates select="Elements" mode="1:Custom">
</xsl:template>


<!-- This doesn't render though it is called above-->
<xsl:template match="Elements">
render something here
</xsl:template>

<!-- This renders twice -->
<xsl:template match="Elements" mode="1:Custom">
render something else here
</xsl:template>
</xsl:stylesheet>

如果我将模式添加到第一个模板,两者都不会呈现。

还试过:

 <xsl:apply-templates select="Elements" mode="1:Custom" />

使用不同的模板应用为:

<xsl:apply-templates select="Elements" mode="Different" />

只有两者之一(渲染第一个具有指定模式的)。即

<xsl:template match="Elements">
</xsl:template>

不渲染

或者 <xsl:template match="Elements" mode="Different" />渲染两次。

我应该如何解决这个问题?在我研究的每个地方,它都建议优先考虑模式。这么多程序员使用它,一定是简单的东西吧?

最佳答案

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/Root">
At root level
<xsl:apply-templates select="Elements"/>

<h1>After first template</h1>

<xsl:apply-templates select="Elements" mode="Custom"/>
</xsl:template>

<xsl:template match="Elements">
<p>First template</p>
<xsl:apply-templates select="Element"/>
</xsl:template>

<xsl:template match="Elements" mode="Custom">
<p>Second template </p>
</xsl:template>
</xsl:stylesheet>

关于xml - 在同一级别为同一 XML 元素呈现不同的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6308807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com