使用其他节点信息创建节点值

时间:2013-10-30 22:41:20

标签: xml xslt xslt-1.0 xslt-2.0

我正在使用XSLT转换XML,我有一个xml,其中一个webdocuments节点的节点类型为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。上面是一个转换的例子,其中param1应该是$ hash,哈希节点值(在这种情况下为1c1bc9f96349fc954cba2dfb58f214b1)和param2应该是$ 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>

总之,我的结果是获取一个节点值并用于生成另一个节点值。

提前致谢

2 个答案:

答案 0 :(得分:0)

这应该是你需要的。没有必要使用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>

答案 1 :(得分:0)

首先要注意的是,由于此行

,您的XML格式不正确
<filePath>http://url.com/param1=$hash&param2=$id</filePath>

&需要在此处进行转义。它应该看起来像这样

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

无论如何,为了解决您的问题,您似乎希望使用具有相同名称的元素替换filePath中的“变量”。

您已将此标记为XSLT2.0,这很好,因为您可以在此处使用 xsl:analyze-string 来查找“变量”,例如$ hash

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

在此范围内,您可以指定在找到“matching-substring”时要执行的操作。在这种情况下,您希望找到与您刚刚匹配的变量同名的元素(减去$前缀)。

<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,按原样输出所有其他节点。

相关问题