XSLT从除一个元素之外的所有元素中删除时间戳

时间:2016-06-07 08:38:53

标签: xml xslt

我有这个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>

&安培;我的源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>

我想从除 EventDate

之外的所有日期元素中删除时间戳

预期产出:

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

如果我从源XML中删除命名空间,则此XSL工作正常,但如果添加了命名空间,则它不会生成预期结果。 感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您不能在没有前缀的xsl中使用命名空间,使用xmlns:my="namespace"然后使用my:为您的元素名称的所有引用添加前缀 - 虽然我没有看到任何,但是你还必须将您xpath中的{strong>全部 name()更改为local-name()

修改

要将时间戳保留在EventDate,请替换

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

通过

and local-name() != 'EventDate'
相关问题