如何解决如何在处理指令中查找更新日期 - XSLT
如果在lastUpdatedOn
元素中找到处理指令并创建值p
,我尝试在p
元素中创建属性updated date
。
输入 XML
<root>
<p content-type="new">Ongoing technological and <?lastUpdatedOn 20/01/2021?>privacy developments present practicing <?lastUpdatedOn 27/01/2021?>attorneys with significant challenges in the field of information privacy and <?lastUpdatedOn 25/01/2020?>security.</p>
<p content-type="new">Developments present <?lastUpdatedOn 26/01/2021?>practicing.</p>
<p content-type="new">Practicing the Labs.</p>
</root>
预期产出
<root>
<p content-type="new" lastupdatedon="27/01/2021">Ongoing technological and <?lastUpdatedOn 20/01/2021?>privacy developments present practicing <?lastUpdatedOn 27/01/2021?>attorneys with significant challenges in the field of information privacy and <?lastUpdatedOn 25/01/2020?>security.</p>
<p content-type="new" lastupdatedon="26/01/2021">Developments present <?lastUpdatedOn 26/01/2021?>practicing.</p>
<p content-type="new">Practicing the Labs.</p>
</root>
XSLT 代码
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:if test="processing-instruction(lastUpdatedOn)">
<xsl:attribute name="lastupdatedon">
<xsl:value-of select="processing-instruction(lastUpdatedOn)"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
解决方法
正确的模式或测试是 processing-instruction('lastUpdatedOn')
。我会把它塞进 p
的匹配中,例如
<xsl:template match="p[processing-instruction('lastUpdatedOn')]">
<xsl:copy>
<xsl:apply-templates select="@*,processing-instruction('lastUpdatedOn'),node() except processing-instruction('lastUpdatedOn')"/>
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('lastUpdatedOn')">
<xsl:attribute select="{name()}" select="."/>
</xsl:template>
其余的可以通过身份转换处理,例如<xsl:mode on-no-match="shallow-copy"/>
。
因为您似乎有多个处理指令对它们进行排序
<xsl:template match="p[processing-instruction('lastUpdatedOn')]">
<xsl:copy>
<xsl:apply-templates select="@*,sort(processing-instruction('lastUpdatedOn'),(),function($p) { xs:date(replace($p,'([0-9]{2})/([0-9]{2})/([0-9]{4})','$3-$2-$1')) })[last()],node() except processing-instruction('lastUpdatedOn')"/>
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('lastUpdatedOn')">
<xsl:attribute name="lastupdatedon" select="."/>
</xsl:template>
可以帮助仅显示最后日期。高阶 fn:sort
在 Saxon PE 和 EE 中至少从 9.8 开始可用,在 Saxon HE 中用于 10 及更高版本。
以上将日期移动到属性,但从 p
中删除处理指令,如果您可能需要保留它们
<xsl:template match="p[processing-instruction('lastUpdatedOn')]">
<xsl:copy>
<xsl:apply-templates select="@*,sort(processing-instruction('lastUpdatedOn')!xs:date(replace(.,'$3-$2-$1')))[last()],node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match=".[. instance of xs:date]">
<xsl:attribute name="lastupdatedon" select="."/>
</xsl:template>
是更好的方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。