使用XSLT将新节点添加到现有XML的特定位置
我的原始XML:使用XSLT将新节点添加到现有XML的特定位置,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我的原始XML: <customers> <client> <custnum>1</custnum> <name>John</name> </client> <client> <custnum>2</custnum> <name>Mary</name> <
<customers>
<client>
<custnum>1</custnum>
<name>John</name>
</client>
<client>
<custnum>2</custnum>
<name>Mary</name>
</client>
</customers>
1.
约翰
2.
玛丽
其他XML(updates.XML)
1.
67890
2.
12345
3.
11111
市场
期望输出
<customers>
<client>
<custnum>1</custnum>
<name>John</name>
<ssn>67890</ssn>
</client>
<client>
<custnum>2</custnum>
<name>Mary</name>
<ssn>12345</ssn>
</client>
</customers>
1.
约翰
67890
2.
玛丽
12345
目前,我正在使用从Stackoverflow中找到的以下XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" />
<xsl:key name="cust" match="something" use="custnum" />
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="client">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<xsl:variable name="myId" select="custnum" />
<xsl:for-each select="document('updates.xml')">
<!-- process all transactions with the right ID -->
<xsl:apply-templates select="key('cust', $myId)" />
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="something/custnum" />
</xsl:stylesheet>
它产生
<customers>
<client>
<custnum>1</custnum>
<name>John</name>
<something>
<ssn>67890</ssn>
</something>
</client>
<client>
<custnum>2</custnum>
<name>Mary</name>
<something>
<ssn>12345</ssn>
</something>
</client>
</customers>
1.
约翰
67890
2.
玛丽
12345
如何在不使用另一个XSL处理结果的情况下去掉
标记?它应该相当简单,但对我来说就是不点击。如果您只想获取ssn
,那么为什么不只获取ssn
XSLT1.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:key name="cust" match="something" use="custnum" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="client">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<xsl:variable name="myId" select="custnum" />
<xsl:for-each select="document('updates.xml')">
<xsl:apply-templates select="key('cust', $myId)/ssn" />
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<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:key name="cust" match="something" use="custnum" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="client">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<xsl:variable name="myId" select="custnum" />
<xsl:for-each select="document('updates.xml')">
<xsl:apply-templates select="key('cust', $myId)/ssn" />
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>