我有两个xmls
-
a.xml
-
b.xml
以下是a.xml
文件
<ConsumerKeyperUsers>
<CONSUMERKEY_1>
<CONSUMERKEY>QoChiX5FlNyqe1FuqU7VB2M</CONSUMERKEY>
</CONSUMERKEY_1>
<CONSUMERKEY_2>
<CONSUMERKEY>uzGjRVxnxmP00zrnZPtB9sM</CONSUMERKEY>
</CONSUMERKEY_2>
<CONSUMERKEY_3>
<CONSUMERKEY>RD37oO2jg4rlerxW5p6f76</CONSUMERKEY>
</CONSUMERKEY_3>
</ConsumerKeyperUsers>
下面是B.xml
<Details>
<Detail>
<ConsumeyKey>QoChiX5FlNyqe1FuqU7VB2M</ConsumeyKey>
<Place>Ocean</Place>
<City>Urban</City>
</Detail>
<Detail>
<ConsumeyKey>uzGjRVxnxmP00zrnZPtB9sM</ConsumeyKey>
<Place>Road</Place>
<City>Rural</City>
</Detail>
<Detail>
<ConsumeyKey>RD37oO2jg4rlerxW5p6f76</ConsumeyKey>
<Place>Plane</Place>
<City>Semiurban</City>
</Detail>
<Detail>
<ConsumeyKey>likujyhtasasa</ConsumeyKey>
<Place>Ship</Place>
<City>Semirural</City>
</Detail>
</Details>
现在,我想得到如下所示的输出
<ConsumerKeyperUsers>
<CONSUMERKEY_1>
<CONSUMERKEY>QoChiX5FlNyqe1FuqU7VB2M</CONSUMERKEY>
<Place>Ocean</Place>
<City>Urban</City>
</CONSUMERKEY_1>
<CONSUMERKEY_2>
<CONSUMERKEY>uzGjRVxnxmP00zrnZPtB9sM</CONSUMERKEY>
<Place>Road</Place>
<City>Rural</City>
</CONSUMERKEY_2>
<CONSUMERKEY_3>
<CONSUMERKEY>RD37oO2jg4rlerxW5p6f76</CONSUMERKEY>
<Place>Plane</Place>
<City>Semiurban</City>
</CONSUMERKEY_3>
</ConsumerKeyperUsers>
我对xslt很陌生。任何 xslt 版本都可以使用。
此外,我还想知道如何将这两个xml提供给xslt。我们是否必须将其存储在xslt可以访问这些文件的服务器中。
这方面的任何指导都会非常有帮助。
谢谢
我还想知道如何将这两个 xml 提供给 xslt。
告诉应用程序处理a.xml
文件,并将 b.xml
的路径作为参数传递。
使用键从b.xml
中查找数据 - 例如(未经测试(:
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:strip-space elements="*"/>
<xsl:param name="file2" select="'b.xml'" />
<xsl:key name="details" match="Detail" use="ConsumeyKey" />
<xsl:template match="/ConsumerKeyperUsers">
<xsl:copy>
<xsl:for-each select="*">
<xsl:copy>
<xsl:copy-of select="CONSUMERKEY"/>
<xsl:variable name="details" select="key('details', CONSUMERKEY, document($file2))" />
<xsl:copy-of select="$details/Place"/>
<xsl:copy-of select="$details/City"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用 XSLT 1.0 处理器执行相同的操作稍微复杂一些:
XSLT 1.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:strip-space elements="*"/>
<xsl:param name="file2" select="'b.xml'" />
<xsl:key name="details" match="Detail" use="ConsumeyKey" />
<xsl:template match="/ConsumerKeyperUsers">
<xsl:copy>
<xsl:for-each select="*">
<xsl:copy>
<xsl:variable name="key" select="CONSUMERKEY" />
<xsl:copy-of select="$key"/>
<!-- switch context to file2 in order to use key -->
<xsl:for-each select="document($file2)">
<xsl:variable name="details" select="key('details', $key)" />
<xsl:copy-of select="$details/Place"/>
<xsl:copy-of select="$details/City"/>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
可以使用以下 XSLT-1.0 样式表实现目标:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" />
<xsl:variable name="bDoc" select="document('b.xml')/Details" />
<xsl:strip-space elements="*" />
<xsl:template match="/ConsumerKeyperUsers">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(local-name(),'CONSUMERKEY_')]">
<xsl:variable name="keyValue" select="CONSUMERKEY/text()" />
<xsl:element name="{local-name()}">
<xsl:copy-of select="$bDoc/Detail[ConsumeyKey/text()=$keyValue]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
其输出为:
<?xml version="1.0"?>
<ConsumerKeyperUsers>
<CONSUMERKEY_1>
<Detail>
<ConsumeyKey>QoChiX5FlNyqe1FuqU7VB2M</ConsumeyKey>
<Place>Ocean</Place>
<City>Urban</City>
</Detail>
</CONSUMERKEY_1>
<CONSUMERKEY_2>
<Detail>
<ConsumeyKey>uzGjRVxnxmP00zrnZPtB9sM</ConsumeyKey>
<Place>Road</Place>
<City>Rural</City>
</Detail>
</CONSUMERKEY_2>
<CONSUMERKEY_3>
<Detail>
<ConsumeyKey>RD37oO2jg4rlerxW5p6f76</ConsumeyKey>
<Place>Plane</Place>
<City>Semiurban</City>
</Detail>
</CONSUMERKEY_3>
</ConsumerKeyperUsers>
但是,您的命名方案不是最佳的,因为在 CONSUMERKEY_...
元素的末尾添加一个数字是一种不好的做法。您应该避免CONSUMERKEY
元素的名称。这还将简化处理此 XML 所需的模板。