我正在使用 xsl 将带有属性的标签添加到 xml。我需要在条件
w:t="${para1}"
的情况下执行此操作。当硬编码 para1
时,该过程有效。但是,当我定义 $variable='para1' 并在大括号内使用 $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>
这在 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>