我對 XSLT 很陌生。我在我的一個場景中使用 XSLT 2.0 隧道引數 - 我需要獲取組中 <last_mod_dt_TS> 的第一個值,并放入 <request_Id> 相同的所有節點。
下面是我正在使用的示例 XML -
<?xml version='1.0' encoding='UTF-8'?>
<root>
<row>
<request_Id>4007</request_Id>
<req_tp>Action</req_tp>
<last_mod_dt_TS>2021-09-07T07:38:11.000</last_mod_dt_TS>
</row>
<row>
<request_Id>4007</request_Id>
<req_tp>Action</req_tp>
<last_mod_dt_TS>2021-10-10T17:32:44.000</last_mod_dt_TS>
</row>
</root>
XSLT 如下 -
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#all">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="row" group-by="request_Id">
<xsl:apply-templates select="current-group()">
<xsl:with-param name="slaStart" select="current-group()[1]/last_mod_dt_TS" tunnel="yes"/>
</xsl:apply-templates>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="last_mod_dt_TS">
<xsl:param name="slaStart" tunnel="yes"/>
<xsl:copy>
<xsl:value-of select="slaStart"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我的預期輸出 -
<?xml version='1.0' encoding='UTF-8'?>
<root>
<row>
<request_Id>4007</request_Id>
<req_tp>Action</req_tp>
<last_mod_dt_TS>2021-09-07T07:38:11.000</last_mod_dt_TS>
</row>
<row>
<request_Id>4007</request_Id>
<req_tp>Action</req_tp>
<last_mod_dt_TS>2021-09-07T07:38:11.000</last_mod_dt_TS>
</row>
</root>
使用我的 XSLT -
<root>
<row>
<request_Id>4007</request_Id>
<req_tp>Action</req_tp>
<last_mod_dt_TS/>
</row>
<row>
<request_Id>4007</request_Id>
<req_tp>Action</req_tp>
<last_mod_dt_TS/>
</row>
</root>
我不確定為什么 XSLT 沒有按預期作業。任何幫助深表感謝。謝謝你。
uj5u.com熱心網友回復:
使用<xsl:value-of select="$slaStart"/>以輸出和參考變數值。
uj5u.com熱心網友回復:
請注意,您根本不需要引數和隧道。在current group和current grouping key通過的通話原樣傳遞xsl:apply-templates-所以你可以簡單地做:
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each-group select="row" group-by="request_Id">
<xsl:apply-templates select="current-group()"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
進而:
<xsl:template match="last_mod_dt_TS">
<xsl:copy>
<xsl:value-of select="current-group()[1]/last_mod_dt_TS"/>
</xsl:copy>
</xsl:template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/316697.html
