2017-12-11 6 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와 동일한 원리로 독자적인 Calcul eval 함수를 만들어야한다고 생각한다. 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을 사용하고 있습니다. 그러나이 경우 평가 메소드를 어떻게 호출해야합니까? 도와 주셔서 미리 감사드립니다. – Pecoris