如何使用 xsltproc 将 shell 变量传递到 xsl 中?



我想传递一个变量name=$(echo "$t" | cut -f 1 -d '.')从外壳脚本到以下 XSL 作为$name变量:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="dc:record">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | *" />
<xsl:param name="name"/>
<dc:relation><xsl:value-of select="$name"/></dc:relation>    
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

是否可以通过xsltproc -o ie1.xml ../../transform.xsl ie1.xml发送变量以及如何执行此操作?

在顶层定义xsl:param

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:param name="name" />
...

然后使用--stringparam参数调用xsltproc

xsltproc --stringparam name "$name" -o ie1.xml ../../transform.xsl ie1.xml

就这样。

最新更新