[英]XSLT to remove timestamps from all elements except one
我有这个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可以正常工作,但是如果添加了名称空间,则不会生成预期的结果。 感谢任何帮助!
您不能在没有前缀的xsl中使用名称空间,请使用xmlns:my="namespace"
,然后在所有对元素名称的引用前加上my:
-尽管我看不到任何名称,但是您还必须更改所有名称您的name()
在您的xpaths中到local-name()
若要将时间戳记保留在EventDate,请替换
and not(ancestor-or-self::(my:)EventDate)
通过
and local-name() != 'EventDate'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.