如何使用XSLT转换生成此XML文档?

问题描述 投票:0回答:2

原始XML:

<EbsSendOTPResponse>
<ResponseHeader>
<GUID/>
<GUID2/>
</ResponseHeader>
</EbsSendOTPResponse>

已转换为:

<EbsSendOTPResponse type="group">
<ResponseHeader type="group">
<GUID/>
<GUID2/>
</ResponseHeader>
</EbsSendOTPResponse>

我想将type =“ group”服装添加到父标签。

xml xslt transform
2个回答
0
投票
您可以尝试以下方法:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="node()|@*"> <xsl:copy> <xsl:if test="*"> <!-- <xsl:if test="name()='ResponseHeader' or name()='EbsSendOTPResponse'"> --> <xsl:attribute name="type">group</xsl:attribute> </xsl:if> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> </xsl:stylesheet>


0
投票
请使用下面的代码,只需匹配模板并添加@type属性

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="EbsSendOTPResponse"> <EbsSendOTPResponse typpe="group"> <xsl:apply-templates/> </EbsSendOTPResponse> </xsl:template> <xsl:template match="ResponseHeader"> <ResponseHeader typpe="group"> <xsl:apply-templates/> </ResponseHeader> </xsl:template> </xsl:stylesheet>

© www.soinside.com 2019 - 2024. All rights reserved.