如何通过xsl创建节点? [英] how create nodes by xsl?
问题描述
两个XML文档,第一个主要输入文档是
Two XML documents, the first primary input document being
第一个文档
First Document
<catalog>
<cd>
<package>A</package>
<id>1</id>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<package>A</package>
<id>1</id>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<package>B</package>
<id>1</id>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<package>B</package>
<id>2</id>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<package>B</package>
<id>3</id>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<package>A</package>
<id>2</id>
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<package>A</package>
<id>3</id>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<package>A</package>
<id>4</id>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1990</year>
</cd>
<cd>
<package>A</package>
<id>2</id>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
</catalog>
映射旧ID的第二个文档
the second document mapping the old ids to new ones as in
第二份文件
Second Document
<catalog>
<ids>
<package>A</package>
<id>1</id>
<newId>A-1001</newId>
</ids>
<ids>
<package>A</package>
<id>2</id>
<newId>A-1002</newId>
</ids>
<ids>
<package>A</package>
<id>3</id>
<newId>A-1003</newId>
</ids>
<ids>
<package>A</package>
<id>4</id>
<newId>A-1004</newId>
</ids>
<ids>
<package>B</package>
<id>1</id>
<newId>B-1001</newId>
</ids>
<ids>
<package>B</package>
<id>2</id>
<newId>B-1002</newId>
</ids>
<ids>
<package>B</package>
<id>3</id>
<newId>B-1003</newId>
</ids>
<ids>
<package>B</package>
<id>4</id>
<newId>B-1005</newId>
</ids>
<ids>
<package>C</package>
<id>1</id>
<newId>C-1005</newId>
</ids>
<ids>
<package>D</package>
<id>2</id>
<newId>D-1005</newId>
</ids>
</catalog>
作为输出你只想复制第一个输入但是替换id值然后下面的样式表显示
and as the output you simply want to copy the first input but replace the id values then the following stylesheet shows
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="id-url" select="'idmap.xml'"/>
<xsl:variable name="id-doc" select="document($id-url)"/>
<xsl:key name="k1" match="catalog/ids" use="concat(package, '|', id)"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="cd/id">
<xsl:copy>
<xsl:variable name="this" select="."/>
<xsl:for-each select="$id-doc">
<xsl:value-of select="key('k1', concat($this/parent::cd/package, '|', $this))/newId"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出
<catalog>
<cd>
<package>A</package>
<id>A-1001</id>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<package>A</package>
<id>A-1001</id>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<package>B</package>
<id>B-1001</id>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<package>B</package>
<id>B-1002</id>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<package>B</package>
<id>B-1003</id>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<package>A</package>
<id>A-1002</id>
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<package>A</package>
<id>A-1003</id>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<package>A</package>
<id>A-1004</id>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1990</year>
</cd>
<cd>
<package>A</package>
<id>A-1002</id>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
</catalog>
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
The above codes are working fine
The above codes are working fine
But i need to make a new filter in the above xsl
But i need to make a new filter in the above xsl
1. In the 2nd document there is package "C" and "D". Wht i need is . if 2nd document package is not exist in the 1st document create a node in the output like below with <Newid> as <id>
1. In the 2nd document there is package "C" and "D". Wht i need is . if 2nd document package is not exist in the 1st document create a node in the output like below with <Newid> as <id>
<catalog>
-
-
-
-<cd>
<package>A</package>
<id>A-1002</id>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
<cd>
<package>C</package>
<id>C-1005</id>
</cd>
<cd>
<package>D</package>
<id>D-1005</id>
</cd>
</catalog>
推荐答案
Here is an adapted stylesheet:
Here is an adapted stylesheet:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="id-url" select="'idmap.xml'"/>
<xsl:variable name="id-doc" select="document(
id-url)"/>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:variable name="input-doc" select="/"/>
<xsl:key name="k1" match="catalog/ids" use="concat(package, ’|’, id)"/>
<xsl:key name="k2" match="catalog/cd" use="package"/>
<xsl:template match="@* |节点()">
<xsl:copy>
<xsl:apply-templates select="@* |节点()" />
</xsl:copy>
< / xsl:template>
<xsl:template match="catalog">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="
id-url)"/> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <xsl:variable name="input-doc" select="/"/> <xsl:key name="k1" match="catalog/ids" use="concat(package, '|', id)"/> <xsl:key name="k2" match="catalog/cd" use="package"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="catalog"> <xsl:copy> <xsl:apply-templates/> <xsl:apply-templates select="
id-doc/catalog/ids"/>
</xsl:copy>
< / xsl:template>
<xsl:template match="cd/id">
<xsl:copy>
<xsl:variable name="this" select="."/>
<xsl:for-each select="
id-doc/catalog/ids"/> </xsl:copy> </xsl:template> <xsl:template match="cd/id"> <xsl:copy> <xsl:variable name="this" select="."/> <xsl:for-each select="
这篇关于如何通过xsl创建节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!