花括号中的 XSL 变量

问题描述 投票:0回答:1

我正在使用 xsl 将带有属性的标签添加到 xml。我需要在条件

w:t="${para1}"
的情况下执行此操作。当硬编码
para1
时,该过程有效。但是,当我定义 $variable='para1' 并在大括号内使用 $variable
{
}
时,它不起作用。

尝试了以下方法:

  1. ${
    $变量
    }
  2. 将左右大括号定义为变量。然后连接(左,变量,右)
  3. 双左右大括号 ${{$variable}}

xml

    <?xml version="1.0"?>
    <w:document xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" 
     xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" 
     xmlns:w10="urn:schemas-microsoft-com:office:word" 
     xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" 
     xmlns:v="urn:schemas-microsoft-com:vml" 
     xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" 
     xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" 
     xmlns:o="urn:schemas-microsoft-com:office:office"
     xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <w:body>
      <w:p>
        <w:r>
           <w:t>${para1}</w:t>
       </w:r>
     </w:p>
    </w:body>
    </w:document>

xsl

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    >
    <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
    </xsl:template>
    <xsl:variable name="variable">para1</xsl:variable>
    <xsl:variable name="attribute_id">1</xsl:variable>
    <xsl:template match='w:p[w:r/w:t="${para1}"]'>
    <!--<xsl:variable name="left">${</xsl:variable>
    <xsl:variable name="right">}</xsl:variable>
    <xsl:template match='w:p[w:r/w:t="concat($left,$variable,$right)"]'>-->
    <!--<xsl:template match='w:p[w:r/w:t="${$variable}"]'>--><!--para1 should be variable-->
    <xsl:copy>
      <xsl:attribute name="w:id"> <xsl:value-of select="$attribute_id" /></xsl:attribute>
     <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

在线编辑代码

xml xslt xslt-1.0 curly-braces
1个回答
0
投票

这在 XSLT 3.0 中有效,但我还没有用 1.0 测试过。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    >
      
    <xsl:variable name="para1">ReplacementValue</xsl:variable>
    <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*,node()" />
      </xsl:copy>
    </xsl:template>
    <xsl:template match="w:p/w:r/w:t/text()[matches(., '[$][{]para1[}]')]">
      <xsl:value-of select="$para1" />
    </xsl:template>
</xsl:stylesheet>

它使用硬编码的

para1
变量生成此输出,该变量可以来自样式表参数。

<?xml version="1.0" encoding="UTF-8"?><w:document xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
    <w:body>
      <w:p>
        <w:r>
           <w:t>ReplacementValue</w:t>
       </w:r>
     </w:p>
    </w:body>
    </w:document>
© www.soinside.com 2019 - 2024. All rights reserved.