即使有幾乎相同的問題,我也沒有得到不適合我的結果。
這個想法可能很簡單,但我不太了解后臺的所有程序來解決這個問題。
我有多個模板具有相同的匹配顯示不同的結果。
從以下節點提取的資料集可能如下所示:
<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>
我正在嘗試使用多個模板獲取文本輸出。
想要的結果應該看起來像使用 3 個模板:
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
我在 kinda-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>
</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>
</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>
</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
我確信還有其他方法可以完成結果,但關鍵問題是如何管理多個模板以獲得這樣的結果?
提前感謝您的任何幫助:)
uj5u.com熱心網友回復:
如果您確實想為同一個節點呼叫多個模板,答案是使用模式:
<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"/>
對于您的特定示例,這似乎過度設計,但也許您的實際任務更復雜。
uj5u.com熱心網友回復:
這將做你想要的。
<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>
</xsl:text>
</xsl:template>
<xsl:template match="@PETRO">
<xsl:text>Petro:: </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="@STRAT">
<xsl:text>Strat: </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471882.html
