2013-05-07 46 views
0

如何使用XSLT將base64編碼文本加載到XML文檔中?如何使用XSLT將base64編碼文本加載到XML文檔中?

例如,如果我有以下兩個文件

輸入文件1:

YTM0NZomIzI2OTsmIzM0NTueYQ== 

輸入文件2:

<xml> 
<Column1></Column1> 
</xml> 

期望的輸出:

<xml> 
<Column1>YTM0NZomIzI2OTsmIzM0NTueYQ==</Column1> 
</xml> 
+0

的方式,以便工作是你嘗試首先解決問題,然後在遇到困難時詢問具體問題,並說明你所做的事情。請閱讀[常見問題]和[問]提供寫作問題的提示。 – 2013-05-07 22:17:43

回答

1

如果您正在使用XSLT 2.0,您可以使用unparsed-text()函數加載從文本文件中的Base64內容。

在下面的示例中,xsl:param設置爲文檔URI的默認值,但可以在調用轉換時設置不同的值。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output indent="yes"/> 

    <xsl:param name="base64-document" select="'base64-content.txt'"/> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Column1"> 
      <xsl:copy> 
      <xsl:value-of select="unparsed-text($base64-document)"/> 
      </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

如果您不能使用XSLT 2.0,那麼在XSLT 1.0,你可以使用第三方的XML文件與實體參照的base64文本文件,包括它的第三個XML文件中的內容。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output indent="yes"/> 

<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Column1"> 
     <xsl:copy> 
     <xsl:value-of select="document('thirdFile.xml')/*"/> 
     </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

你也可以閱讀的base64文本文件的內容(XSLT的上下文之外)併發送內容爲xsl:param值:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output indent="yes"/> 

<xsl:param name="base64-content" /> 

<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Column1"> 
     <xsl:copy> 
     <xsl:value-of select="$base64-content"/> 
     </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

非常感謝。我會嘗試這段代碼,並讓你知道,如果有更多的查詢.. – 2013-05-09 16:12:53

+0

獲取此錯誤:javax.xml.transform.TransformerException:找不到函數:unparsed-text 請幫忙? – 2013-05-13 17:30:34

+0

聽起來像您正在使用XSLT 1.0引擎。 unparsed-text()是一個2.0版本的函數。要麼使用2.0功能的引擎(如撒克遜)或嘗試我的1.0解決方案 – 2013-05-13 18:27:46

相關問題