2016-06-07 102 views
0

我有这样的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> 

我想从以外的所有日期元素中移除时间戳事件日期

预期输出:

<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

不能使用的命名空间中的XSL没有前缀,使用xmlns:my="namespace"然后你前缀的元素名称的所有引用与my: - 虽然我看不出有什么,但你也必须改变所有name() S IN您的XPath来local-name()

编辑

为了保持时间戳在EVENTDATE,更换

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

and local-name() != 'EventDate' 
+0

嗨Stefan,感谢您的回复,但我没有在我的xml中使用前缀。我试着根据你的建议改变XSLT中的命名空间声明&** name()**,但是我没有看到输出的任何改变。 –

+0

我看到了,请检查我的编辑请 –

+0

它的工作完美。非常感谢 ! –