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

时间:2021-04-26 14:09:53

标签: xml xslt 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 hint2 这些都不起作用,或者我可能不知道如何在我的代码中实现它。

2 个答案:

答案 0 :(得分:2)

这是在 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('') 访问样式表文档及其后代

答案 1 :(得分:1)

如果您使用例如创建属性

<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"/>,具体取决于您实现转换的需要。

相关问题