【发布时间】:2021-09-14 10:57:56
【问题描述】:
我找到了多个关于 xsl:for-each 的教程、文章、帖子等,但似乎没有一个与我的用例相匹配,所以如果这已经在其他地方得到解答,我们深表歉意。
我有以下示例 XML(有多个文档项):
<doc>
<att>
<PRef>52545125</PRef>
<BlobContent Name="rtf" ID="A7A9348763A111EA887800505685156C">
<Property Name="MimeType" Value="application/rtf"/>
<Property Name="FileName" Value="P0001A__m.rtf"/>
<Reference Link="Directory\export_819280402327681192_P0001A__m.rtf"
Type="RELATIVEFILE"/>
</BlobContent>
<InvoiceDate>01/01/1970</InvoiceDate>
<NetAmount>100.75</NetAmount>
<GrossAmount/>
<BlobContent Name="tif" ID="A7A90D7663A111EA887800505685156C">
<Property Name="MimeType" Value="image/tiff"/>
<Property Name="FileName" Value="P0001A__m.tif"/>
<Reference Link="Directory\export_4747308861722121077_P0001A__m.tif"
Type="RELATIVEFILE"/>
</BlobContent>
</att>
我正在尝试从每个 BlobContent 中更改以下内容
<Reference Link="Directory\export_4747308861722121077_P0001A__m.tif" Type="RELATIVEFILE"/>
读作:
<rtf>Directory\export_819280402327681192_P0001A__m.rtf</rtf>
<tif>Directory\export_4747308861722121077_P0001A__m.tif</tif>
以下 xsl 将按预期工作,但将数据放入标签中,无需定义这是 tif 还是 rtf:
<?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"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/doc/att/BlobContent/Reference">
<xsl:variable name="element-name" select="@Link"/>
<xsl:element name="Link">
<xsl:value-of select="@Link"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我尝试添加一个 for-each 以根据 BlobContent 名称创建新标签,但收到一个错误,指出 {tif rtf} 是无效的 QName,因此它似乎在拾取每个值,但将它们分组为一个元素名称。
谢谢
【问题讨论】:
-
tif或rtf的值应该从哪里来?