我正在使用XSLT转换XML,并且我有一个带有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>我正在尝试将filepath节点值(文件系统)转换为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>总之,我的结果是获取一个节点值并使用它生成另一个节点值。
提前感谢
发布于 2013-10-30 23:20:46
这应该是你需要的。没有真正的使用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>发布于 2013-10-30 23:46:19
首先要注意的是,您的XML格式不太好,因为这一行。
<filePath>http://url.com/param1=$hash¶m2=$id</filePath>&需要在这里逃脱。它真的应该像这样
<filePath>http://url.com/param1=$hash&param2=$id</filePath>无论如何,为了回答您的问题,您似乎希望使用同名的元素替换filePath中的“变量”,使用来自XML的实际值。
您已经将其标记为XSLT2.0,这很好,因为您可以在这里使用xsl:analyze-string查找您的“变量”,如$hash
<xsl:analyze-string select="." regex="\$\w+">在此范围内,您可以指定在找到“匹配-子字符串”时要做什么。在本例中,您希望找到与您刚刚匹配的变量同名的元素(减去$prefix)。
<xsl:matching-substring>
<xsl:value-of select="$WebDocument/*[local-name() = substring(current(), 2)]"/>
</xsl:matching-substring>(在本例中,$WebDocument是指向当前WebDocument元素的变量)
对于“非匹配”字符串,只需按如下方式输出
<xsl:non-matching-substring>
<xsl:value-of select="current()"/>
</xsl:non-matching-substring>试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="filePath">
<xsl:variable name="WebDocument" select=".."/>
<xsl:copy>
<xsl:analyze-string select="." regex="\$\w+">
<xsl:matching-substring>
<xsl:value-of select="$WebDocument/*[local-name() = substring(current(), 2)]"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="current()"/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>这应该会输出以下内容
<webDocuments>
<WebDocument>
<id>808924</id>
<fileName>file name</fileName>
<filePath>http://url.com/param1=1c1bc9f96349fc954cba2dfb58f214b1&param2=808924</filePath>
<hash>1c1bc9f96349fc954cba2dfb58f214b1</hash>
<title>Primer document</title>
<creationDate class="sql-timestamp">30/05/2012 15:49:57</creationDate>
</WebDocument>
</webDocuments>请注意这里使用的XSLT标识模板,以输出所有其他节点。
https://stackoverflow.com/questions/19695200
复制相似问题