[英]Two XML documents xpath xslt mapping
嘗試了幾個小時才能使它正常工作,但我一定錯過了一些東西。
我想通過兩個文檔中的ID將兩個XML文檔映射在一起。 我可以使用一個XSLT從兩個文檔中獲取數據輸出,但是我不知道如何映射它們。
第一個xml:
...
<member>
<id>1</id>
<name>John</name>
</member>
<member>
<id>2</id>
<name>Otto</name>
</member>
...
第二個xml:(ss名稱空間)
...
<row>
<cell ss:Type="String">id</cell>
<cell ss:Type="String">Number 1</cell>
<cell ss:Type="String">Number 2</cell>
</row>
<row>
<cell ss:Type="Number">1</cell>
<cell ss:Type="Number">1231312313</cell>
<cell ss:Type="Number">234234234342</cell>
</row>
<row>
<cell ss:Type="Number">2</cell>
<cell ss:Type="Number">4353453453</cell>
<cell ss:Type="Number">345345345455</cell>
</row>
...
我遍歷XSLT文件中的名稱和id元素,並且在此循環中,我嘗試使用Apply模板獲取值以映射到第二個xml中的id。
最終的HTML輸出應該是這個樣子:
Id Name Number 1 Number 2
1 John 1231312313 234234234342
2 Otto 4353453453 345345345455
也許以下可能是一個起點:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:ss="NS:SS">
<xsl:template match="/">
<html>
<table>
<xsl:apply-templates/>
</table>
</html>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="ss:row">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="ss:cell">
<td><xsl:value-of select="."/></td>
</xsl:template>
<xsl:template match="ss:cell[1]">
<xsl:variable name="id" select="string()"/>
<td><xsl:value-of select="$id"/></td>
<!-- Especially for large amounts of data, making use of key() would be better than "//"
The key() call can be wrapped inside a <xsl:for-each select="document('firstDocument.xml')>
that changes the context to the other document -->
<td><xsl:value-of select="document('firstDocument.xml')//member[id=$id]/name"/></td>
</xsl:template>
</xsl:stylesheet>
這必須應用於您稱為第二個文檔的位置。 使用document()
函數調用第一個文檔。
正如Martin Honnen指出的那樣,使用key()
會更好(特別是如果您有大數據集的話)。 請參閱有關將對key()
的調用包裝到<xsl:for-each>
的注釋,以使它在XSLT 1.0中起作用。
我用調用模板“函數”解決了它,並將id的值作為參數傳遞。 然后使用for-each和if語句在第二個文件中拾取“ Number”標簽值。 有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.