第一個xml檔案in.xml:
<conf title="co" attr01="val01">
<c attr02="val02">
<Item key="db" attr03="val03">
<m1 />
<s>
<m2 />
<Item key="1">
<m3 />
<sp>
<Chart />
</sp>
</Item>
<m4 />
<Item key="2">
<sp>
<m5 />
<Chart />
</sp>
</Item>
<Item key="3">
<sp>
<Chart />
</sp>
</Item>
<m6 />
</s>
</Item>
</c>
</conf>
第二個 xml 檔案 profile.xml,其中包含有關小部件的資訊的第一個檔案:
<p title="profile">
<c>
<Item key="db">
<s>
<Item key="1">
<sp>
<Chart>
<Widget title="widget12">
<Test title="test1"/>
</Widget>
</Chart>
</sp>
</Item>
<Item key="2">
<sp>
<Chart>
<Widget title="widget32">
<Test title="test3"/>
</Widget>
</Chart>
</sp>
</Item>
<Item key="3">
<sp>
<Chart>
<Widget title="widget54">
<Test title="test6"/>
</Widget>
</Chart>
</sp>
</Item>
</s>
</Item>
</c>
</p>
profile.xml 檔案與 in.xml 的不同之處在于它具有不同的根標記名稱,每個 Chart 標記都有一個對應的子 Widget 標記,檔案中僅存在相對于 Chart 標記的父標記,并且除根標記和小部件只有關鍵屬性。
轉換后,生成的檔案與原始檔案的不同之處僅在于 Chart 標簽的 Widget 子標簽:
<conf title="co" attr01="val01">
<c attr02="val02">
<Item key="db" attr03="val03">
<m1 />
<s>
<m2 />
<Item key="1">
<m3 />
<sp>
<Chart>
<Widget title="widget12">
<Test title="test1"/>
</Widget>
</Chart>
</sp>
</Item>
<m4 />
<Item key="2">
<sp>
<m5 />
<Chart>
<Widget title="widget32">
<Test title="test3"/>
</Widget>
</Chart>
</sp>
</Item>
<Item key="3">
<sp>
<Chart>
<Widget title="widget54">
<Test title="test6"/>
</Widget>
</Chart>
</sp>
</Item>
<m6 />
</s>
</Item>
</c>
</conf>
現在有一個不完整的 XSLT1.0 轉換檔案:
<xsl:stylesheet version="1.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="fileName" select="'profile.xml'" />
<xsl:param name="updates" select="document($fileName)" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如何重寫“p”模板XSLT1.0?
uj5u.com熱心網友回復:
嘗試這個:
轉換.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="fileName" select="'profile.xml'" />
<xsl:param name="updates" select="document($fileName)" />
<!--transform starts here on input XML-->
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<!--rules for general nodes()-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--special rule for Chart elements-->
<xsl:template match="Chart">
<xsl:variable name="context" select="."/>
<xsl:if test="$updates//Item/@key = $context/ancestor-or-self::Item[1]/@key">
<xsl:copy-of select="($updates//Item[@key = $context/ancestor-or-self::Item[1]/@key])//Chart"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
輸出.xml
<?xml version="1.0" encoding="UTF-8"?>
<conf title="co" attr01="val01">
<c attr02="val02">
<Item key="db" attr03="val03">
<m1/>
<s>
<m2/>
<Item key="1">
<m3/>
<sp>
<Chart>
<Widget title="widget12">
<Test title="test1"/>
</Widget>
</Chart>
</sp>
</Item>
<m4/>
<Item key="2">
<sp>
<m5/>
<Chart>
<Widget title="widget32">
<Test title="test3"/>
</Widget>
</Chart>
</sp>
</Item>
<Item key="3">
<sp>
<Chart>
<Widget title="widget54">
<Test title="test6"/>
</Widget>
</Chart>
</sp>
</Item>
<m6/>
</s>
</Item>
</c>
</conf>
如果需要,您可以將引數更改為profile.xml其他引數...
uj5u.com熱心網友回復:
不使用密鑰
如果你的屬性@attr01-@attr03 很重要?:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.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="fileName" select="'profile.xml'" />
<xsl:variable name="updates" select="document($fileName)/*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Chart">
<xsl:variable name="idattr01" select="ancestor::*[@attr01]/@attr01"/>
<xsl:variable name="idattr02" select="ancestor::*[@attr02]/@attr02"/>
<xsl:variable name="idattr03" select="ancestor::*[@attr03]/@attr03"/>
<xsl:variable name="key" select="ancestor::*[@key][1]/@key"/>
<xsl:variable name="chartFromUpdate" select="$updates[@attr01=$idattr01]/c[@attr02=$idattr02]/Item[@attr03=$idattr03]/s/Item[@key=$key]/sp/Chart" />
<xsl:choose>
<xsl:when test="$chartFromUpdate">
<xsl:copy-of select="$chartFromUpdate"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
或者你也可以像這樣使用鍵功能:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.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="fileName" select="'profile.xml'" />
<xsl:variable name="updates" select="document($fileName)" />
<xsl:key name="item" match="Item" use="@key"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Chart">
<xsl:variable name="key" select="ancestor::*[@key][1]/@key"/>
<xsl:variable name="chartFromUpdate">
<!-- Since the lookup is in an different document we have to change the context -->
<xsl:for-each select="$updates">
<xsl:copy-of select="key('item', $key)/sp/Chart"/>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="$chartFromUpdate">
<xsl:copy-of select="$chartFromUpdate"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
關于使用 xslt 1.0 在不同檔案上使用鍵功能,請參閱此答案
對于這兩個 xslt,這是輸出:
<?xml version="1.0" encoding="UTF-8"?>
<conf title="co" attr01="val01">
<c attr02="val02">
<Item key="db" attr03="val03">
<m1/>
<s>
<m2/>
<Item key="1">
<m3/>
<sp>
<Chart>
<Widget title="widget12">
<Test title="test1"/>
</Widget>
</Chart>
</sp>
</Item>
<m4/>
<Item key="2">
<sp>
<m5/>
<Chart>
<Widget title="widget32">
<Test title="test3"/>
</Widget>
</Chart>
</sp>
</Item>
<Item key="3">
<sp>
<Chart>
<Widget title="widget54">
<Test title="test6"/>
</Widget>
</Chart>
</sp>
</Item>
<m6/>
</s>
</Item>
</c>
</conf>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/437733.html
