2015-10-14 76 views
0

我在xslt中遇到了问题。我的问题是,我在xml中获得了一行,应该将其替换为xsl。我想替换由informatica本身生成的informatica文件。切换顶级元素

起初,这里是我的xsl:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output 
    method="xml" 
    indent="yes" 
    omit-xml-declaration="no" 
    media-type="string" 
    encoding="ISO-8859-1" 
    doctype-system="deftable.dtd" 
    /> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="//AUTOEDIT2[@NAME='%%PARAM']"> 
     <xsl:choose> 
      <xsl:when test="../JOB[@JOBNAME]='FILE_STUFF'"> 
       <AUTOEDIT2 NAME="%%PARAM" VALUE="*"/> 
      </xsl:when> 
      <xsl:when test="../JOB[@JOBNAME]='DATA_STUFF'"> 
       <AUTOEDIT2 NAME="%%PARAM" VALUE="*"/> 
      </xsl:when> 
      <xsl:when test="../JOB[@JOBNAME]='TANSFER_STUFF'"> 
       <AUTOEDIT2 NAME="%%PARAM" VALUE="*"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <AUTOEDIT2 NAME="%%PARAM" VALUE="20150910"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
    </xsl:stylesheet> 

现在,这是我的XML,在更换时应做到:

<SOMETREE> 
    <JOB JOBNAME="FILE_STUFF"> 
     <AUTOEDIT2 NAME="%%PARAM" VALUE="not so important, should be overwritten"/> 
    </JOB> 
    <JOB JOBNAME="DATA_STUFF"> 
     <AUTOEDIT2 NAME="%%PARAM" VALUE="not so important, should be overwritten"/> 
    </JOB> 
    <JOB JOBNAME="TANSFER_STUFF"> 
     <AUTOEDIT2 NAME="%%PARAM" VALUE="not so important, should be overwritten"/> 
    </JOB> 
    <JOB JOBNAME="OTHER_STUFF"> 
     <AUTOEDIT2 NAME="%%PARAM" VALUE="not so important, should be overwritten"/> 
    </JOB> 
</SOMETREE> 

所以我要覆盖值 “VALUE” AUTOEDIT2字段,与JOBNAME相关。

非常感谢您的时间。

问候 比约恩

回答

1

不要使用<xsl:choose>了点。你可以做到这一点通过模板匹配:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no" /> 
    <xsl:output encoding="ISO-8859-1" doctype-system="deftable.dtd" /> 

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

    <xsl:template match="AUTOEDIT2[@NAME = '%%PARAM' and ../@JOBNAME = 'FILE_STUFF']" priority="1"> 
     <AUTOEDIT2 NAME="%%PARAM" VALUE="*" /> 
    </xsl:template> 

    <xsl:template match="AUTOEDIT2[@NAME = '%%PARAM' and ../@JOBNAME = 'DATA_STUFF']" priority="1"> 
     <AUTOEDIT2 NAME="%%PARAM" VALUE="*" /> 
    </xsl:template> 

    <xsl:template match="AUTOEDIT2[@NAME = '%%PARAM' and ../@JOBNAME = 'TANSFER_STUFF']" priority="1"> 
     <AUTOEDIT2 NAME="%%PARAM" VALUE="*" /> 
    </xsl:template> 

    <xsl:template match="AUTOEDIT2[@NAME = '%%PARAM']" priority="0"> 
     <xsl:copy>20150910</xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

注意的显式模板的优先级:所有AUTOEDIT2模板默认情况下具有相同的优先级(由XSLT引擎从他们的匹配表达式计算)。设置明确的优先级可以定义哪个模板赢得平局。

任何前三个模板和最后一个模板之间可能发生联系。给它一个较低的优先级,确保它只在AUTOEDIT2元素不能与其他三个匹配时才被选中。

由于前三AUTOEDIT2模板期望的结果似乎是一样的,你可能崩溃他们进入一个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no" /> 
    <xsl:output encoding="ISO-8859-1" doctype-system="deftable.dtd" /> 

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

    <xsl:template match="AUTOEDIT2[@NAME = '%%PARAM' and 
     (
     ../@JOBNAME = 'FILE_STUFF' 
     or ../@JOBNAME = 'DATA_STUFF' 
     or ../@JOBNAME = 'TANSFER_STUFF' 
    ) 
    ]" priority="1"> 
     <AUTOEDIT2 NAME="%%PARAM" VALUE="*" /> 
    </xsl:template> 

    <xsl:template match="AUTOEDIT2[@NAME = '%%PARAM']" priority="0"> 
     <xsl:copy>20150910</xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

另一种方式把匹配表达式是:

JOB[ 
    @JOBNAME = 'FILE_STUFF' or @JOBNAME = 'DATA_STUFF' or @JOBNAME = 'TANSFER_STUFF' 
]/AUTOEDIT2[@NAME = '%%PARAM']