我正在使用XSLT转换XML,并且我有一个带有一个webdocuments节点和一个类型为webdocument的节点的xml。
<webDocuments>
<WebDocument>
<id>808924</id>
<fileName><![CDATA[file name]]></fileName>
<filePath><![CDATA[.../201504/808924/filename.pdf]]></filePath>
<hash><![CDATA[1c1bc9f96349fc954cba2dfb58f214b1]]></hash>
<title><![CDATA[Primer document]]></title>
<creationDate class="sql-timestamp">30/05/2012 15:49:57</creationDate>
</WebDocument>
</webDocuments>
我正在尝试将文件路径节点值(文件系统)转换为URL。上面有一个转换后的示例,其中param1应该是$ hash,哈希节点值(在这种情况下为1c1bc9f96349fc954cba2dfb58f214b1),而param2应该是$ id,在这种情况下为id节点值808924
<webDocuments>
<WebDocument>
<id>808924</id>
<fileName><![CDATA[file name]]></fileName>
<filePath>http://url.com/param1=$hash¶m2=$id</filePath>
<hash><![CDATA[1c1bc9f96349fc954cba2dfb58f214b1]]></hash>
<title><![CDATA[Primer document]]></title>
<creationDate class="sql-timestamp">30/05/2012 15:49:57</creationDate>
</WebDocument>
</webDocuments>
综上所述
<filePath>http://url.com/param1=1c1bc9f96349fc954cba2dfb58f214b1¶m2=808924
我做了很多尝试,但没有得到预期的结果:
<xsl:template match="/webDocuments">
<xsl:for-each select="/WebDocument">
<xsl:value-of select="$hash"/>
<xsl:value-of select="concat($baseUrl,$baseCAPUrl,$hash,'&fileId=')"/>
</xsl:for-each>
</xsl:template>
总之,我的结果是取一个节点值并用于生成另一个值。
提前致谢
这应该是您所需要的。没有真正的需要使用concat()
,您可以只具有连续的xsl:value-of
和xsl:text
节点即可在此处实现所需的功能。此外,请尝试设计使用的模板,xsl:apply-templates
而不要使用的“拉”模板xsl:for-each
。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="WebDocument">
<WebDocument>
<xsl:apply-templates select="*"/>
</WebDocument>
</xsl:template>
<xsl:template match="filePath">
<filePath>
<!-- change below to the right value -->
<xsl:variable name="baseUrl">http://url.com</xsl:variable>
<!-- change below too, but I can't see where you use this in the URL -->
<xsl:variable name="baseCAPUrl">/CAP/</xsl:variable>
<xsl:value-of select="$baseUrl"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$baseCAPUrl"/>
<xsl:text>?param1=</xsl:text>
<xsl:value-of select="../hash"/>
<xsl:text>param2=</xsl:text>
<xsl:value-of select="../id"/>
</filePath>
</xsl:template>
<!-- XSL identity -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
给出:
<?xml version="1.0" encoding="UTF-8"?>
<webDocuments>
<WebDocument>
<id>808924</id>
<fileName>file name</fileName>
<filePath>http://url.com//CAP/?param1=1c1bc9f96349fc954cba2dfb58f214b1param2=808924</filePath>
<hash>1c1bc9f96349fc954cba2dfb58f214b1</hash>
<title>Primer document</title>
<creationDate class="sql-timestamp">30/05/2012 15:49:57</creationDate>
</WebDocument>
</webDocuments>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句