从 xml 中提取(架构)标签
Extract (schema) tags from xml
我有一个 xml 文件,其中包含模式信息。为了验证 xml,我想提取架构信息。我如何通过 phyton 脚本或 xslt 转换来实现这一点?验证将在 nifi xmlValidator 处理器中进行。
我尝试了 xsl 转换,但 xsd 前缀是问题所在。
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:od="urn:schemas-microsoft-com:officedata">
<xsd:schema>
<xsd:element name="dataroot">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element ref="AE"></xsd:element>
...
</xsd:schema>
<dataroot>
...</dataroot>
</root>
您可以使用以下 XSLT-1.0 样式表从您的文件中提取 XSD 部分。首先,它匹配 /root
元素,然后使用自定义 身份模板 :
复制所有 xsd:...
子元素
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<!-- Identity template for 'xsd' -->
<xsl:template match="@*|node()" mode="xsd">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="xsd" />
</xsl:copy>
</xsl:template>
<xsl:template match="/root/xsd:schema">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="xsd" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
结果是:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="dataroot">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded"><xsd:element ref="AE"/>
...
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
我忽略了 ...
,因为它们可能不属于 XML。
以下 XSLT 2.0 样式表将文档拆分为模式文档和实例文档。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="xsd:schema">
<xsl:result-document href="schema.xsd">
<xsl:copy-of select="." />
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
我有一个 xml 文件,其中包含模式信息。为了验证 xml,我想提取架构信息。我如何通过 phyton 脚本或 xslt 转换来实现这一点?验证将在 nifi xmlValidator 处理器中进行。
我尝试了 xsl 转换,但 xsd 前缀是问题所在。
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:od="urn:schemas-microsoft-com:officedata">
<xsd:schema>
<xsd:element name="dataroot">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element ref="AE"></xsd:element>
...
</xsd:schema>
<dataroot>
...</dataroot>
</root>
您可以使用以下 XSLT-1.0 样式表从您的文件中提取 XSD 部分。首先,它匹配 /root
元素,然后使用自定义 身份模板 :
xsd:...
子元素
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<!-- Identity template for 'xsd' -->
<xsl:template match="@*|node()" mode="xsd">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="xsd" />
</xsl:copy>
</xsl:template>
<xsl:template match="/root/xsd:schema">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="xsd" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
结果是:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="dataroot">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded"><xsd:element ref="AE"/>
...
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
我忽略了 ...
,因为它们可能不属于 XML。
以下 XSLT 2.0 样式表将文档拆分为模式文档和实例文档。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="xsd:schema">
<xsl:result-document href="schema.xsd">
<xsl:copy-of select="." />
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>