【发布时间】:2017-10-22 19:29:11
【问题描述】:
我正在尝试将输入 XML 完全转换为新的 XML 格式。它没有保留输出 XML 上的命名空间。我尝试了一些来自 SO 的建议,但是由于我正在创建新的根元素,因此它没有保留命名空间。任何帮助深表感谢!提前致谢!
输入的 XML 是:
<Container
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://hgkl.kj.com">
<Request>
<Id>Guid</Id>
<Name>ABC</Name>
</Request>
</Container>
XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="/*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:variable name="root" select="name()" />
<xsl:element name="{$root}">
<xsl:apply-templates select="child::*"/>
</xsl:element>
</xsl:template>
<xsl:template match="child::*">
<xsl:element name="Input">
// rest of the logic
</xsl:element>
</xsl:template>
</xsl:stylesheet>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<Container>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://hgkl.kj.com">
<Input>
// rest of the logic for forming the element
</Input>
</Container>
XML 中的根标记和其他标记是动态的。转换背后的主要目标是根据字典 xml 中的值将传入的 XML 元素和属性转换为另一种语言 [如:西班牙语]。类似于post
【问题讨论】:
-
您的输入 XML 和预期的输出 XML 格式不正确。
标签: xslt xml-namespaces