XSL - 身份转换 - 更改元素的值 [英] XSL - Identity transform - change value of an element

查看:26
本文介绍了XSL - 身份转换 - 更改元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将@sorregion name[.='default']"下的 RequestQueue elemt 的值更改为DEFAULT.REQUEST.我尝试使用以下身份模板.任何人都可以帮助我使用此身份模板.我只想使用身份模板.我的xsl文件

I would like to change the value of RequestQueue elemt under "@sorregion name[.='default']" to "DEFAULT.REQUEST. I tried using the below identity template. Could anyone please help me with this dentity template. I would like to use identity template, only. My xsl file

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@name[.='default']/QueueDetails/RequestQueue">
         <xsl:value-of select="'DEFAULT.REQUEST'"/>
    </xsl:template>
</xsl:stylesheet>

我的输入xml

推荐答案

我将如何处理这个问题与@Kirill Polishchuk 所做的相同 (+1 btw),即仅覆盖节点的身份转换需要改变.

How I would approach this question is the same way that @Kirill Polishchuk did (+1 btw) and that is to override the identity transform only for the node that needs to change.

但是,在您的问题中,您表示我只想使用身份模板.".如果确实如此并且您只想要一个模板,您可以这样做:

However, in your question you stated "I would like to use identity template, only.". If this is truly the case and you only want one template, you can do it like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:choose>
      <xsl:when test="current()[name()='RequestQueue'][ancestor::SORRegion[@name = 'default']]">
        <xsl:copy>
          <xsl:text>DEFAULT.REQUEST</xsl:text>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>        
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

我很想知道为什么您只想使用身份转换模板.如果您最终需要修改的不仅仅是 RequestQueue,它会很快变得丑陋.

I would be curious to know though why you would want to use the identity transform template only. If you end up needing to modify more than just RequestQueue, it's going to get ugly fast.

这篇关于XSL - 身份转换 - 更改元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆