通过XSLT在Excel单元格中添加超链接

4

我有一个XML文件和一个XSLT样式表文件。

如何在XSLT文件中通过特定的XML字段仅向特定的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>
1个回答

5
如果你想了解如何在Excel Spreadsheet XML中执行某些操作,最好的方法就是询问Excel本身!进入Excel,在单元格中输入URL并将其保存为XML电子表格格式。然后在记事本(或其他文本编辑器)中打开保存的文件。希望你能看到类似这样的内容。
<Cell ss:StyleID="s62" ss:HRef="http://stackoverflow.com/">
   <Data ss:Type="String">http://stackoverflow.com/</Data>
</Cell>

因此,在您的XSLT中,您需要确保在创建Data元素之前向Cell元素添加一个ss:HRef属性。类似于这样:

<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://”开头),而不是相对地址(以“../”开头)。

我用你的代码示例尝试了一下,但它没有起作用。你有其他想法吗? - mossini
1
可能是因为您正在使用相对URL(以“../”开头的URL),而不是绝对URL(以“http://”开头的URL)。 - Tim C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接