XML数据类型转换
我需要将源模式定义的所有浮点和双精度数据类型转换为十进制数据类型 我们有一个现有的XSLT,它从第三方获取XML文档并将其转换为XML结构,然后我们可以将其传递到4GL DB应用程序中。本机4GL接口的优点在于,它可以将入站XML转换为本机数据集结构。缺点是它将Float和Double数据类型映射到字符数据类型 XSLT中是否有方法标识浮点/双精度数据类型的元素并将其转换为十进制。我认为这可能是主XSLT之前的步骤,然后将输出传递到主XSLT 中的示例架构:XML数据类型转换,xml,xslt,xsd,xslt-2.0,Xml,Xslt,Xsd,Xslt 2.0,我需要将源模式定义的所有浮点和双精度数据类型转换为十进制数据类型 我们有一个现有的XSLT,它从第三方获取XML文档并将其转换为XML结构,然后我们可以将其传递到4GL DB应用程序中。本机4GL接口的优点在于,它可以将入站XML转换为本机数据集结构。缺点是它将Float和Double数据类型映射到字符数据类型 XSLT中是否有方法标识浮点/双精度数据类型的元素并将其转换为十进制。我认为这可能是主XSLT之前的步骤,然后将输出传递到主XSLT 中的示例架构: <?xml version="
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ttPTManifest">
<xs:complexType>
<xs:sequence>
<xs:element ref="JobCode"/>
<xs:element ref="EstimateHours"/>
<xs:element ref="ActualHours"/>
<xs:element ref="Density"/>
<xs:element ref="NettLitres"/>
<xs:element ref="QuitOutLitres"/>
<xs:element ref="QuitInLitres"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="QuitOutLitres" type="xs:double"/>
<xs:element name="QuitInLitres" type="xs:float"/>
<xs:element name="PTManifestSonicIn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ttPTManifest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobCode" type="xs:string"/>
<xs:element name="NettLitres" type="xs:double"/>
<xs:element name="EstimateHours" type="xs:float"/>
<xs:element name="Density" type="xs:decimal"/>
<xs:element name="ActualHours" type="xs:float"/>
</xs:schema>
<?xml version="1.0"?>
<PTManifestSonicIn xsi:noNamespaceSchemaLocation="SampleIn.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttPTManifest>
<JobCode>000123</JobCode>
<EstimateHours>3.14159E3</EstimateHours>
<ActualHours>3.14159E3</ActualHours>
<Density>123.456</Density>
<NettLitres>3.14159265358979E3</NettLitres>
<QuitOutLitres>3.14159265358979E3</QuitOutLitres>
<QuitInLitres>3.14159E3</QuitInLitres>
</ttPTManifest>
</PTManifestSonicIn>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ttPTManifest">
<xs:complexType>
<xs:sequence>
<xs:element ref="JobCode"/>
<xs:element ref="EstimateHours"/>
<xs:element ref="ActualHours"/>
<xs:element ref="Density"/>
<xs:element ref="NettLitres"/>
<xs:element ref="QuitOutLitres"/>
<xs:element ref="QuitInLitres"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="QuitOutLitres" type="xs:decimal"/>
<xs:element name="QuitInLitres" type="xs:decimal"/>
<xs:element name="PTManifestSonicIn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ttPTManifest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobCode" type="xs:string"/>
<xs:element name="NettLitres" type="xs:decimal"/>
<xs:element name="EstimateHours" type="xs:decimal"/>
<xs:element name="Density" type="xs:decimal"/>
<xs:element name="ActualHours" type="xs:decimal"/>
</xs:schema>
<?xml version="1.0"?>
<PTManifestSonicIn xsi:noNamespaceSchemaLocation="SampleOut.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttPTManifest>
<JobCode>000123</JobCode>
<EstimateHours>3141.59</EstimateHours>
<ActualHours>3141.59</ActualHours>
<Density>123.456</Density>
<NettLitres>3141.59265358979</NettLitres>
<QuitOutLitres>3141.59265358979</QuitOutLitres>
<QuitInLitres>3141.59</QuitInLitres>
</ttPTManifest>
</PTManifestSonicIn>
中的示例XML数据:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ttPTManifest">
<xs:complexType>
<xs:sequence>
<xs:element ref="JobCode"/>
<xs:element ref="EstimateHours"/>
<xs:element ref="ActualHours"/>
<xs:element ref="Density"/>
<xs:element ref="NettLitres"/>
<xs:element ref="QuitOutLitres"/>
<xs:element ref="QuitInLitres"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="QuitOutLitres" type="xs:double"/>
<xs:element name="QuitInLitres" type="xs:float"/>
<xs:element name="PTManifestSonicIn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ttPTManifest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobCode" type="xs:string"/>
<xs:element name="NettLitres" type="xs:double"/>
<xs:element name="EstimateHours" type="xs:float"/>
<xs:element name="Density" type="xs:decimal"/>
<xs:element name="ActualHours" type="xs:float"/>
</xs:schema>
<?xml version="1.0"?>
<PTManifestSonicIn xsi:noNamespaceSchemaLocation="SampleIn.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttPTManifest>
<JobCode>000123</JobCode>
<EstimateHours>3.14159E3</EstimateHours>
<ActualHours>3.14159E3</ActualHours>
<Density>123.456</Density>
<NettLitres>3.14159265358979E3</NettLitres>
<QuitOutLitres>3.14159265358979E3</QuitOutLitres>
<QuitInLitres>3.14159E3</QuitInLitres>
</ttPTManifest>
</PTManifestSonicIn>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ttPTManifest">
<xs:complexType>
<xs:sequence>
<xs:element ref="JobCode"/>
<xs:element ref="EstimateHours"/>
<xs:element ref="ActualHours"/>
<xs:element ref="Density"/>
<xs:element ref="NettLitres"/>
<xs:element ref="QuitOutLitres"/>
<xs:element ref="QuitInLitres"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="QuitOutLitres" type="xs:decimal"/>
<xs:element name="QuitInLitres" type="xs:decimal"/>
<xs:element name="PTManifestSonicIn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ttPTManifest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobCode" type="xs:string"/>
<xs:element name="NettLitres" type="xs:decimal"/>
<xs:element name="EstimateHours" type="xs:decimal"/>
<xs:element name="Density" type="xs:decimal"/>
<xs:element name="ActualHours" type="xs:decimal"/>
</xs:schema>
<?xml version="1.0"?>
<PTManifestSonicIn xsi:noNamespaceSchemaLocation="SampleOut.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttPTManifest>
<JobCode>000123</JobCode>
<EstimateHours>3141.59</EstimateHours>
<ActualHours>3141.59</ActualHours>
<Density>123.456</Density>
<NettLitres>3141.59265358979</NettLitres>
<QuitOutLitres>3141.59265358979</QuitOutLitres>
<QuitInLitres>3141.59</QuitInLitres>
</ttPTManifest>
</PTManifestSonicIn>
000123
3.14159E3
3.14159E3
123.456
3.14159265358979E3
3.14159265358979E3
3.14159E3
重新请求的架构输出:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ttPTManifest">
<xs:complexType>
<xs:sequence>
<xs:element ref="JobCode"/>
<xs:element ref="EstimateHours"/>
<xs:element ref="ActualHours"/>
<xs:element ref="Density"/>
<xs:element ref="NettLitres"/>
<xs:element ref="QuitOutLitres"/>
<xs:element ref="QuitInLitres"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="QuitOutLitres" type="xs:double"/>
<xs:element name="QuitInLitres" type="xs:float"/>
<xs:element name="PTManifestSonicIn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ttPTManifest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobCode" type="xs:string"/>
<xs:element name="NettLitres" type="xs:double"/>
<xs:element name="EstimateHours" type="xs:float"/>
<xs:element name="Density" type="xs:decimal"/>
<xs:element name="ActualHours" type="xs:float"/>
</xs:schema>
<?xml version="1.0"?>
<PTManifestSonicIn xsi:noNamespaceSchemaLocation="SampleIn.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttPTManifest>
<JobCode>000123</JobCode>
<EstimateHours>3.14159E3</EstimateHours>
<ActualHours>3.14159E3</ActualHours>
<Density>123.456</Density>
<NettLitres>3.14159265358979E3</NettLitres>
<QuitOutLitres>3.14159265358979E3</QuitOutLitres>
<QuitInLitres>3.14159E3</QuitInLitres>
</ttPTManifest>
</PTManifestSonicIn>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ttPTManifest">
<xs:complexType>
<xs:sequence>
<xs:element ref="JobCode"/>
<xs:element ref="EstimateHours"/>
<xs:element ref="ActualHours"/>
<xs:element ref="Density"/>
<xs:element ref="NettLitres"/>
<xs:element ref="QuitOutLitres"/>
<xs:element ref="QuitInLitres"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="QuitOutLitres" type="xs:decimal"/>
<xs:element name="QuitInLitres" type="xs:decimal"/>
<xs:element name="PTManifestSonicIn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ttPTManifest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobCode" type="xs:string"/>
<xs:element name="NettLitres" type="xs:decimal"/>
<xs:element name="EstimateHours" type="xs:decimal"/>
<xs:element name="Density" type="xs:decimal"/>
<xs:element name="ActualHours" type="xs:decimal"/>
</xs:schema>
<?xml version="1.0"?>
<PTManifestSonicIn xsi:noNamespaceSchemaLocation="SampleOut.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttPTManifest>
<JobCode>000123</JobCode>
<EstimateHours>3141.59</EstimateHours>
<ActualHours>3141.59</ActualHours>
<Density>123.456</Density>
<NettLitres>3141.59265358979</NettLitres>
<QuitOutLitres>3141.59265358979</QuitOutLitres>
<QuitInLitres>3141.59</QuitInLitres>
</ttPTManifest>
</PTManifestSonicIn>
所需的XML数据输出:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ttPTManifest">
<xs:complexType>
<xs:sequence>
<xs:element ref="JobCode"/>
<xs:element ref="EstimateHours"/>
<xs:element ref="ActualHours"/>
<xs:element ref="Density"/>
<xs:element ref="NettLitres"/>
<xs:element ref="QuitOutLitres"/>
<xs:element ref="QuitInLitres"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="QuitOutLitres" type="xs:double"/>
<xs:element name="QuitInLitres" type="xs:float"/>
<xs:element name="PTManifestSonicIn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ttPTManifest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobCode" type="xs:string"/>
<xs:element name="NettLitres" type="xs:double"/>
<xs:element name="EstimateHours" type="xs:float"/>
<xs:element name="Density" type="xs:decimal"/>
<xs:element name="ActualHours" type="xs:float"/>
</xs:schema>
<?xml version="1.0"?>
<PTManifestSonicIn xsi:noNamespaceSchemaLocation="SampleIn.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttPTManifest>
<JobCode>000123</JobCode>
<EstimateHours>3.14159E3</EstimateHours>
<ActualHours>3.14159E3</ActualHours>
<Density>123.456</Density>
<NettLitres>3.14159265358979E3</NettLitres>
<QuitOutLitres>3.14159265358979E3</QuitOutLitres>
<QuitInLitres>3.14159E3</QuitInLitres>
</ttPTManifest>
</PTManifestSonicIn>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ttPTManifest">
<xs:complexType>
<xs:sequence>
<xs:element ref="JobCode"/>
<xs:element ref="EstimateHours"/>
<xs:element ref="ActualHours"/>
<xs:element ref="Density"/>
<xs:element ref="NettLitres"/>
<xs:element ref="QuitOutLitres"/>
<xs:element ref="QuitInLitres"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="QuitOutLitres" type="xs:decimal"/>
<xs:element name="QuitInLitres" type="xs:decimal"/>
<xs:element name="PTManifestSonicIn">
<xs:complexType>
<xs:sequence>
<xs:element ref="ttPTManifest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="JobCode" type="xs:string"/>
<xs:element name="NettLitres" type="xs:decimal"/>
<xs:element name="EstimateHours" type="xs:decimal"/>
<xs:element name="Density" type="xs:decimal"/>
<xs:element name="ActualHours" type="xs:decimal"/>
</xs:schema>
<?xml version="1.0"?>
<PTManifestSonicIn xsi:noNamespaceSchemaLocation="SampleOut.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ttPTManifest>
<JobCode>000123</JobCode>
<EstimateHours>3141.59</EstimateHours>
<ActualHours>3141.59</ActualHours>
<Density>123.456</Density>
<NettLitres>3141.59265358979</NettLitres>
<QuitOutLitres>3141.59265358979</QuitOutLitres>
<QuitInLitres>3141.59</QuitInLitres>
</ttPTManifest>
</PTManifestSonicIn>
000123
3141.59
3141.59
123.456
3141.59265358979
3141.59265358979
3141.59
为了转换XML数据,可以使用以下样式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="*
[local-name(.)=document(/*/@xsi:noNamespaceSchemaLocation)/*/xs:element[@type=('xs:double', 'xs:float')]/@name]
[. castable as xs:double or . castable as xs:float]">
<xsl:copy>
<xsl:value-of select="number(.)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@type[.=('xs:double', 'xs:float')]">
<xsl:attribute name="type" select="'xs:decimal'"/>
</xsl:template>
</xsl:stylesheet>
您可以使用以下样式表转换架构:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="*
[local-name(.)=document(/*/@xsi:noNamespaceSchemaLocation)/*/xs:element[@type=('xs:double', 'xs:float')]/@name]
[. castable as xs:double or . castable as xs:float]">
<xsl:copy>
<xsl:value-of select="number(.)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@type[.=('xs:double', 'xs:float')]">
<xsl:attribute name="type" select="'xs:decimal'"/>
</xsl:template>
</xsl:stylesheet>
如果可以使用XSL v2,请使用Mads的答案。 如果您坚持使用v1,则可以进行XSD转换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xs:element[@type='xs:double' or @type='xs:float']">
<xsl:copy>
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="type">xs:decimal</xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
十进制
这可以用来转换XML:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="*
[local-name(.)=document(/*/@xsi:noNamespaceSchemaLocation)/*/xs:element[@type='xs:double' or @type='xs:float']/@name]
[number(.)=number(.)]">
<xsl:copy>
<xsl:value-of select="number(.)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我使用的是v2,尽管示例显示的是v1。当我写这篇文章时,我没有意识到这一点。感谢您提供答案。使用XSLT 1.0解决方案进行了更新,该解决方案考虑了引用的XSD文件中的元素类型。谢谢您,Mads,我会尝试一下,并让您知道结果。转换数据的样式表非常有效。但有一个问题,它没有区分由数字字符表示的字符元素值和数字元素。我应该在我的示例中包含一个字符数据类型元素。我编辑了我的原始帖子并包含了一个新元素JobCode。我在@match中添加了额外的条件,它利用模式和声明的数据类型只过滤xs:double
和xs:float
元素。