【问题标题】:Why does this xslt transformation replace all occurences?为什么这个 xslt 转换会替换所有出现的事件?
【发布时间】:2012-12-06 10:35:34
【问题描述】:

使用http://xslt.online-toolz.com/tools/xslt-transformation.php

.xml

<?xml version="1.0"?>
<my:project xmlns:my="http://myns">
<my:properties>
 <my:property>
  <my:name>customerId</my:name>
  <my:value>1</my:value>
 </my:property>
 <my:property>
  <my:name>userId</my:name>
  <my:value>20</my:value>
 </my:property>
</my:properties>
</my:project>

我现在想查找名称 customerId 并想替换 value

它几乎可以工作,但它会替换文档中的所有值。只是替换名称匹配的值,我做错了什么?

.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="http://myns" xmlns:saxon="http://saxon.sf.net"> 

 <xsl:param name="name" select="'customerId'"/>
 <xsl:param name="value" select="'0'"/>

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
 </xsl:template>

 <xsl:template match="/*/my:properties/my:property/my:value/text()" > 
   <xsl:choose>
     <xsl:when test="/*/my:properties/my:property/my:name = $name">
        <xsl:value-of select="$value"/>
     </xsl:when>
     <xsl:otherwise><xsl:copy-of select="saxon:parse(.)" /></xsl:otherwise>
   </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: java xml xslt-2.0


    【解决方案1】:

    /*/my:properties/my:property/my:name = $name 的测试总是成功,因为它使用绝对路径,因此结果与周围的模板上下文无关。使用相对 xpath 的测试应该可以工作。

    XSL:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:my="http://myns" xmlns:saxon="http://saxon.sf.net"> 
    
        <xsl:param name="name" select="'customerId'"/>
        <xsl:param name="value" select="'0'"/>
    
        <xsl:template match="node() | @*">
            <xsl:copy>
                <xsl:apply-templates select="node() | @*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="/*/my:properties/my:property/my:value/text()" > 
            <xsl:choose>
                <xsl:when test="../../my:name = $name">
                    <xsl:value-of select="$value"/>
                </xsl:when>
                <xsl:otherwise>otherwise</xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    XML:

    <my:project xmlns:my="http://myns">
        <my:properties>
            <my:property>
                <my:name>customerId</my:name>
                <my:value>1</my:value>
            </my:property>
            <my:property>
                <my:name>userId</my:name>
                <my:value>20</my:value>
            </my:property>
        </my:properties>
    </my:project>
    

    saxonb-xslt -s:test.xml -xsl:test.xsl的结果

    <my:project xmlns:my="http://myns">
        <my:properties>
            <my:property>
                <my:name>customerId</my:name>
                <my:value>0</my:value>
            </my:property>
            <my:property>
                <my:name>userId</my:name>
                <my:value>otherwise</my:value>
            </my:property>
        </my:properties>
    </my:project>
    

    【讨论】:

    • @membersound:我添加了我测试的完整示例,你得到了什么错误?
    • 好吧,你是对的,它按预期工作。谢谢!问题似乎是saxon:parse(.) 声明。只需使用 . 一切正常。
    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 2020-06-13
    • 2017-03-15
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多