【问题标题】:XSL - Identity transform - change value of an elementXSL - 身份转换 - 更改元素的值
【发布时间】:2011-11-04 13:17:17
【问题描述】:

我想将“@sorregion name[.='default']”下的 RequestQueue elemt 的值更改为“DEFAULT.REQUEST”。我尝试使用以下身份模板。谁能帮我处理这个身份模板。我只想使用身份模板。 我的 xsl 文件

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

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    我将如何处理这个问题与@Kirill Polishchuk 所做的相同(+1 顺便说一句),即只为需要更改的节点覆盖身份转换。

    但是,在您的问题中,您说“我只想使用身份模板。”。如果确实如此,并且您只需要一个模板,您可以这样做:

    <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,它会很快变得丑陋。

    【讨论】:

    • 感谢您的所有回答。如何单独更改属性值“默认”?其余一切都应保持不变。 SORRegion name="default" 应改为 SORRegion name="New"
    • @user1004770 - 看起来 Dimitre 在这里回答了您关于更改“名称”属性值的问题:stackoverflow.com/questions/8036882/…
    【解决方案2】:

    使用此模板:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="SORRegion[@name = 'default']/QueueDetails/RequestQueue">
          <xsl:copy>
            <xsl:text>DEFAULT.REQUEST</xsl:text>
          </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多