第一個要更新的 xml 檔案:
<Conf key="11" title="tit">
<Item key="1" >
<Lock con="E0" />
<Vol title="te" description="de">
<All con="1" title="uu" />
<Widget title="11" description="11" />
</Vol>
<Am title="re" description="de">
<Hold con="50" />
<Widget title="22" description="22" />
</Am>
</Item>
<Item key="2" title="tit">
<Lock const="E0" />
<Vol title="ty">
<All con="1" title="fg" />
<Widget title="33" description="33" />
</Vol>
<Am title="gh" description="gh">
<Hold const="50" />
<Widget title="44" description="44" />
</Am>
</Item>
</Conf>
第二個 xml 檔案“updates.xml”從中獲取資料以更新第一個檔案:
<Profile title="u">
<Item key="1">
<Vol>
<Widget title="AA"/>
</Vol>
<Am>
<Widget title="BB" description="BB" />
</Am>
</Item>
<Item key="2">
<Vol>
<Widget title="CC" description="CC" />
</Vol>
<Am>
<Widget title="Dd" description="DD" />
</Am>
</Item>
</Profile>
update.xml 檔案與輸入檔案的不同之處在于它僅包含那些是 Widget 標記的祖先的標記并且缺少一些屬性。在 XSLT1.0 轉換之后,輸入樹被轉移到輸出樹,并替換了“updates.xml”檔案中的 Widget 標記。如果輸入檔案中有 Widget 標簽,但“updates.xml”檔案中沒有,則將輸入檔案中的 Widget 標簽傳輸到輸出樹。現在轉換檔案:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
exclude-result-prefixes="exslt"
version="1.0">
<xsl:output method="xml"/>
<xsl:param name="updates" select="document('updates.xml')"/>
<xsl:key name="replacement" match="Widget" use="local-name(parent::*)"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Widget">
<xsl:variable name="parentName" select="local-name(parent::*)"/>
<xsl:variable name="replacement">
<xsl:for-each select="$updates">
<xsl:copy-of select="key('replacement', $parentName)"/>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="exslt:node-set($replacement)/*">
<xsl:copy-of select="$replacement"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
轉換檔案后:
<?xml version="1.0" encoding="utf-8"?>
<Conf key="11" title="tit">
<Item key="1">
<Lock con="E0" />
<Vol title="te" description="de">
<All con="1" title="uu" />
<Widget title="AA" />
<Widget title="CC" description="CC" />
</Vol>
<Am title="re" description="de">
<Hold con="50" />
<Widget title="BB" description="BB" />
<Widget title="Dd" description="DD" />
</Am>
</Item>
<Item key="2" title="tit">
<Lock const="E0" />
<Vol title="ty">
<All con="1" title="fg" />
<Widget title="AA" />
<Widget title="CC" description="CC" />
</Vol>
<Am title="gh" description="gh">
<Hold const="50" />
<Widget title="BB" description="BB" />
<Widget title="Dd" description="DD" />
</Am>
</Item>
</Conf>
如何確保在 XSLT1.0 轉換后,輸出檔案沒有多余的 Widget 標簽,輸出檔案如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Conf key="11" title="tit">
<Item key="1">
<Lock con="E0" />
<Vol title="te" description="de">
<All con="1" title="uu" />
<Widget title="AA" />
</Vol>
<Am title="re" description="de">
<Hold con="50" />
<Widget title="BB" description="BB" />
</Am>
</Item>
<Item key="2" title="tit">
<Lock const="E0" />
<Vol title="ty">
<All con="1" title="fg" />
<Widget title="CC" description="CC" />
</Vol>
<Am title="gh" description="gh">
<Hold const="50" />
<Widget title="Dd" description="DD" />
</Am>
</Item>
</Conf>
uj5u.com熱心網友回復:
我會以這種方式處理它,并且不需要使用exslt:node-set()擴展功能。
創建一個使用父元素@key的Item和 的 local-name()復合鍵。Widget使用該復合鍵值Widget從用于將背景關系切換到檔案的$updates中查找對應的值。xs:for-eachupdates.xml
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
exclude-result-prefixes="exslt"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="updates" select="document('updates.xml')"/>
<xsl:key name="replacement" match="Widget" use="concat(../../@key, local-name(..))"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Widget">
<xsl:variable name="key" select="concat(../../@key, local-name(..))"/>
<xsl:variable name="replacement">
<xsl:for-each select="$updates">
<xsl:copy-of select="key('replacement', $key)"/>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="$replacement">
<xsl:copy-of select="$replacement"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/476303.html
下一篇:如何只選擇頂級標簽?
