简体   繁体   中英

XSLT to remove timestamps from all elements except one

I have this XSLT to remove timestamps & convert attributes into elements:

<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 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

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. 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()

Edit

To keep the timestamp at the EventDate, replace

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

by

and local-name() != 'EventDate'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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