2015-08-24 20 views
0

第一個文件XML1.xmlXSL模板來獲取價值的子節點的具體位置在XML

<?xml version="1.0" encoding="ISO-8859-1"?> 
<akn> 
    <meta> 
     <identification source="#blah"> 
      <FRBRWork> 
       <FRBRdate date="2011-11-29" name="Generation"/> 
      </FRBRWork> 
      <FRBRExpression> 123 </FRBRExpression>      
     </identification> 
    </meta> 
    <preface> 345</preface> 
    <content> 456</content> 
</akn> 

二XML文件 'XML2.xml'

<response> 
    <objects type="list"> 
     <object> 
      <name>Anders</name> 
      <number> 98 </number> 
      <address>SCS</address> 
     </object> 
     <object> 
      <name>Anders</name> 
      <number> 98 </number> 
      <address>PQR</address> 
     </object> 
     <object> 
      <name>peter</name> 
      <number> 58 </number> 
      <address>ACS</address> 
     </object> 
    </objects> 
</response> 

我的xls文件是如下圖所示:

<?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:akn="http://docs.oasis-open.org/" 
    exclude-result-prefixes="xs xd" version="2.0"> 

    <!--creating shortcut to get acess cListenerOutput.xml file--> 
    <xsl:variable name="XML2Output" 
     select="document('/src/main/resources/XML2.xml')/objects"/> 

     <!-- For stripping the whitespaces in the output xml file --> 
     <xsl:strip-space elements="*"/> 

     <!-- For defining the output of the transformation --> 
     <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="ISO-8859-1"/> 
     <xsl:template match="/"> 
      <xsl:copy-of select="//akn:meta"/> 
      <header> 
       <docProponent> 
        <xsl:call-template name="cListener1"/> 
       </docProponent> 
      </header> 
     </xsl:template> 

     <xsl:template name = "cListener1"> 
      <xsl:for-each select="$XML2Output//object"> 
       <xsl:if test="position() = 1"> 
        <xsl:value-of select="address"/> 
       </xsl:if> 
      </xsl:for-each> 
     </xsl:template> 
    </xsl:stylesheet> 

雖然,XSL文件正確地進行第一操作(提取的節點),其不會提取值fr om第二個文件中的對象的第一個實例。

我也嘗試添加

<xsl:template name = "cListener1" match = "$cListenerOutput/object"> 

但是這將引發一個錯誤

Error in expression $cListenerOutput/object: Unexpected token in pattern, found $  

可有人點我到正確的方式使用xml:變量在模板中?

回答

1

您需要在文檔變量聲明供應的XPath:

<xsl:variable name="XML2Output" 
     select="document('/src/main/resources/XML2.xml')/response/objects"/> 

憑藉那張變化,輸出XML是(希望)如您所願:

<header xmlns:akn="http://docs.oasis-open.org/"> 
    <docProponent>SCS</docProponent> 
</header>