我已經有一個類似的問題,但那個問題有點復雜。
我正在嘗試使用 xslt 從 XML 中獲取一些地質層資料的文本結果。
我得到的 XML 示例可能如下所示:
<LAYERS>
<LAYER DEPTHTO="1.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="94.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="94.20" INTV="1" INDEX_ZONE="-1" EGART="Lost_Data"/>
<LAYER DEPTHTO="95.00" PETRO="Gravel" STRAT="geologiscal_formation_1" INTV="1"/>
<LAYER DEPTHTO="100.00" PETRO="Sand" STRAT="geologiscal_formation_2" INTV="1"/>
<LAYER DEPTHTO="100.50" PETRO="Mud" STRAT="geologiscal_formation_2" INTV="1"/>
<LAYER DEPTHTO="101.50" PETRO="Sand" STRAT="geologiscal_formation_2" INTV="1"/>
<LAYER DEPTHTO="101.80" PETRO="Mud" STRAT="geologiscal_formation_2" INTV="1"/>
<LAYER DEPTHTO="102.90" PETRO="Mud" STRAT="geologiscal_formation_3" INTV="1"/>
<LAYER DEPTHTO="103.00" PETRO="Sand" STRAT="geologiscal_formation_3" INTV="1"/>
<LAYER DEPTHTO="103.25" INTV="1" INDEX_ZONE="-1" EGART="Lost_Data"/>
<LAYER DEPTHTO="103.69" PETRO="Sand" STRAT="geologiscal_formation_3" INTV="1"/>
<LAYER DEPTHTO="104.00" PETRO="Mud" STRAT="geologiscal_formation_3" INTV="1"/>
<LAYER DEPTHTO="1.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="2"/>
<LAYER DEPTHTO="94.00" PETRO="Sand" STRAT="geologiscal_formation_1" INTV="2"/>
<LAYER DEPTHTO="94.20" INTV="2" INDEX_ZONE="-1" EGART="Lost_Data"/>
<LAYER DEPTHTO="95.00" PETRO="Gravel" STRAT="geologiscal_formation_2" INTV="2"/>
<LAYER DEPTHTO="100.00" PETRO="Sand" STRAT="geologiscal_formation_2" INTV="2"/>
<LAYER DEPTHTO="100.50" PETRO="Mud" STRAT="geologiscal_formation_4" INTV="2"/>
<LAYER DEPTHTO="101.50" PETRO="Sand" STRAT="geologiscal_formation_4" INTV="2"/>
<LAYER DEPTHTO="101.80" PETRO="Mud" STRAT="geologiscal_formation_5" INTV="2"/>
<LAYER DEPTHTO="102.90" PETRO="Mud" STRAT="geologiscal_formation_3" INTV="2"/>
<LAYER DEPTHTO="103.00" PETRO="Sand" STRAT="geologiscal_formation_3" INTV="2"/>
<LAYER DEPTHTO="103.25" INTV="2" INDEX_ZONE="-1" EGART="Lost_Data"/>
<LAYER DEPTHTO="103.69" PETRO="Sand" STRAT="geologiscal_formation_2" INTV="2"/>
<LAYER DEPTHTO="104.00" PETRO="Mud" STRAT="geologiscal_formation_2" INTV="2"/>
</LAYERS>
它就像 2 層描述,僅在 INTV 屬性值上有所不同。
我正在尋找一種按特定 INTV 的屬性 STRAT 進行分組的方法,結果應該如下所示(假設來自 INTV= 1 資料集):
ZONE "geologiscal_formation_1" 0.00 95.00
ZONE "geologiscal_formation_2" 95.00 101.80
ZONE "geologiscal_formation_3" 101.80 104.00
棘手的部分是“忽略”分組中帶有 EGART="Lost_Data" 的資料。
既然這個問題在這里得到了部分回答: XSLT grouping Brothers with conditions?
但我在我的例子中犯了一個錯誤,我為那個新的scanrio做了一個新的問題。
我希望我的想法得到通過,我會繼續尋找。
感謝所有幫助。
uj5u.com熱心網友回復:
要獲得您顯示的結果,您可以對上一個問題的答案進行相當簡單的調整:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:key name="layer-by-strat" match="LAYER[@INTV=1]" use="@STRAT" />
<xsl:template match="LAYERS" >
<xsl:call-template name="generate-rows">
<xsl:with-param name="layers" select="LAYER[@INTV=1 and not(@EGART='Lost_Data')][count(. | key('layer-by-strat', @STRAT)[1]) = 1]"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="generate-rows">
<xsl:param name="layers" select="/.."/>
<xsl:param name="accumulated-depth" select="'0.00'"/>
<xsl:if test="$layers">
<xsl:variable name="strat" select="$layers[1]/@STRAT" />
<xsl:variable name="max-depth" select="key('layer-by-strat', $strat)[last()]/@DEPTHTO" />
<!-- output -->
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="$strat" />
<xsl:text>" </xsl:text>
<xsl:value-of select="$accumulated-depth" />
<xsl:text> </xsl:text>
<xsl:value-of select="$max-depth" />
<xsl:text> </xsl:text>
<!-- recursive call -->
<xsl:call-template name="generate-rows">
<xsl:with-param name="layers" select="$layers[position() > 1]"/>
<xsl:with-param name="accumulated-depth" select="$max-depth"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
請注意,我們在這里假設 a LAYERwith@EGART='Lost_Data'沒有STRAT屬性 - 因此可以在鍵定義中跳過此條件。
--
PS 看來您可能希望引數化選擇INTV- 但是,這是不可能的(至少如果您想使用 Muenchian 分組方法則不能),因為在 XSLT 1.0 中,您不允許在匹配模式中使用變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471888.html
上一篇:移動XML不同的元素
