这是一个问题--在父元素和子元素中转换日期格式顺序()。原始XSLT代码可以正常工作,但只有在代码中没有其他模板时才能工作。当我添加其他模板时,也引用了该元素--,然后父元素的date拒绝转换(子元素仍然在转换它的日期)。
有什么更好的改进\更改:
这是密码。
Part I-“好的时候”
来源:
<root>
<parent attr-1="1000" attr-2="abc" DateAttributeParent="10.11.2017">
<child attr-3="whatever" attr-4="whatever2" DateAttributeChild="09.12.2010"> </child>
</parent>
<parent attr-1="2222" attr-2="abc" DateAttributeParent="04.06.2016">
<child attr-3="whatever" attr-4="whatever2" DateAttributeChild="02.06.2009"> </child>
</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>
<xsl:template match="@DateAttributeParent | @DateAttributeChild">
<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>
</xsl:stylesheet>输出
<?xml version="1.0" encoding="utf-16"?>
<root>
<parent attr-1="1000" attr-2="abc" DateAttributeParent="2017.11.10">
<child attr-3="whatever" attr-4="whatever2" DateAttributeChild="2010.12.09"> </child>
</parent>
<parent attr-1="2222" attr-2="abc" DateAttributeParent="2016.06.04">
<child attr-3="whatever" attr-4="whatever2" DateAttributeChild="2009.06.02"> </child>
</parent>
</root>-------------------------------------
Part II -当它不是时
源
<root>
<parent attr-1="1000" attr-2="abc" DateAttributeParent="10.11.2017">
<child attr-3="whatever" attr-4="whatever2" DateAttributeChild="09.12.2010"> </child>
</parent>
<parent attr-1="2222" attr-2="abc" DateAttributeParent="04.06.2016">
<child attr-3="whatever" attr-4="whatever2" DateAttributeChild="02.06.2009"> </child>
</parent>
</root>XSLT转换2
<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">
<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>
<!-- an addition to the original code starts here -->
<!-- parent additional template -->
<xsl:template match="parent">
<parent
A="foo"
B="bar" >
<xsl:copy-of select="@*" /> <!--self-copying -->
<xsl:apply-templates/>
</parent>
</xsl:template>
<!-- child additional template -->
<xsl:template match="child">
<child D="some data">
<xsl:for-each select="*">
<xsl:attribute name="{name()}">
<xsl:value-of select="text()"/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="@*"/>
</child>
</xsl:template>
</xsl:stylesheet>输出- 2
<?xml version="1.0" encoding="utf-16"?>
<root>
<parent A="foo" B="bar" attr-1="1000" attr-2="abc" DateAttributeParent="10.11.2017">
<child D="some data" attr-3="whatever" attr-4="whatever2" DateAttributeChild="2010.12.09" />
</parent>
<parent A="foo" B="bar" attr-1="2222" attr-2="abc" DateAttributeParent="04.06.2016">
<child D="some data" attr-3="whatever" attr-4="whatever2" DateAttributeChild="2009.06.02" />
</parent>
</root>在这里,您可以看到添加了一些属性(这并不重要),重要的是父元素中的日期没有转换。
发布于 2019-12-05 19:55:14
而不是:
<xsl:copy-of select="@*" />
<xsl:apply-templates/>尝试:
<xsl:apply-templates select="@*|node()"/>https://stackoverflow.com/questions/59202347
复制相似问题