【问题标题】:How to load base64 encoded text into an XML document using XSLT?如何使用 XSLT 将 base64 编码的文本加载到 XML 文档中?
【发布时间】:2013-05-02 00:28:31
【问题描述】:

如何使用 XSLT 将 base64 编码的文本加载到 XML 文档中?

例如,如果我有以下两个文件

输入文件 1:

YTM0NZomIzI2OTsmIzM0NTueYQ==

输入文件 2:

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

想要的输出:

<xml>
<Column1>YTM0NZomIzI2OTsmIzM0NTueYQ==</Column1>
</xml>

【问题讨论】:

  • SO 的工作方式是您先尝试解决问题,然后在遇到困难时提出具体问题,展示您已完成的工作。请阅读FAQHow to Ask 以获取有关编写问题的提示。

标签: xslt xslt-1.0 xslt-2.0


【解决方案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>

【讨论】:

  • 收到此错误:javax.xml.transform.TransformerException: 找不到函数:未解析文本请帮忙?
  • 听起来您正在使用 XSLT 1.0 引擎。 unparsed-text() 是 2.0 唯一的函数。要么使用支持 2.0 的引擎(如 Saxon),要么尝试我的 1.0 解决方案
猜你喜欢
  • 2020-11-22
  • 2010-12-18
  • 1970-01-01
  • 2012-06-16
  • 2019-12-17
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多