2017-12-11 24 views
0

xmlファイルの表示と評価にxslファイルを使用したいと考えています。 XMLファイルには、タイトル、説明、イメージ、および金額のある部品が含まれています。しかし、この金額は、XSLファイルで宣言するいくつかの変数に依存します。しかし、この場合は、XSLファイルのXML内から式を計算する必要があります。これには道がありますか?変数付きのXSL評価属性

例を参照してください:(@amountの式は、XMLからの変数を評価する必要があります)

XML:

<partslist>   
<part value="1" amount="$variable1" visible="true">  
    <title> 
     <nl>Onderdeel 1</nl> 
     <fr>Partie 1</fr> 
     <en>Part 1</en> 
     <de>Teil 1</de> 
    </title>  
    <image src="images/partslist/part1.jpg"/> 
</part>  
<part value="2" amount="$variable1 * $variable2" visible="true">   
    <title> 
     <nl>Onderdeel 2</nl> 
     <fr>Partie 2</fr> 
     <en>Part 2</en> 
     <de>Teil 2</de> 
    </title>  
    <image src="images/partslist/part2.jpg"/> 
</part> 
<part value="3" amount="$variable3" visible="true">  
    <title> 
     <nl>Onderdeel 3</nl> 
     <fr>Partie 3</fr> 
     <en>Part 3</en> 
     <de>Teil 3</de> 
    </title>  
    <image src="images/partslist/part3.jpg"/> 
</part> 
<part value="4" amount="$variable1 + $variable3" visible="true">   
    <title> 
     <nl>Onderdeel 4</nl> 
     <fr>Partie 4</fr> 
     <en>Part 4</en> 
     <de>Teil 4</de> 
    </title>  
    <image src="images/partslist/part4.jpg"/> 
</part> 
</partslist>    

XSL:事前

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
<xsl:template match="/"> 
    <html> 
     <head> 
      <link rel="stylesheet" href="style.css" type="text/css"/> 
     </head> 
     <body> 
      <xsl:variable name="variable1" select="1" /> 
      <xsl:variable name="variable2" select="2" /> 
      <xsl:variable name="variable3" select="3" /> 

      <div class="np"> 
       <H2>Parts List</H2> 
       <table border ="1"> 
        <tr> 
         <th style="width:15%;">ID</th> 
         <th style="width:15%;">Amount</th> 
         <th style="width:40%;">Description</th> 
         <th style="width:30%;">Image</th> 
        </tr> 
        <xsl:for-each select="partslist/part"> 
         <tr> 
          <td><xsl:value-of select="@value" /></td> 
          <td><xsl:value-of select="@amount"/></td> 
          <td><xsl:value-of select="title/nl"/></td> 
          <td><img style="width:100%;" src="{image/@src}"/></td> 
         </tr> 
        </xsl:for-each> 
       </table> 
      </div> 
     </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

感謝を

+0

あなたの試行が動作していない場所を正確にそれを明確にすることができますしてください? – Spangen

+0

私は、xpath evalのDimitre answerと同じ原則であなた自身の計算評価関数を作成しなければならないと思う:https://stackoverflow.com/questions/7321553/xslt-interpret-a-text-nodes-value-as- xpath-query-and-it-in-transformati#7328577 – GGO

+0

@Spangen 'amount'列に結果がXML内の式の評価式になるようにしたいが、それは動作していない。たとえば、最初の列は 'value = 1、amount = 1(variable1 = 1であるため)、title = Onderdeel1、image) – Pecoris

答えて

0

EXSLTをサポートするXalanインタプリタのようなXSLTプロセッサを使用する場合dyn:evaluate機能(http://exslt.org/dyn/functions/evaluate/index.html)あなたが

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:dyn="http://exslt.org/dynamic" exclude-result-prefixes="dyn"> 
<xsl:template match="/"> 
    <html> 
     <head> 
      <link rel="stylesheet" href="style.css" type="text/css"/> 
     </head> 
     <body> 
      <xsl:variable name="variable1" select="1" /> 
      <xsl:variable name="variable2" select="2" /> 
      <xsl:variable name="variable3" select="3" /> 

      <div class="np"> 
       <H2>Parts List</H2> 
       <table border ="1"> 
        <tr> 
         <th style="width:15%;">ID</th> 
         <th style="width:15%;">Amount</th> 
         <th style="width:40%;">Description</th> 
         <th style="width:30%;">Image</th> 
        </tr> 
        <xsl:for-each select="partslist/part"> 
         <tr> 
          <td><xsl:value-of select="@value" /></td> 
          <td><xsl:value-of select="dyn:evaluate(@amount)"/></td> 
          <td><xsl:value-of select="title/nl"/></td> 
          <td><img style="width:100%;" src="{image/@src}"/></td> 
         </tr> 
        </xsl:for-each> 
       </table> 
      </div> 
     </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

http://xsltransform.hikmatu.com/948Fn5dを使用することができます。

XSLTプロセッサによっては、dyn:evaluateのサポートがなくても、拡張機能を使用して機能を実装することができます。

またはxsl:evaluateをサポートXSLTプロセッサ3に移動:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math" 
    version="3.0"> 

    <xsl:template match="/"> 
     <html> 
      <head> 
       <link rel="stylesheet" href="style.css" type="text/css"/> 
      </head> 
      <body> 
       <xsl:variable name="variable1" select="1"/> 
       <xsl:variable name="variable2" select="2"/> 
       <xsl:variable name="variable3" select="3"/> 

       <div class="np"> 
        <H2>Parts List</H2> 
        <table border="1"> 
         <tr> 
          <th style="width:15%;">ID</th> 
          <th style="width:15%;">Amount</th> 
          <th style="width:40%;">Description</th> 
          <th style="width:30%;">Image</th> 
         </tr> 
         <xsl:for-each select="partslist/part"> 
          <tr> 
           <td> 
            <xsl:value-of select="@value"/> 
           </td> 
           <td> 
            <xsl:evaluate xpath="@amount" context-item="." 
             with-params=" 
              map { 
               xs:QName('variable1'): $variable1, 
               xs:QName('variable2'): $variable2, 
               xs:QName('variable3'): $variable3 
              }" 
            /> 
           </td> 
           <td> 
            <xsl:value-of select="title/nl"/> 
           </td> 
           <td> 
            <img style="width:100%;" src="{image/@src}"/> 
           </td> 
          </tr> 
         </xsl:for-each> 
        </table> 
       </div> 
      </body> 
     </html> 
    </xsl:template> 

</xsl:stylesheet> 
+0

である必要があります。 "あなたのXSLTプロセッサによっては、dyn:evaluateがサポートされていなくても、拡張機能を使ってその機能を実装できるかもしれません。" この機能を正確に実装するにはどうすればよいですか? – Pecoris

+0

どのプラットフォームにどのXSLTプロセッサを使用していますか? 'XslCompiledTransform'はC#やVB.NETを使ってhttps://msdn.microsoft.com/en-us/library/system.xmlを呼び出すことができるなど、実装されているプログラミング言語やフレームワークを呼び出すことができます。 .xpath.xpathnavigator.evaluate(v = vs.110).aspx。 –

+0

私は確かにC#でXslCompiledTransformを使用しています。しかし、この場合、evaluateメソッドをどのように呼び出すのですか?あなたの助けを前もってありがとう! – Pecoris