XSLT样式表中的多个模板具有相同的匹配?

  • 本文关键字:样式 XSLT xml xslt xslt-1.0
  • 更新时间 :
  • 英文 :


即使有几乎相同的问题,我也得不到我想要的结果。

这个想法可能很简单,但我不太了解后台的所有过程来解决这个问题。

我得到了多个模板,相同的匹配显示不同的结果。

从以下节点提取的数据集可能如下所示:

<LAYERS> 
<LAYER DEPTHTO="93.63" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="94.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="95.00" PETRO="Gravel" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="100.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="100.50" PETRO="Mud" STRAT="geologiscal_formation_1" INTV="1"/>
</LAYERS>  

和我试图得到一个文本输出使用多个模板。

想要的结果应该看起来像使用三个模板:

Depth_to: 93.63
Depth_to: 94.00
Depth_to: 95.00
Depth_to: 100.00
Depth_to: 100.50
Petro:: Sand
Petro:: Sand
Petro:: Gravel
Petro:: Sand
Petro:: Mud
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1

我在kind -pseudo-xslt中的想法是这样的,试图将所有模板收集在顶部(去掉注释),同时将模板放在底部的块中:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes=" xml xsl xs">
<xsl:output method="text" version="1.0" indent="yes" />

<!--
<xsl:template name="all_data" >
<xsl:use-template name="path" />
<xsl:use-template name="petro" />
<xsl:use-template name="strat" />
</xsl:template>
-->

<xsl:template name="path" match="/" >
<xsl:for-each select="LAYERS/LAYER">
<xsl:text>Depth_to: </xsl:text>
<xsl:value-of select="@DEPTHTO"/>
<xsl:text>&#xA;</xsl:text>
</xsl:for-each>
</xsl:template>

<xsl:template name="petro" match="/" >
<xsl:for-each select="LAYERS/LAYER">
<xsl:text>Petro:: </xsl:text>
<xsl:value-of select="@PETRO"/>
<xsl:text>&#xA;</xsl:text>
</xsl:for-each>
</xsl:template>

<xsl:template name="strat" match="/" >
<xsl:for-each select="LAYERS/LAYER">
<xsl:text>Strat: </xsl:text>
<xsl:value-of select="@STRAT"/>
<xsl:text>&#xA;</xsl:text>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

但是这只给了我一个模板的结果:

Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1
Strat: geologiscal_formation_1

我相信还有其他的方法来完成结果,但关键的问题是如何管理多个模板的结果像这样?

提前感谢你的帮助:)

如果您想为同一个节点调用多个模板,答案是使用mode:

<xsl:template match="/" mode="Depth">
...
</xsl:template>
<xsl:template match="/" mode="Petro">
...
</xsl:template>
<xsl:template match="/" mode="Strat">
...
</xsl:template>
...
<xsl:apply-templates select="." mode="Depth"/>
<xsl:apply-templates select="." mode="Petro"/>
<xsl:apply-templates select="." mode="Strat"/>

对于您的特定示例,这似乎是过度设计的,但也许您的实际任务更复杂。

这是你想要的。

<xsl:template match="LAYERS">
<xsl:apply-templates select="LAYER/@DEPTHTO"/>
<xsl:apply-templates select="LAYER/@PETRO"/>
<xsl:apply-templates select="LAYER/@STRAT"/>
</xsl:template>
<xsl:template match="@DEPTHTO">
<xsl:text>Depth_to: </xsl:text>
<xsl:value-of select="."/>
<xsl:text>&#xA;</xsl:text>
</xsl:template>
<xsl:template match="@PETRO">
<xsl:text>Petro:: </xsl:text>
<xsl:value-of select="."/>
<xsl:text>&#xA;</xsl:text>
</xsl:template>
<xsl:template match="@STRAT">
<xsl:text>Strat: </xsl:text>
<xsl:value-of select="."/>
<xsl:text>&#xA;</xsl:text>
</xsl:template>

最新更新