首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同模板方法中的反向日期格式

不同模板方法中的反向日期格式
EN

Stack Overflow用户
提问于 2019-12-09 12:50:38
回答 1查看 41关注 0票数 0

主要目标-还原所有日期的格式,这些日期与不同的转换方法相关联(这里有3种转换方法)。有日期的3个节点(父节点和子节点-1)中的2个可以通过提交的日期恢复模板进行更改,而最后一个节点不能(子节点-2)。

子-2 XSLT模板的结构必须保持不变(或者如果考虑到其目标的话)。此节点转换的目标是更改此节点中的属性名称,并且日期的属性名称也必须重命名(以及还原)。

如何改进XSLT代码以更改所有三个日期?使用XSLT1.0

代码语言:javascript
复制
<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变换

代码语言:javascript
复制
<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>

输出

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-09 13:54:05

如果要重命名DateAttributeChild-2属性,则不能使用用于其他日期属性的相同模板,因为该模板保留了原始名称:

代码语言:javascript
复制
<xsl:attribute name="{name()}">

为了避免代码重复,我建议您在可以根据需要调用的命名模板中进行日期格式转换。请考虑以下示例:

XSLT1.0

代码语言:javascript
复制
<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>

注意:

  • 我删除了您的“父模板”,因为它没有做任何身份转换模板没有做的事情。
  • 我删除了您的“子-1模板”,因为在输入中没有看到child-1元素。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59249288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档