有人可以幫我解決一個未完成的想法嗎?
我不太習慣 xslt,但我正在嘗試,并且感謝很多互聯網幫助我相處。
但:
以下情況我無法解決:
給定一個帶有一些地質層資料的示例 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"/>
</LAYERS>
并使用以下 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:key name="adj" match="LAYER" use="generate-id(preceding-sibling::LAYER[not(@STRAT = current()/@STRAT)][1])" />
<xsl:template match="/*">
<xsl:for-each select="LAYER[generate-id() = generate-id(key('adj', generate-id(preceding-sibling::LAYER[not(@STRAT = current()/@STRAT)][1]))[1])]">
<xsl:variable name="INTVID" select="@INTV"/>
<xsl:variable name="precedingZone" select="preceding-sibling::LAYER[@INTV = $INTVID][1]"/>
<xsl:variable name="DEPTHFROM" select="$precedingZone/@DEPTHTO"/>
<xsl:if test="@STRAT != ''">
<xsl:variable name="current-group" select="key('adj', generate-id(preceding-sibling::LAYER[not(@STRAT = current()/@STRAT)][1]))" />
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="@STRAT"/>
<xsl:text>" </xsl:text>
<xsl:for-each select="$current-group">
<xsl:sort select="$DEPTHFROM" data-type="number" order="ascending"/>
<xsl:if test="position() = 1 ">
<xsl:value-of select="$DEPTHFROM"/>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:for-each select="$current-group">
<xsl:sort select="@DEPTHTO" data-type="number" order="descending"/>
<xsl:if test="position() = 1 ">
<xsl:value-of select="@DEPTHTO"/>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:value-of select="@INDEX_ZONE"/>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
結果如下所示:
ZONE "geologiscal_formation_1" 94.00
ZONE "geologiscal_formation_1" 94.20 95.00
ZONE "geologiscal_formation_2" 95.00 101.80
ZONE "geologiscal_formation_3" 101.80 103.00
ZONE "geologiscal_formation_3" 103.25 104.00
但所需的輸出應如下所示:
ZONE "geologiscal_formation_1" 0.00 95.00
ZONE "geologiscal_formation_2" 95.00 101.80
ZONE "geologiscal_formation_3" 101.80 104.00
更多背景資訊:由于第一行僅提供 DEPTHTO 資料,我假設它從 0 開始,只要不知道。分組僅部分起作用,因為我們得到了一些帶有“lost_data”的層,這將所有想法都帶入了一個糟糕的結果。
所以我需要某種條件,忽略/跳過找到“lost_data”的資料。但由于我使用“for-each”,我不知道如何做到這一點。我聽說過一些“管道處理”,但沒有深入研究,因為我已經對我得到的東西不知所措。
也許有人知道更好?
是的,我堅持使用 xslt 1.0 :)
感謝您的任何想法或幫助。
感謝答案和一些小的改動,我能夠實作我的目標:
更改如下所示:
<?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:key name="layer-by-strat" match="LAYER" use="@STRAT" />
<xsl:template match="/*">
<xsl:for-each select="LAYER[not(@EGART='Lost_Data')][count(. | key('layer-by-strat', @STRAT)[1]) = 1]">
<xsl:variable name="INTVID" select="@INTV"/>
<xsl:variable name="precedingZone" select="preceding-sibling::LAYER[@INTV = $INTVID][1]"/>
<xsl:variable name="DEPTHFROM" select="$precedingZone/@DEPTHTO"/>
<xsl:choose>
<xsl:when test="$precedingZone">
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="@STRAT" />
<xsl:text>" </xsl:text>
<xsl:value-of select="$DEPTHFROM"/>
<xsl:text> </xsl:text>
<xsl:value-of select="key('layer-by-strat', @STRAT)[last()]/@DEPTHTO" />
<xsl:text> </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="@STRAT" />
<xsl:text>" 0.00 </xsl:text>
<xsl:value-of select="key('layer-by-strat', @STRAT)[last()]/@DEPTHTO" />
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
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" use="@STRAT" />
<xsl:template match="LAYERS" >
<xsl:for-each select="LAYER[not(@EGART='Lost_Data')][count(. | key('layer-by-strat', @STRAT)[1]) = 1]">
<xsl:text>ZONE "</xsl:text>
<xsl:value-of select="@STRAT" />
<xsl:text>" 0.00 </xsl:text>
<xsl:value-of select="key('layer-by-strat', @STRAT)[last()]/@DEPTHTO" />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
這是假設LAYER每組中的最后一個具有所需的最大值 - 否則將需要進行微調。
添加:
我注意到我錯過了一部分要求,即每一層都從前一層的最大深度開始。這是調整后的樣式表:
<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" use="@STRAT" />
<xsl:template match="LAYERS" >
<xsl:call-template name="generate-rows">
<xsl:with-param name="layers" select="LAYER[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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471890.html
