xml - 通过 xslt 将超链接添加到 excel 单元格

标签 xml excel xslt

我有一个 xml 文件和一个 xslt 样式表文件。

我如何通过特定的 xml 字段仅向 xslt 文件中的特定 excel 单元格添加超链接。

我有以下伪代码,但不知道如何在 xslt 文件中设置它。

if (xmlAttribute =="Beleg")
{   href = "http://www.xyz.de/beleg.php?beleg=$fieldvalue
}
else if (xmlAttribute == "Lieferant")
{  href = "http://www.xyz.de/lieferant.php?lieferant=$fieldvalue
}
else
{  // no link
}

谁能帮帮我。

这是我的 xml 和 xslt

XML:

<xml>
 <s:Schema id="RowsetSchema" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<s:ElementType name="row" content="eltOnly">
<s:AttributeType name="ART">
  <s:datatype dt:type="string" dt:maxLength="3" rs:maybenull="false" /> 
  </s:AttributeType>
<s:AttributeType name="BELEG">
  <s:datatype dt:type="string" dt:maxLength="63" rs:maybenull="false" /> 
  </s:AttributeType>
<s:AttributeType name="BELEGPOSITION">
  <s:datatype dt:type="i2" /> 
  </s:AttributeType>
<s:AttributeType name="BESTELLUNGSDATUM">
  <s:datatype dt:type="dateTime" /> 
  </s:AttributeType>
<s:AttributeType name="VERURSACHERARTIKEL">
  <s:datatype dt:type="string" dt:maxLength="22" rs:maybenull="false" /> 
  </s:AttributeType>
<s:AttributeType name="WIEDERBESCHAFFUNGSZEIT">
  <s:datatype dt:type="i2" /> 
  </s:AttributeType>
<s:AttributeType name="LIEFERANT">
  <s:datatype dt:type="string" dt:maxLength="15" /> 
  </s:AttributeType>
<s:AttributeType name="LIEFERANTENNAME">
  <s:datatype dt:type="string" dt:maxLength="63" /> 
  </s:AttributeType>
<s:AttributeType name="BEARBEITER">
  <s:datatype dt:type="string" dt:maxLength="50" /> 
  </s:AttributeType>
<s:AttributeType name="VERURSACHER">
  <s:datatype dt:type="string" dt:maxLength="19" /> 
  </s:AttributeType>
<s:AttributeType name="VERURSACHERPOSITION">
  <s:datatype dt:type="string" dt:maxLength="31" /> 
  </s:AttributeType>
<s:AttributeType name="ERSTELLER">
  <s:datatype dt:type="string" dt:maxLength="31" /> 
  </s:AttributeType>
<s:AttributeType name="ENDE">
  <s:datatype dt:type="dateTime" /> 
  </s:AttributeType>
<s:AttributeType name="BEDARFSTERMIN">
  <s:datatype dt:type="dateTime" /> 
  </s:AttributeType>
<s:AttributeType name="LIEFERTERMIN">
  <s:datatype dt:type="dateTime" /> 
  </s:AttributeType>
<s:AttributeType name="VERSPAETUNG">
  <s:datatype dt:type="int" /> 
  </s:AttributeType>
<s:AttributeType name="UNTERSCHREITUNG">
  <s:datatype dt:type="int" /> 
  </s:AttributeType>
  <s:extends type="rs:rowbase" /> 
  </s:ElementType>
  </s:Schema>
<rs:data xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
  <z:row ART="ZB" BELEG="982831" BELEGPOSITION="4" BESTELLUNGSDATUM="2012-08-27T00:00:00" VERURSACHERARTIKEL="A16" WIEDERBESCHAFFUNGSZEIT="28" LIEFERANT="123" LIEFERANTENNAME="XXXcOMPANY" BEARBEITER="HarryPotter" VERURSACHER="0.123" VERURSACHERPOSITION="470" ERSTELLER="Max Mustermann" ENDE="2012-12-20T10:10:00" BEDARFSTERMIN="2011-12-19T00:00:00" LIEFERTERMIN="2011-12-31T00:00:00" VERSPAETUNG="-36" UNTERSCHREITUNG="46" /> 
  <z:row ART="ZB" BELEG="982838" BELEGPOSITION="4" BESTELLUNGSDATUM="2012-08-27T00:00:00" VERURSACHERARTIKEL="B16" WIEDERBESCHAFFUNGSZEIT="28" LIEFERANT="134" LIEFERANTENNAME="XXXcOMPANY" BEARBEITER="HarryPotter" VERURSACHER="0.234" VERURSACHERPOSITION="472" ERSTELLER="Max Mustermann" ENDE="2012-12-20T10:10:00" BEDARFSTERMIN="2012-12-19T00:00:00" LIEFERTERMIN="2013-12-31T00:00:00" VERSPAETUNG="-376" UNTERSCHREITUNG="86" /> 
  </rs:data>
</xml>

XSLT 特定部分:

<Row>
<xsl:for-each select="../../s:Schema/s:ElementType/s:AttributeType">
<Cell>
<xsl:variable name="v" select="$x/@*[name()=current()/@name]" />
<xsl:choose>
<xsl:when test="string-length($v)=0">
</xsl:when>
<xsl:when test="s:datatype[@dt:type='dateTime']">
<Data ss:Type="DateTime">
<xsl:value-of select="$v" />
</Data>
</xsl:when>
<xsl:otherwise>
<Data ss:Type="String">
<xsl:value-of select="$v" />
</Data>
</xsl:otherwise>
</xsl:choose>
</Cell>
</xsl:for-each>
</Row>

最佳答案

如果您想了解如何在 Excel 电子表格 XML 中执行某些操作,那么最好的查找方法就是询问 Excel 本身!进入 Excel,在单元格中输入 URL 并将其保存为 XML 电子表格格式。然后在记事本(或您选择的文本编辑器)中打开保存的文件。希望你应该看到这样的东西

<Cell ss:StyleID="s62" ss:HRef="http://stackoverflow.com/">
   <Data ss:Type="String">http://stackoverflow.com/</Data>
</Cell>

因此,您在 XSLT 中需要做的就是确保将 ss:HRef 属性添加到 Cell 元素(在创建 Data 之前 元素)。像这样

<xsl:otherwise>
   <xsl:choose>
      <xsl:when test="$attribute = 'Beleg'">
          <xsl:attribute name="HRef">
              <xsl:text>http://www.xyz.de/beleg.php?beleg=<xsl:text>
              <xsl:value-of select="$fieldvalue" />
          </xsl:attribute>
      </xsl:when>
   </xsl:choose>
   <Data ss:Type="String">
      <xsl:value-of select="$v" />
   </Data>
</xsl:otherwise>

显然,您必须确保将 $attribute 设置为您希望检查的属性的值,但它应该能为您提供大致的思路。

编辑:您可能需要确保您设置的 URL 是绝对地址(以“http://”开头)而不是相对地址(以“../”开头)

关于xml - 通过 xslt 将超链接添加到 excel 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425284/

相关文章:

excel - 访问 VBA 创建不合格的引用,同时通过 excel 工作表循环

xslt - 如何在一个 xsl 中使用另一个 xsl 中的函数

xslt - 如何为 heat.exe 收集的文件创建快捷方式?

xml - Golang 解码 MusicXML

vba - 如何将颜色应用于 xlHairline

xml - 如何定义 XSD 元素具有多个子元素,所有子元素都很简单并且只有属性?

vba - 运行时错误 91 对象变量或未设置变量

xml - 根据 XSLT 中的条件增加值

xml - XML 命名空间有什么用?

C# - 当 webRequest 发送 XML 时,将 Base64 安全为 Gif?