首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用其他节点信息创建节点值

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

Stack Overflow用户
提问于 2013-10-30 22:41:20
回答 2查看 145关注 0票数 1

我正在使用XSLT转换XML,并且我有一个带有webdocument类型节点的xml节点

代码语言:javascript
复制
<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 )。

代码语言:javascript
复制
<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>

总而言之

代码语言:javascript
复制
<filePath>http://url.com/param1=1c1bc9f96349fc954cba2dfb58f214b1&param2=808924

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

代码语言:javascript
复制
<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>

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

提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-30 23:20:46

这应该是你需要的。没有真正的使用concat()的必要,您可以使用连续的xsl:value-ofxsl:text节点来实现您在这里所需的功能。另外,尝试并使用设计“推送”模板使用xsl:apply-templates,而不是使用xsl:for-each的“拉”模板。

代码语言:javascript
复制
<?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>

给予:

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2013-10-30 23:46:19

首先要注意的是,您的XML格式不太好,因为这一行。

代码语言:javascript
复制
<filePath>http://url.com/param1=$hash&param2=$id</filePath>

&需要在这里逃脱。它真的应该像这样

代码语言:javascript
复制
<filePath>http://url.com/param1=$hash&amp;param2=$id</filePath>

无论如何,为了回答您的问题,您似乎希望使用同名的元素替换filePath中的“变量”,使用来自XML的实际值。

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

代码语言:javascript
复制
<xsl:analyze-string select="." regex="\$\w+">

在此范围内,您可以指定在找到“匹配-子字符串”时要做什么。在本例中,您希望找到与您刚刚匹配的变量同名的元素(减去$prefix)。

代码语言:javascript
复制
<xsl:matching-substring>
    <xsl:value-of select="$WebDocument/*[local-name() = substring(current(), 2)]"/>
</xsl:matching-substring>

(在本例中,$WebDocument是指向当前WebDocument元素的变量)

对于“非匹配”字符串,只需按如下方式输出

代码语言:javascript
复制
  <xsl:non-matching-substring>
     <xsl:value-of select="current()"/>
  </xsl:non-matching-substring>

试试这个XSLT

代码语言:javascript
复制
<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>

这应该会输出以下内容

代码语言:javascript
复制
<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标识模板,以输出所有其他节点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19695200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档