我从今天早上开始尝试使用xslt在xml文件中添加值。 所以基本上我有这个xml文件1.xml
<?xml version="1.0" encoding="UTF-8"?>
<Orderfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<order>
<establishmenthour>10:38:00</ establishmenthour>
<ExpirationDate/>
<acc/>
<identification>170610009-01</identification>
</order>
<order>
<establishmenthour>10:40:00</ establishmenthour>
<ExpirationDate/>
<acc/>
<identification>170610910-03</identification>
</order>
<order>
<establishmenthour>10:42:00</ establishmenthour>
<ExpirationDate/>
<acc/>
<identification>170610015-01</identification>
</order>
我从第二个名为2.xml
的xml文件中获取了这些信息<?xml version="1.0" encoding="UTF-8" ?>
<Orderfile>
<order>
<identification>170610009-01</identification>
<ExpirationDate>2017-06-21</ExpirationDate>
</order>
<order>
<identification>170610015-01</identification>
<ExpirationDate>2017-02-22</ExpirationDate>
</order>
<order>
<identification>170610024-01</identification>
<ExpirationDate>2017-08-02</ExpirationDate>
</order>
</Orderfile>
我希望有 - &gt; merged.xml
<?xml version="1.0" encoding="UTF-8"?>
<Orderfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<order>
<establishmenthour>10:38:00</establishmenthour>
<ExpirationDate>2017-06-21</ExpirationDate/>
<acc/>
<identification>170610009-01</identification>
</order>
<order>
<establishmenthour>10:40:00</ establishmenthour>
<ExpirationDate/>
<acc/>
<identification>170610910-03</identification>
</order>
<order>
<establishmenthour>10:42:00</ establishmenthour>
<ExpirationDate>2017-02-22</ExpirationDate/>
<acc/>
<identification>170610015-01</identification>
</order>
我想阅读texte文件,并且对于与xml文件匹配的任何“id”,我都会将twill添加到期日。
这就是我的尝试:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="version">
<xsl:copy>
<xsl:apply-templates select="*"/>
<xsl:apply-templates select="document('2.xml')/Orderfile/order/*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但那不起作用你可以帮助我。
答案 0 :(得分:2)
以下是使用X unparsed-text
读取XSLT 2.0以及tokenize
解析文本文件中数据的方法:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:param name="text-uri" select="'input.txt'"/>
<xsl:param name="lines" select="tokenize(unparsed-text($text-uri), '\r?\n')"/>
<xsl:param name="data" select="for $line in $lines return tokenize($line, ';')"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="order[id = $data[position() mod 2 = 1]]/ExpirationDate">
<xsl:copy>
<xsl:value-of select="$data[index-of($data, current()/../id) + 1]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XSLT 2.0受到各种工具的支持,如oXygen,Stylus Studio,Altova XMLSpy以及Saxon 9,XmlPrime,Exselt等独立处理器。
现在您已经将问题更改为使用两个XML输入文档,您可以按如下方式使用XSLT 1.0:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="data-uri" select="'input2.xml'"/>
<xsl:param name="data-doc" select="document($data-uri)"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ExpirationDate">
<xsl:copy>
<xsl:variable name="match" select="$data-doc//order[identification = current()/../identification]/ExpirationDate"/>
<xsl:choose>
<xsl:when test="$match">
<xsl:value-of select="$match"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>