我是XSL地图的新手。我想替换不正确时区的所有实例。我知道这可能发生的字段的名称。在某些情况下,时区是正确的,所以我只想用+00:00替换+01:00。
这是我的输入看起来像(粗略):
<WORKORDERSet>
<WORKORDER>
<CLIENT_WORKORDER>
<COMPLETION_SLA_DATE>2017-07-14T10:11:20+01:00</COMPLETION_SLA_DATE>
<REGION>100000</REGION>
<RESPONSE_SLA_DATE>2017-07-12T10:11:20+01:00</RESPONSE_SLA_DATE>
<WO_NUM>W1000669</WO_NUM>
</CLIENT_WORKORDER>
</WORKORDER>
<WORKORDER>
<CLIENT_WORKORDER>
<COMPLETION_SLA_DATE>2017-12-14T10:11:20+01:00</COMPLETION_SLA_DATE>
<REGION>100000</REGION>
<RESPONSE_SLA_DATE>2017-12-12T10:12:20+01:00</RESPONSE_SLA_DATE>
<WO_NUM>W1000420</WO_NUM>
</CLIENT_WORKORDER>
</WORKORDER>
</WORKORDERSet>
这就是我希望我的输出看起来像:
<WORKORDERSet>
<WORKORDER>
<CLIENT_WORKORDER>
<COMPLETION_SLA_DATE>2017-07-14T10:11:20+00:00</COMPLETION_SLA_DATE>
<REGION>100000</REGION>
<RESPONSE_SLA_DATE>2017-07-12T10:11:20+00:00</RESPONSE_SLA_DATE>
<WO_NUM>W1000669</WO_NUM>
</CLIENT_WORKORDER>
</WORKORDER>
<WORKORDER>
<CLIENT_WORKORDER>
<COMPLETION_SLA_DATE>2017-12-14T10:11:20+00:00</COMPLETION_SLA_DATE>
<REGION>100000</REGION>
<RESPONSE_SLA_DATE>2017-12-12T10:12:20+00:00</RESPONSE_SLA_DATE>
<WO_NUM>W1000420</WO_NUM>
</CLIENT_WORKORDER>
</WORKORDER>
</WORKORDERSet>
这是我尝试使用的XSL,但我不知道我做错了什么:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:max="http://www.ibm.com/maximo" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- identity template: copy everything 1:1 -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!--More specific template for Node766 that provides custom behavior -->
<xsl:template match="WORKORDERSet/CLIENT_WORKORDER/WORKORDER/">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<!--Do something special for Node766, like add a certain string-->
<xsl:choose>
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="by"/>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$by"/>
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="by" select="$by"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
对于noob问题很抱歉,但感谢您的任何帮助。谢谢!
这是另一个选项,它只处理在名称以+01:00
结尾的元素中包含_DATE
的text()...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="*[contains(concat(local-name(),'|'),'_DATE|')]/text()[contains(.,'+01:00')]">
<xsl:value-of select="substring-before(.,'+01:00')"/>
<xsl:text>+00:00</xsl:text>
</xsl:template>
</xsl:stylesheet>
您应首先准备替换模板以使其动态化,然后匹配CLIENT_WORKORDER的所有节点 - *[parent::CLIENT_WORKORDER]
以检查它们中的每一个并用+01:00
替换+00:00
,请参阅下面的XSL:
<xsl:stylesheet xmlns:max="http://www.ibm.com/maximo" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<!--template with value replacing-->
<xsl:template name="replace-template">
<xsl:param name="param.str"/>
<xsl:param name="param.target"/>
<xsl:param name="param.replacement"/>
<xsl:choose>
<xsl:when test="contains($param.str, $param.target)">
<xsl:value-of select="concat(substring-before($param.str, $param.target), $param.replacement, substring-after($param.str, $param.target))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$param.str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--copy all nodes-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--take each element of CLIENT_WORKORDER and if it contains +01:00 it will replace it with +00:00-->
<xsl:template match="*[parent::CLIENT_WORKORDER]">
<xsl:element name="{name()}">
<xsl:call-template name="replace-template">
<xsl:with-param name="param.str" select="."/>
<xsl:with-param name="param.target" select="'+01:00'"/>
<xsl:with-param name="param.replacement" select="'+00:00'"/>
</xsl:call-template>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
结果如预期:
<WORKORDERSet>
<WORKORDER>
<CLIENT_WORKORDER>
<COMPLETION_SLA_DATE>2017-07-14T10:11:20+00:00</COMPLETION_SLA_DATE>
<REGION>100000</REGION>
<RESPONSE_SLA_DATE>2017-07-12T10:11:20+00:00</RESPONSE_SLA_DATE>
<WO_NUM>W1000669</WO_NUM>
</CLIENT_WORKORDER>
</WORKORDER>
<WORKORDER>
<CLIENT_WORKORDER>
<COMPLETION_SLA_DATE>2017-12-14T10:11:20+00:00</COMPLETION_SLA_DATE>
<REGION>100000</REGION>
<RESPONSE_SLA_DATE>2017-12-12T10:12:20+00:00</RESPONSE_SLA_DATE>
<WO_NUM>W1000420</WO_NUM>
</CLIENT_WORKORDER>
</WORKORDER>
</WORKORDERSet>