2013-10-30 135 views
1

我使用XSLT转换为XML节点值和我有一个webdocuments节点的XML型webdocument创建与其他节点的信息

<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。上面,有一个例子转化,其中参数1应该是$散列,散列节点的值(在这种情况下1c1bc9f96349fc954cba2dfb58f214b1)和参数2应该是$ ID的ID节点值在这种情况下,808924

<webDocuments> 
    <WebDocument> 
    <id>808924</id> 
    <fileName><![CDATA[file name]]></fileName> 
    <filePath>http://url.com/param1=$hash&param2=$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&param2=808924 

我尝试了很多事情,但我没有得到我预期的结果:

<xsl:template match="/webDocuments"> 
    <xsl:for-each select="/WebDocument"> 
    <xsl:value-of select="$hash"/> 
     <xsl:value-of select="concat($baseUrl,$baseCAPUrl,$hash,'&amp;fileId&#61;')"/> 

    </xsl:for-each> 
</xsl:template> 

总之我的结果是采取一个节点值,并用它来产生一个又一个。

在此先感谢

+0

提供完整的代码。什么是'$ baseUrl,$ baseCAPUrl,$ hash'? –

回答

0

这应该是你需要的。 Theres没有真正需要使用concat(),您只需要连续的xsl:value-ofxsl:text节点即可实现您在此处需要的功能。此外,请尝试design "push" templatesxsl: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> 
+0

非常感谢它! – user670852

0

要注意的第一件事情就是你的XML不能很好地形成,因为这条线

<filePath>http://url.com/param1=$hash&param2=$id</filePath> 

在这里逃走了&需求。它应该是这样的

<filePath>http://url.com/param1=$hash&amp;param2=$id</filePath> 

总之,在回答你的问题,它看起来像你想替换的文件路径“变量”,与使用相同名称的元素的XML实际值。

您已将此作为XSLT2.0,这是很好的,因为你可以使用XSL :分析串这里寻找你的“变量”,如$哈希

<xsl:analyze-string select="." regex="\$\w+"> 

在此,您可以指定在找到“匹配子字符串”时要执行的操作。在这种情况下,您想要找到与刚刚匹配的变量名称相同的元素(减去$前缀)。

<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&amp;param2=808924</filePath> 
     <hash>1c1bc9f96349fc954cba2dfb58f214b1</hash> 
     <title>Primer document</title> 
     <creationDate class="sql-timestamp">30/05/2012 15:49:57</creationDate> 
    </WebDocument> 
</webDocuments> 

请注意,这里使用的XSLT identity template量,输出的所有其他节点原样。