主要目标-还原所有日期的格式,这些日期与不同的转换方法相关联(这里有3种转换方法)。有日期的3个节点(父节点和子节点-1)中的2个可以通过提交的日期恢复模板进行更改,而最后一个节点不能(子节点-2)。
子-2 XSLT模板的结构必须保持不变(或者如果考虑到其目标的话)。此节点转换的目标是更改此节点中的属性名称,并且日期的属性名称也必须重命名(以及还原)。
如何改进XSLT代码以更改所有三个日期?使用XSLT1.0
源
<root>
<parent attr-1="some value" attr-2="abc" DateAttributeParent="10.11.2017">
<child attr-2="some value-2" DateAttributeChild-1="09.12.2010"> </child>
<child-2 attr-3="some value-3" DateAttributeChild-2="05.11.2007"> </child-2>
</parent>
</root>XSLT变换
<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"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- identity transform -->
<xsl:template match="@DateAttributeParent | @DateAttributeChild-1 | @DateAttributeChild-2">
<xsl:attribute name="{name()}">
<xsl:value-of select="substring-after(substring-after(., '.'), '.')"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="substring-before(substring-after(., '.'), '.')"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="substring-before(., '.')"/>
</xsl:attribute>
</xsl:template>
<!-- parent template -->
<xsl:template match="parent">
<parent>
<xsl:apply-templates select="@*|node()"/>
</parent>
</xsl:template>
<!-- child-1 template -->
<xsl:template match="child-1">
<child-1>
<xsl:for-each select="*">
<xsl:attribute name="{name()}" >
<xsl:value-of select="text()"/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="@*"/>
</child-1>
</xsl:template>
<!-- child-2 template -->
<xsl:template match="child-2">
<child-2>
<xsl:attribute name="attr-3renamed">
<xsl:value-of select="@attr-3"/>
</xsl:attribute>
<xsl:attribute name="NonRevertingDate">
<xsl:value-of select="@DateAttributeChild-2"/>
</xsl:attribute>
</child-2>
</xsl:template>
</xsl:stylesheet>输出
<root>
<parent attr-1="some value" attr-2="abc" DateAttributeParent="2017.11.10">
<child attr-2="some value-2" DateAttributeChild-1="2010.12.09"> </child>
<child-2 attr-3renamed="some value-3" NonRevertingDate="05.11.2007" /> <!-- didn't revert! -->
</parent>
</root>发布于 2019-12-09 13:54:05
如果要重命名DateAttributeChild-2属性,则不能使用用于其他日期属性的相同模板,因为该模板保留了原始名称:
<xsl:attribute name="{name()}">为了避免代码重复,我建议您在可以根据需要调用的命名模板中进行日期格式转换。请考虑以下示例:
XSLT1.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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- reformat some date attributes -->
<xsl:template match="@DateAttributeParent | @DateAttributeChild-1">
<xsl:attribute name="{name()}">
<xsl:call-template name="reformat-date">
<xsl:with-param name="date" select="."/>
</xsl:call-template>
</xsl:attribute>
</xsl:template>
<!-- child-2 template -->
<xsl:template match="child-2">
<xsl:copy>
<xsl:attribute name="attr-3renamed">
<xsl:value-of select="@attr-3"/>
</xsl:attribute>
<xsl:attribute name="NonRevertingDate">
<xsl:call-template name="reformat-date">
<xsl:with-param name="date" select="@DateAttributeChild-2"/>
</xsl:call-template>
</xsl:attribute>
</xsl:copy>
</xsl:template>
<xsl:template name="reformat-date">
<xsl:param name="date"/>
<xsl:value-of select="substring-after(substring-after($date, '.'), '.')"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="substring-before(substring-after($date, '.'), '.')"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="substring-before($date, '.')"/>
</xsl:template>
</xsl:stylesheet>注意:
child-1元素。。
https://stackoverflow.com/questions/59249288
复制相似问题