【发布时间】:2021-03-29 04:40:45
【问题描述】:
我正在努力让这个名为 XSLT 的可憎之物工作。我需要在 EXACT 路径中获取一个 EXACT 属性,将其原始值传递给模板,然后用模板的结果重写该值。
我有一个这样的文件:
<?xml version="1.0" encoding="windows-1251"?>
<File>
<Document ReportYear="17">
...
...
</Document>
</File>
所以我做了一个这样的 XSLT:
<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" encoding="windows-1251" indent="yes" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template name="formatYear">
<xsl:param name="year" />
<xsl:value-of select="$year + 2000" />
</xsl:template>
<xsl:template match="File/Document">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:attribute name="ReportYear">
<xsl:call-template name="formatYear">
<xsl:with-param name="year" select="@ReportYear" />
</xsl:call-template>
</xsl:attribute>
</xsl:copy>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
这很好用,只是它会立即关闭 <Document> 标记并将其内容紧跟其后。
另外,我可以在不重复两次的情况下处理ReportYear 属性值吗?我试过current(),但没用。
【问题讨论】: