我有點卡住了,也許有人可以幫我一把。
所以我有一個 xml,我想計算日期范圍內的那些歌曲。我試過翻譯日期或子串它,但它仍然不計算它。
我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<albums>
<ALBUM>
<closingDate>
<DATE date="2021-10-09"/>
</closingDate>
<songsinalbum>
<SONGSINALBUM>
<song>
<SONG>
<songStatus>
<ESP_SONGSTATUS name="Prehistoric"/>
</songStatus>
<SONGType>
<ESP_SONGTYPE type="metal"/>
</SONGType>
<Date>
<ESP_DATE date="2021-10-09"/>
</Date>
</SONG>
<SONG>
<songStatus>
<ESP_SONGSTATUS name="Prehistoric"/>
</songStatus>
<SONGType>
<ESP_SONGTYPE type="metal"/>
</SONGType>
<Date>
<ESP_DATE date="2022-01-09"/>
</Date>
</SONG>
<SONG>
<songStatus>
<ESP_SONGSTATUS name="Expired"/>
</songStatus>
<SONGType>
<ESP_SONGTYPE type="metal"/>
</SONGType>
<Date/>
</SONG>
<SONG>
<songStatus>
<ESP_SONGSTATUS name="Available"/>
</songStatus>
<SONGType>
<ESP_SONGTYPE type="metal"/>
</SONGType>
<Date>
<ESP_DATE date="2022-03-20"/>
</Date>
</SONG>
</song>
</SONGSINALBUM>
</songsinalbum>
</ALBUM>
</albums>
我的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Workbook>
<Styles>
<Style ss:ID="mydefault" ss:Name="mydefault">
<Font ss:FontName="Verdana" ss:Bold="0"/>
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
</Style>
</Styles>
<Worksheet ss:Name="Report">
<xsl:call-template name="ws_table"/>
</Worksheet>
</Workbook>
</xsl:template>
<xsl:template name="ws_table">
<Table>
<xsl:call-template name="headerrow"/>
<xsl:apply-templates select="albums/ALBUM" mode="datarow"/>
</Table>
</xsl:template>
<xsl:template name="headerrow">
<Row>
<!-- Date -->
<Cell>
<Data ss:Type="String">Date</Data>
</Cell>
<!-- test counting -->
<Cell>
<Data ss:Type="String">test counting</Data>
</Cell>
<!-- real count -->
<Cell>
<Data ss:Type="String">real count</Data>
</Cell>
</Row>
</xsl:template>
<xsl:template match="ALBUM" mode="datarow">
<xsl:variable name="InRange" select="translate(songsinalbum/SONGSINALBUM/song/SONG[songStatus/ESP_SONGSTATUS/@name='Prehistoric'][SONGType/ESP_SONGTYPE/@type='metal']/Date/ESP_DATE/@date,'-','') <= translate(closingDate/DATE/@date,'-','')"/>
<xsl:variable name="formateddate" select="number(concat
(substring(closingDate/DATE/@date, 1, 4),
substring(closingDate/DATE/@date, 6, 2),
substring(closingDate/DATE/@date, 9, 2)))
<=
number(concat
(substring(songsinalbum/SONGSINALBUM/song/SONG[songStatus/ESP_SONGSTATUS/@name='Prehistoric'][SONGType/ESP_SONGTYPE/@type='metal']/Date/ESP_DATE/@date, 1, 4),
substring(songsinalbum/SONGSINALBUM/song/SONG[songStatus/ESP_SONGSTATUS/@name='Prehistoric'][SONGType/ESP_SONGTYPE/@type='metal']/Date/ESP_DATE/@date, 6, 2),
substring(songsinalbum/SONGSINALBUM/song/SONG[songStatus/ESP_SONGSTATUS/@name='Prehistoric'][SONGType/ESP_SONGTYPE/@type='metal']/Date/ESP_DATE/@date, 9, 2)))" />
<Row>
<!-- Date -->
<Cell>
<Data ss:Type="String">
<xsl:value-of select="closingDate/DATE/@date"/>
</Data>
</Cell>
<!-- test counting -->
<Cell>
<Data ss:Type="String">
<xsl:value-of select="count(songsinalbum/SONGSINALBUM/song/SONG[songStatus/ESP_SONGSTATUS/@name='Prehistoric'][SONGType/ESP_SONGTYPE/@type='metal'])"/>
</Data>
</Cell>
<!-- songs what is -->
<Cell>
<Data ss:Type="String">
<xsl:value-of select="count($formateddate)"/>
</Data>
</Cell>
</Row>
</xsl:template>
</xsl:stylesheet>
輸出應該是這樣的: 在此處輸入影像描述
和ofc。截止日期可以更改,所以如果像“2022-01-09”,那么“實際計數”值應該是“2”。
那么我做錯了什么?
謝謝,問候
uj5u.com熱心網友回復:
考慮以下最小化的示例:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/albums">
<results>
<xsl:for-each select="ALBUM">
<xsl:variable name="close-date" select="translate(closingDate/DATE/@date, '-', '')" />
<xsl:variable name="songs" select="songsinalbum/SONGSINALBUM/song/SONG" />
<xsl:variable name="songs-before" select="$songs[translate(Date/ESP_DATE/@date, '-', '') <= $close-date]" />
<songs>
<xsl:value-of select="count($songs)" />
</songs>
<songs-before>
<xsl:value-of select="count($songs-before)" />
</songs-before>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
當應用于您的 XML 示例輸入時,結果將是:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<songs>4</songs>
<songs-before>1</songs-before>
</results>
如果您將截止日期更改為,2022-01-09則結果將是:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<songs>4</songs>
<songs-before>2</songs-before>
</results>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452398.html
上一篇:如何跟蹤從模板到不同模板的位置?
