简体   繁体   English

XSLT从所有元素中除去时间戳记

[英]XSLT to remove timestamps from all elements except one

I have this XSLT to remove timestamps & convert attributes into elements: 我有这个XSLT可以删除时间戳并将属性转换为元素:

<xsl:stylesheet version="1.0" xmlns="namespace" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="*[@*]">
<xsl:choose>
<xsl:when test="text() and @*">
<xsl:copy>
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()"/>
</xsl:element>
<xsl:apply-templates select="@*"/>
</xsl:copy>
</xsl:when>
<xsl:when test="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

<xsl:template match="//*[contains(name(), 'Date') and not(ancestor-or-self::EventDate)]">
<xsl:element name="{local-name()}">
<xsl:value-of select="substring(.,1,10)"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

& my source XML looks like this: 和我的源XML看起来像这样:

<?xml version="1.0" encoding="ISO-8859-1" standalone='yes'?>
<CaseEvent xmlns="namespace">
<TransactionInfo>
<EventDate>2016-06-02T02:07:30.000-04:00</EventDate>
<CaseID>872519</CaseID>
</TransactionInfo>
<NewCaseEvent>
<Case changeType="I">           
    <Plan changeType="I">
    <Provision>
          <ProvisionTextValue>70</ProvisionTextValue>
          <EffectiveDate>2016-01-01-05:00</EffectiveDate>
        </Provision>
    </Plan>    
    <BillGroup changeType="I">
      <BillGroupIdentifierDate>2016-01-01-05:00</BillGroupIdentifierDate>
      </BillGroup>     
  </Case>
  </NewCaseEvent>
 </CaseEvent>

I want to remove timestamp from all Date Elements except EventDate 我想从EventDate之外的所有日期元素中删除时间戳

Expected Output: 预期产量:

    <CaseEvent  xmlns="namespace">
<TransactionInfo>
    <EventDate>2016-06-02T02:07:30.000-04:00</EventDate>
    <CaseID>872519</CaseID>
</TransactionInfo>
<NewCaseEvent>
    <Case changeType="I">
        <Plan changeType="I">
            <Provision>
                <ProvisionTextValue>70</ProvisionTextValue>
                <EffectiveDate>2016-01-01</EffectiveDate>
            </Provision>
        </Plan>
        <BillGroup changeType="I">
            <BillGroupIdentifierDate>2016-01-01</BillGroupIdentifierDate>
        </BillGroup>
    </Case>
</NewCaseEvent>
</CaseEvent>

If I remove namespace from source XML, this XSL is working fine, but if namespace is added then it is not generating the expected result. 如果我从源XML中删除名称空间,则此XSL可以正常工作,但是如果添加了名称空间,则不会生成预期的结果。 Appreciate any help on this! 感谢任何帮助!

You can't use the namespace in your xsl without a prefix, use xmlns:my="namespace" and then prefix all your references of your element names with my: - though I don't see any, but you must also change all your name() s in you xpaths to local-name() 您不能在没有前缀的xsl中使用名称空间,请使用xmlns:my="namespace" ,然后在所有对元素名称的引用前加上my: -尽管我看不到任何名称,但是您还必须更改所有名称您的name()在您的xpaths中到local-name()

Edit 编辑

To keep the timestamp at the EventDate, replace 若要将时间戳记保留在EventDate,请替换

and not(ancestor-or-self::(my:)EventDate)

by 通过

and local-name() != 'EventDate'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM