Xml XSLT添加重复节点的新实例
我是XSLT新手,正在寻找向现有列表中添加重复节点/元素的方法。下面是一个xml示例。如何使用XSLT添加狗或猫的新实例Xml XSLT添加重复节点的新实例,xml,xslt,Xml,Xslt,我是XSLT新手,正在寻找向现有列表中添加重复节点/元素的方法。下面是一个xml示例。如何使用XSLT添加狗或猫的新实例 `<?xml version="1.0" encoding="UTF-16"?> <Animals> <Cats> <Cat> <Name>Felix</Name> <Color>Orange</Color>
`<?xml version="1.0" encoding="UTF-16"?>
<Animals>
<Cats>
<Cat>
<Name>Felix</Name>
<Color>Orange</Color>
<Gender>Male</Gender>
<Age>5</Age>
</Cat>
<Cat>
<Name>Fluffy</Name>
<Color>White</Color>
<Gender>Female</Gender>
<Age>4</Age>
</Cat>
<Cat>
<Name>Shadow</Name>
<Color>Black</Color>
<Gender>Female</Gender>
<Age>2</Age>
</Cat>
</Cats>
<Dogs>
<Dog>
<Name>Spot</Name>
<Color>White/Brown/Black</Color>
<Gender>Male</Gender>
<Age>11</Age>
</Dog>
<Dog>
<Name>Rocky</Name>
<Color>Black</Color>
<Gender>Male</Gender>
<Age>8</Age>
</Dog>
<Dog>
<Name>Goldie</Name>
<Color>Gold</Color>
<Gender>Female</Gender>
<Age>3</Age>
</Dog>
</Dogs>
</Animals>`
`
费利克斯
橙色
男性
5.
毛茸茸的
白色
女性
4.
影子
黑色
女性
2.
斑点
白色/棕色/黑色
男性
11
多石的
黑色
男性
8.
戈尔迪
黄金
女性
3.
`
如果每次都要手动更新xslt。。。您可以使用类似的方法,您需要根据需要不断添加Dog/Cat节点
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/Animals/Cats/Cat">
<Cat>
<Name>New</Name>
<Color>New</Color>
<Gender>New</Gender>
<Age>500</Age>
</Cat>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Animals/Dogs/Dog">
<Dog>
<Name>New</Name>
<Color>New</Color>
<Gender>New</Gender>
<Age>500</Age>
</Dog>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
新的
新的
新的
500
新的
新的
新的
500
我将手动更新XSLT并使用它来更新XML 有了新的数据 我认为这不是一个好方法,但如果您愿意,可以很容易地做到: 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="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Cats">
<xsl:copy>
<xsl:apply-templates/>
<Cat>
<Name>New Cat</Name>
<Color>New Cat's Color</Color>
<Gender>New Cat's Gender</Gender>
<Age>New Cat's Age</Age>
</Cat>
</xsl:copy>
</xsl:template>
<xsl:template match="Dogs">
<xsl:copy>
<xsl:apply-templates/>
<Dog>
<Name>New Dog</Name>
<Color>New Dog's Color</Color>
<Gender>New Dog's Gender</Gender>
<Age>New Dog's Age</Age>
</Dog>
</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="*"/>
<!-- path to the additions document -->
<xsl:param name="additions" select="'additions.xml'"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Cats">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="document($additions)/Animals/Cats/Cat"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Dogs">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="document($additions)/Animals/Dogs/Dog"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
新猫
新猫的颜色
新猫的性别
新猫的年龄
新狗
新狗的颜色
新狗的性别
新狗的年龄
更智能的方法是使用带有附加数据的外部XML文档,例如: Additions.xml
<Animals>
<Cats>
<Cat>
<Name>New Cat</Name>
<Color>New Cat's Color</Color>
<Gender>New Cat's Gender</Gender>
<Age>New Cat's Age</Age>
</Cat>
</Cats>
<Dogs>
<Dog>
<Name>New Dog</Name>
<Color>New Dog's Color</Color>
<Gender>New Dog's Gender</Gender>
<Age>New Dog's Age</Age>
</Dog>
</Dogs>
</Animals>
新猫
新猫的颜色
新猫的性别
新猫的年龄
新狗
新狗的颜色
新狗的性别
新狗的年龄
并让样式表将其与处理后的XML输入中的数据相结合:
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="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Cats">
<xsl:copy>
<xsl:apply-templates/>
<Cat>
<Name>New Cat</Name>
<Color>New Cat's Color</Color>
<Gender>New Cat's Gender</Gender>
<Age>New Cat's Age</Age>
</Cat>
</xsl:copy>
</xsl:template>
<xsl:template match="Dogs">
<xsl:copy>
<xsl:apply-templates/>
<Dog>
<Name>New Dog</Name>
<Color>New Dog's Color</Color>
<Gender>New Dog's Gender</Gender>
<Age>New Dog's Age</Age>
</Dog>
</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="*"/>
<!-- path to the additions document -->
<xsl:param name="additions" select="'additions.xml'"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Cats">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="document($additions)/Animals/Cats/Cat"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Dogs">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="document($additions)/Animals/Dogs/Dog"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
欢迎来到SO!你读过哪些没有意义的东西,或者尝试过哪些不起作用的东西?新狗或猫的属性来自哪里?我将手动更新XSLT,并使用它用新数据更新XML。目前,我的公司有一个类似的XML,我们将其更新为向自定义shell添加快捷方式。有些位置需要其他位置不需要的其他快捷方式,因此目前我们有几个相同XML的版本。这很难管理,因为下一次全局更新将删除所述XML的“定制”版本。我们希望使用XSLT来管理这一点,但我们是一家小公司,我们中没有人对XSLT有任何经验。这为每个现有的Cat
添加了一个新的Cat
!谢谢,这很好用。你就是那个人!或者女人,你可以是女人。英雄联盟