【发布时间】:2014-12-09 10:16:38
【问题描述】:
我尝试了几天,但没有成功。我有以下 XSLT,它不接受任何输入 XML,但有一个参数作为 XML:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:param name="products">
<products author="Jesper">
<product id="p1">
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</product>
<product id="p2">
<name>Golf</name>
<price>1000</price>
<stock>5</stock>
<country>Germany</country>
</product>
<product id="p3">
<name>Alfa</name>
<price>1200</price>
<stock>19</stock>
<country>Germany</country>
</product>
<product id="p4">
<name>Foxtrot</name>
<price>1500</price>
<stock>5</stock>
<country>Australia</country>
</product>
<!-- p5 is a brand new product -->
<product id="p5">
<name>Tango</name>
<price>1225</price>
<stock>3</stock>
<country>Japan</country>
</product>
</products>
</xsl:param>
<xsl:template match="@*|node()" name="initial">
<xsl:copy>
<xsl:apply-templates select="$products / @*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="products">
<xsl:copy>
<xsl:attribute name="dateUpdated">
<xsl:value-of select="current-dateTime()" />
</xsl:attribute>
<xsl:apply-templates select="$products / @*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这是来自here 的示例,我只是使用输入 XML 作为参数。 我的问题是如何在 XSLT 参数上进行身份转换并使这种转换工作?
【问题讨论】:
-
这个参数是硬编码到你的样式表中的吗?如果是,为什么需要转换它(在同一个样式表中)?
-
它实际上是作为外部参数来的,这只是一个例子,这样更容易测试
-
是的,如果它作为参数出现,那么它作为字符串出现,而不是作为 XML。我从您的另一个问题中了解到您已经意识到这一点,但是这个问题本身并没有什么意义。也许将文件的路径作为参数传递会更好?
-
还要注意,在这个特定的例子中,复制比恒等变换更合适。