0

我有一个applescript,它使用xmltransform 来转换使用外部xlst 文件的xml。我想在 applescript 中添加一个变量以供 xslt 文件拾取。satimage 字典谈到使用 xsl 参数作为 xmltransform 的一部分,但我找不到任何示例。

使用模板XMLTransform xmlFile with xsltFile in outputpath 我将如何在 applescript 和以下 xsl 文件示例中定义变量。

<xsl:stylesheet version="1.0">
    <xsl:param name="vb1" value="'unknown'"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="xmeml/list/name">
    <xsl:value-of select="{$vb1}"/>
  </xsl:template>
</xsl:stylesheet>

Applescript 片段

set vb1 to "Hello" as string
XMLTransform xmlFile with xsltFile1 in outputpathtemp xsl params vb1

当前的小程序返回“无法使 vb1 进入记录”。

我现在正在考虑使用以下内容,但它在 XML 中返回 NULL

set vb1 to {s:"'hello'"}
XMLTransform xmlFile with xsltFile1 in outputpathtemp xsl string params vb1

输入是

<xmeml>
   <list> 
     <name>IMaName</name>
     <!-- other nodes -->
   </list>
</xmeml>

当前输出为

<xmeml>
   <list> 
     <name/>
     <!-- other nodes -->
   </list>
</xmeml>

任何人都可以帮忙吗?非常感谢。

4

3 回答 3

1

xsl params需要记录或清单

必须引用原始字符串参数

set vb1 to "'Hello'" -- added simple quote
XMLTransform xmlFile with xsltFile1 in outputpathtemp xsl params {vb1} --  {} = list
于 2012-07-25T01:07:02.873 回答
1

当您使用参数调用 XSLT 时,参数在内部看起来与 XSLT 中的变量非常相似(就像发送给函数的参数一样),因此如果您将参数作为名称传入:

<xsl:stylesheet version="1.0">
  <xsl:parameter name="paramname" value="'unknown'"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="xmeml/list/name">
    <xsl:value-of select="{$paramname}"/>
  </xsl:template>
</xsl:stylesheet>

在这种情况下传递参数时,您将使用名称“paramname”(尽管也许其他名称会更好!),并且该值存在以防您不传递任何内容。如果你这样做,它将被替换。

于 2012-07-24T09:33:02.803 回答
0

这是答案:

苹果脚本

XMLTransform xmlFile with xsltFile1 in outputpathtemp xsl params {"vb1", "'hello'"}

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:param name="vb1" />

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy> 
</xsl:template>

<xsl:template match="/xmeml/list/name">
<xsl:element name="name">
       <xsl:value-of select="$vb1"/>
    </xsl:element>
 </xsl:template>
</xsl:stylesheet>

谢谢。

于 2012-07-25T10:41:59.847 回答