我有大约相同的项目,这都保存在客户端和服务器数据2个XML文件。 一些数据是相同的,一些属性/子元素是相比于服务器上的客户端不同。
客户端的数据看起来像这样(有更多的属性是无关的比较):
<item id="1" create_dttm="05/28/2010 12:00:00 AM" name="Correct_Name">
<text1>sample</text1>
<icon>iconurl</icon>
</item>
服务器数据看起来像这样(有更多的属性和可能的子元素):
<item type="4" id="1" name="mispelled_name">
</item>
因为对于项目的匹配已经在我们的代码通过ID进行,即做了数据录入的server.xml中的人都不是很小心的名字,留下错别字或占位符名称。 这不会导致错误,但是我宁愿是在安全方面,并确保所有拼写错误的条目在server.xml中通过从client.xml的正确名称进行替换(这些都是双重检查和是正确的)
是否有可能运行一些脚本/代码/ XSLT样式表与来自client.xml的名字在server.xml更换名字?
我不是很熟悉的样式表和不知道从哪里开始与编码类似的东西
基本上,我希望它看起来是这样的:
Read client.xml
Read server.xml
For each item in client.xml, read attributes "id" and "name"
find item with same "id" in server.xml
replace "name" in server.xml with value from client.xml for the item with that "id"
感谢您提供任何帮助
你可以利用文档功能的这里,从第二份文件中查找信息(在你的情况“client.xml的”)将XSLT时server.xml中
例如,你可以定义一个变量一样,包含在client.xml的所有item元素
<xsl:variable name="client" select="document('client.xml')//item" />
然后,以取代在server.xml中的@name属性,你可以创建一个模板来匹配属性,并输出client.xml的值来代替。
<xsl:template match="item/@name">
<xsl:attribute name="name">
<xsl:value-of select="$client[@id=current()/../@id]/@name" />
</xsl:attribute>
</xsl:template>
下面是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="clientXml" select="'client.xml'" />
<xsl:variable name="client" select="document($clientXml)//item" />
<xsl:template match="item/@name">
<xsl:attribute name="name">
<xsl:value-of select="$client[@id=current()/../@id]/@name" />
</xsl:attribute>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
当应用到你的样品client.xml的server.xml中和文件,下面是输出
<item type="4" id="1" name="Correct_Name"></item>
请注意,我已参数化的“client.xml的”文档的名称,因为这将允许您如果需要使用XSLT的不同命名的文件。 你只需要通过第二XML文件作为参数的名称。