修改 XML 标记(属性值)XSLT 1.0

Modify XML Tag (Value of an Attribute) XSLT 1.0

我正在尝试构建一个输出文件(基于确定的模板)并将另一个 xml.

作为输入

用于构建输出的 xslt 代码 (XSLT 1.0) 按部分“划分”。

我跳过了代码的第一部分...我正在“分配”变量, 例如:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date">
    
<xsl:variable name="securityCode"select="/A/securityCode"/>
<!--and the others variables to I think is are not relevant for my question-->

xslt(原来的代码比这个多了很多段,而且我必须用它来对不同的输入产生不同的输出)

                                       <!--Here we call the templates-->

<xsl:template match="/">
 <Header>

 <xsl:call-template name="Header">
 <xsl:with-param name="securityCode" select = "$securityCode"/>
 <xsl:with-param name="generatedTime" select = "$generatedTime"/>
 </xsl:call-template>

 </Header>

</xsl:template>

                                       



<xsl:call-template name="StructureData">

<xsl:with-param name = "payDate"/>
<xsl:with-param name = "amount"/>
<xsl:with-param  name = "priRte"/> 
<xsl:with-param name = "settlement"/> 
<xsl:with-param name = "rate"/> 
<xsl:with-param name = "curr"/> 
<xsl:with-param name = "rdate"/>
<xsl:with-param name = "ID"/>
<xsl:with-param name = "CID" />
<xsl:with-param name = "Nom" />

</xsl:call-template>

                                     <!--Here we call "define" the templates-->
<xsl:template name = "Header" >
<xsl:param name = "securityCode" />
<xsl:param name = "generatedTime" />

 <NHeader>
  <MessageID><xsl:value-of select="$securityCode"/></MessageID>
 
  <Timezone>GMT</Timezone>
  <GeneratedTime><xsl:value-of select="$generatedTime"/></GeneratedTime>
  
 </NHeader>

</xsl:template>

<xsl:template name = "StructureData" >

<xsl:param name = "payDate"/>
<xsl:param name = "amount"/>
<xsl:param name = "priRte"/> 
<xsl:param name = "settlement"/> 
<xsl:param name = "rate"/> 
<xsl:param name = "curr"/> 
<xsl:param name = "rdate"/>
<xsl:param name = "ID"/>
<xsl:param name = "CID" />
<xsl:param name = "Nom" />
<xsl:param name = "securityCode" />
                    <Data2>
                      <ID><xsl:value-of select="$ID"/></ID>
                      <Sett><xsl:value-of select="$settlement"/></Sett>
                      <BuyOrSell value="Sell"/>
                      <price value="Price"><xsl:value-of select="$priRte"/></price>
                      <Cost><xsl:value-of select="$Nom"/></Cost>
                     <Accrual>
                        <Cashflow CFLType="Principal">
                          <Cid><xsl:value-of select="$CID"/></CID>
                          <CashflowPayment>
                            <PayDate><xsl:value-of select="$payDate"/></PayDate>
                            <Amount><xsl:value-of select="$amount"/></Amount>
                        </CashflowPayment>
                      </Cashflow>
                  </Accrual>
                </Data2>

</xsl:template>


</xsl:stylesheet>

最后,我的问题:

我要修改(或重命名节点值 :

<BuyOrSell value="Sell"/>

根据输入条件(来自输入)变量,假设上面定义了 secutityCode

假设(securityCode可以是:12

  1. securityCode=1 然后 <BuyOrSell value="Sell"/>
  2. securityCode=2 然后 <BuyOrSell value="Buy"/>

我知道如何修改节点,但是一旦我将模板作为输入...实际上有一些提示:hint1 none 这些作品,或者可能我不知道如何在我的代码中实现它。

如果您使用

创建属性
<BuyOrSell>
  <xsl:attribute name="value">
    <xsl:choose>
      <xsl:when test="$securityCode = 1">Sell</xsl:when>
      <xsl:when test="$securityCode = 2">Buy</xsl:when>
    </xsl:choose>
  </xsl:attribute>
</BuyOrSell>

您可以实现该条件,尽管在 XSLT 1 中这是一种相当冗长的方式。

一般来说,我会建议尝试使用模板匹配,例如

<xsl:template match="/A/securityCode[. = 1]">
  <BuyOrSell>Sell</BuyOrSell>
</xsl:template>

<xsl:template match="/A/securityCode[. = 2]">
  <BuyOrSell>Buy</BuyOrSell>
</xsl:template>

然后在树处理中进一步使用 <xsl:apply-templates/><xsl:apply-templates select="securityCode"/>,具体取决于您的需要以实现转换。

这是在 XSLT 1.0

中执行此操作的紧凑方法

(就像这样在一行中:<BuyOrSell value="{$vOps[$psecurityCode]}"/>,不管可能的不同操作码的数量):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:param name="psecurityCode" select="1"/>
 <xsl:variable name="vTxs">
   <op>Sell</op>
   <op>Buy</op>
 </xsl:variable>
 
 <xsl:variable name="vOps" select="document('')/*/xsl:variable[@name='vTxs']/*"/>

  <xsl:template match="/">
    <BuyOrSell value="{$vOps[$psecurityCode]}"/>
  </xsl:template>
</xsl:stylesheet>

当此转换应用于任何 XML 文档(未使用)时,会产生所需的正确结果:

<BuyOrSell value="Sell"/>

如果您将 xsl:param 声明替换为:<xsl:param name="psecurityCode" select="2"/> 然后再次产生想要的结果:

<BuyOrSell value="Buy"/>

二.说明

  1. 使用 AVT (Attribute Value Templates)

  2. 使用document('')访问样式表文档及其后代