【问题标题】:How can I produce this XML document using an XSLT transformation?如何使用 XSLT 转换生成此 XML 文档?
【发布时间】:2020-03-27 23:54:46
【问题描述】:

原始 XML:

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

转化为:

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

我想将 type="group" 属性添加到父标签。

【问题讨论】:

    标签: xml xslt transform


    【解决方案1】:

    你可以试试这个:

    <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>
    

    【讨论】:

    • 非常感谢,但我有不同的标签,没有“ResponseHeader”和“EbsSendOTPResponse”。我需要捕获父节点并添加我的属性( type="group" )。
    • 如果我理解的很好,尝试用“”替换测试条件
    【解决方案2】:

    请使用下面的代码,只需匹配模板并添加@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>
    

    【讨论】:

    • 非常感谢,但我有不同的标签,没有“ResponseHeader”和“EbsSendOTPResponse”。我需要捕获父节点并添加我的属性( type="group" )。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2017-05-09
    • 2012-05-23
    • 1970-01-01
    • 2020-11-22
    • 2012-01-12
    • 1970-01-01
    相关资源
    最近更新 更多