1

XSLT を使用して XML を変換しています。webdocument タイプのノードを持つ 1 つの webdocuments ノードを持つ 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&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>

要約すると、私の結果は、ノード値を取得し、それを使用して別の値を生成することです。

前もって感謝します

4

2 に答える 2

0

最初に注意すべきことは、この行のために、XML が整形式ではないことです。

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

ここ&でエスケープする必要があります。本当はこんな感じのはず

<filePath>http://url.com/param1=$hash&amp;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&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 ID テンプレートを使用して、他のすべてのノードをそのまま出力することに注意してください。

于 2013-10-30T23:46:19.847 に答える