[英]merge two xml files using xslt
我想使用xslt將兩個xml文件合並為一個。
file1:
<cut> <content1> .... </content1> </cut>
file1:
<cut> <content2> .... </content2> </cut>
merged:
<cut>
<content1> ... </content1>
<content2> ... </content2>
</cut>
我想將參數傳遞給包含要合並文件的xslt。
xsltproc.exe“ --stringparam file1 s:\\ file1.xml --stringparam file2 s:\\ file2.xml s:\\ merge.xslt
merge.xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="file1"/>
<xsl:param name="file2"/>
<xsl:variable name="big-doc-rtf">
<xsl:copy-of select="document($file1)"/>
<xsl:copy-of select="document($file2)"/>
</xsl:variable>
<xsl:variable name="big-doc" select="exsl:node-set($big-doc-rtf)"/>
<xsl:template match="/">
<cut>
<xsl:apply-templates select="$big-doc/cut/*"/>
</cut>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|text()|comment()|processing-instruction()">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
我只得到一個空的“ cut”標簽。 怎么了?
不使用xsltproc而是xmllint :
(編輯:xsltproc也允許xinclude)
--xinclude:對文檔輸入進行XInclude處理
x1.xml
<cut><content1>content1</content1></cut>
x2.xml
<cut><content2>content2</content2></cut>
x3.xml
<?xml version="1.0"?>
<cut xmlns:xi="http://www.w3.org/2003/XInclude">
<xi:include href="x1.xml" parse="xml" xpointer="xpointer(/cut/content1)"/>
<xi:include href="x2.xml" parse="xml" xpointer="xpointer(/cut/content2)"/>
</cut>
跑:
$ xmllint -xinclude x3.xml
<?xml version="1.0"?>
<cut xmlns:xi="http://www.w3.org/2003/XInclude">
<content1>content1</content1>
<content2>content2</content2>
</cut>
無法重現問題 。
您的代碼中的兩個document()
函數很可能都返回“ nothing”(空)-這意味着用作每個調用的第一個參數的URI不能標識文件(無法找到/解析該文件),或者該文件不能包含格式正確的XML文檔。
這對於xalan和saxon解析器在document()調用中具有硬編碼路徑的情況來說對我來說很好。 問題可能是由於某種原因,您的xsl看不到您的文檔。
我懷疑源文檔中的xml是否存在問題,因為這很可能會引發錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.