如何使用xslt将xml转换为字符串
我想转换下面的XML如何使用xslt将xml转换为字符串,xml,xslt,biztalk-2010,biztalk-mapper,Xml,Xslt,Biztalk 2010,Biztalk Mapper,我想转换下面的XML <Root> <instruction> <header> <type>A</type> <date>29-08-2018</date> </header> <message> <name>parth</name> <age>24</age>
<Root>
<instruction>
<header>
<type>A</type>
<date>29-08-2018</date>
</header>
<message>
<name>parth</name>
<age>24</age>
</message>
</instruction>
</Root>
A.
29-08-2018
党
24
使用XSLT访问XML
<Root>
<request>
<instruction>
<header>
<type>A</type>
<date>29-08-2018</date>
</header>
<message>
<name>parth</name>
<age>24</age>
</message>
</instruction>
</request>
</Root>
A.
29-08-2018
党
24
在上面的输出中,
标记中的所有标记都是字符串形式,而不是XML元素。有什么建议吗?根据您的问题所示,我将这样编写XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Root">
<xsl:copy>
<request>
<xsl:apply-templates />
</request>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这将输出问题中所示的XML。看
请注意,在复制现有元素时使用了标识模板
但是,从您的注释来看,您似乎希望将元素转换为文本,这是通过“转义”它们来实现的。如果是这种情况,我会这样编写XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" cdata-section-elements="request" />
<xsl:template match="*">
<xsl:value-of select="concat('<', name())" />
<xsl:for-each select="@*">
<xsl:value-of select="concat(' ', name(), '="', ., '"')" />
</xsl:for-each>
<xsl:text>></xsl:text>
<xsl:apply-templates />
<xsl:value-of select="concat('</', name(), '>')" />
</xsl:template>
<xsl:template match="Root">
<xsl:copy>
<request>
<xsl:apply-templates />
</request>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这将产生以下结果
<Root>
<request><![CDATA[
<instruction a="1">
<header>
<type>A</type>
<date>29-08-2018</date>
</header>
<message>
<name>parth</name>
<age>24</age>
</message>
</instruction>
]]></request>
</Root>
A.
29-08-2018
党
原始文件中有更多字段需要在输出映射中具有整个xml结构。请给出建议。您的输入XML格式不正确,因为它有一个开始标记,但没有结束标记。您能否更正问题中的XML,否则将无法对其应用XSLT。你也能说出你试图实现的逻辑吗?您正在尝试添加请求
节点吗?祝您好运Tim我检查并更正了它现在请看一看我指的是输入XML,它缺少结束标记。另外,您是否可以确认,您所要做的只是将request
标记添加到XML中?谢谢,您还可以说“标签中的标签是字符串形式的”。您的意思是想“转义”本例中的所有节点,还是将它们作为CDATA输出。如果是这种情况,则应该显示所需的实际XML。谢谢你,谢谢你的解决方案,蒂姆。当我尝试在BizTalk脚本functoid中应用此命令时,我遇到了一些错误。请在下面查找请求xml(源)年龄CRef错误3 XSL转换错误:“XSL:template”不能是“XSL:template”元素的子元素。我恐怕不知道BizTalk,因此不知道如何使用它应用XSLT。请就错误提供建议。如果源xml目标命名空间为“”,目标xml命名空间为“http:gootemp.com”,请告诉我如何进行此转换