我正在嘗試使用給定 xml 檔案的 XSL 生成查找表。
我有一個定期更改的資料集,我需要將此資料用作另一個 XSL (2.0) 腳本中的查找表。我目前在我的 XSL 腳本中硬編碼了查找表,但是由于資料發生了變化,因此更新腳本很痛苦。
我想將此查找表生成為外部檔案并從我的其他腳本中參考它。
帶有用于生成查找表的資料的 xml 如下所示:
<doc>
<BO>
<RTS>
<Code>001</Code>
<Val>74</Val>
</RTS>
<RTS>
<Code>002</Code>
<Val>111</Val>
</RTS>
<!-- Lots more -->
<BO>
<doc>
當前的查找表(即嵌入的)如下所示:
<xsl:variable name="lookup" >
<row Code="001" Val="74"/>
<row Code="002" Val="111"/>
<!-- Lots more -->
</xsl:variable>
我不確定外部檔案查找表是否需要采用不同的格式(?)。只要我可以生成查找表并能夠從我當前的腳本中使用它就可以了。
編輯:澄清一下,(我是 XSL 的新手)我正在尋找有關如何在我的 XSL 中使用輸入 xml 作為查找表的指導。
uj5u.com熱心網友回復:
我正在尋找有關如何在我的 XSL 中使用輸入 xml 作為查找表的指導。
您沒有發布您的 XSL 或您的 XML 輸入。考慮以下示例,大致基于您之前的問題:
XML
<input>
<object>
<code>002</code>
</object>
<object>
<code>001</code>
</object>
</input>
查找.xml
<doc>
<BO>
<RTS>
<Code>001</Code>
<Val>74</Val>
</RTS>
<RTS>
<Code>002</Code>
<Val>111</Val>
</RTS>
<!-- Lots more -->
</BO>
</doc>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="lookup-path">lookup.xml</xsl:param>
<xsl:key name="lookup" match="RTS" use="Code"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="object">
<xsl:copy>
<xsl:copy-of select="code"/>
<xsl:copy-of select="key('lookup', code, document($lookup-path))/Val"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
結果
<?xml version="1.0" encoding="UTF-8"?>
<input>
<object>
<code>002</code>
<Val>111</Val>
</object>
<object>
<code>001</code>
<Val>74</Val>
</object>
</input>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/493596.html
