Windows 使用[MS]XSLT脚本修改XML节点
我想选择一个节点,并使用 xsl:script函数。此外,还应提供与该节点的子节点匹配的模板 仍然执行它们的工作(在脚本处理完节点之后)Windows 使用[MS]XSLT脚本修改XML节点,windows,xml,xslt,scripting,Windows,Xml,Xslt,Scripting,我想选择一个节点,并使用 xsl:script函数。此外,还应提供与该节点的子节点匹配的模板 仍然执行它们的工作(在脚本处理完节点之后) 可以使用XSLT完成吗 你能为这种转换提供一个示例/框架吗 是的,可以做到。我似乎看不出问题出在哪里,因为XSL脚本的XML(或任何输出)是独立于其输入进行缓冲的 下面的示例说明了这一点,其中一个简单的XSL脚本主要按原样复制输入XML文档,并更改了一些内容: 根元素名称和属性 通过从层次结构中删除元素来展开 删除结果/日期元素 重命名项目的“源”属性“源”
是的,可以做到。我似乎看不出问题出在哪里,因为XSL脚本的XML(或任何输出)是独立于其输入进行缓冲的 下面的示例说明了这一点,其中一个简单的XSL脚本主要按原样复制输入XML文档,并更改了一些内容:
- 根元素名称和属性
- 通过从层次结构中删除元素来展开
- 删除结果/日期元素
- 重命名项目的“源”属性“源”
- 更改项目的“级别”属性值
- 重命名item元素的FirstName和LastName元素
<?xml version="1.0" encoding="ISO-8859-1"?>
<MyRoot version="1.2">
<results>
<info>Alpha Bravo</info>
<author>Employee No 321</author>
<date/>
<item source="www" level="6" cost="33">
<FirstName>Jack</FirstName>
<LastName>Frost</LastName>
<Date>1998-10-30</Date>
<Organization>Lemon growers association</Organization>
</item>
<item source="db-11" level="1" cost="65" qry="routine 21">
<FirstName>Mike</FirstName>
<LastName>Black</LastName>
<Date>2006-10-30</Date>
<Organization>Ford Motor Company</Organization>
</item>
</results>
</MyRoot>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#default">
<xsl:template match="MyRoot">
<xsl:call-template name="MainTemplate">
</xsl:call-template>
</xsl:template>
<xsl:template name="MainTemplate">
<MyNewRoot version="0.1">
<xsl:copy-of select="results/author" />
<xsl:copy-of select="results/info" />
<xsl:for-each select="results/item">
<xsl:call-template name="FixItemElement"/>
</xsl:for-each>
</MyNewRoot>
</xsl:template>
<xsl:template name="FixItemElement">
<xsl:copy>
<xsl:copy-of select="@*[not(name()='source' or name()='level')]" />
<xsl:attribute name="origin">
<xsl:value-of select="@source"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="77"/>
</xsl:attribute>
<xsl:for-each select="descendant::*">
<xsl:choose>
<xsl:when test="local-name(.) = 'FirstName'">
<GivenName>
<xsl:value-of select="."/>
</GivenName>
</xsl:when>
<xsl:when test="local-name(.) = 'LastName'">
<FamilyName>
<xsl:value-of select="."/>
</FamilyName>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
阿尔法布拉沃
雇员编号321
杰克
霜冻
1998-10-30
柠檬种植者协会
迈克
黑色
2006-10-30
福特汽车公司
产出
<?xml version="1.0" encoding="utf-16"?>
<MyNewRoot version="0.1">
<author>Employee No 321</author>
<info>Alpha Bravo</info>
<item cost="33" origin="www" level="77">
<GivenName>Jack</GivenName>
<FamilyName>Frost</FamilyName>
<Date>1998-10-30</Date>
<Organization>Lemon growers association</Organization>
</item>
<item cost="65" qry="routine 21" origin="db-11" level="77">
<GivenName>Mike</GivenName>
<FamilyName>Black</FamilyName>
<Date>2006-10-30</Date>
<Organization>Ford Motor Company</Organization>
</item>
</MyNewRoot>
雇员编号321
阿尔法布拉沃
杰克
霜冻
1998-10-30
柠檬种植者协会
迈克
黑色
2006-10-30
福特汽车公司
XSL脚本
<?xml version="1.0" encoding="ISO-8859-1"?>
<MyRoot version="1.2">
<results>
<info>Alpha Bravo</info>
<author>Employee No 321</author>
<date/>
<item source="www" level="6" cost="33">
<FirstName>Jack</FirstName>
<LastName>Frost</LastName>
<Date>1998-10-30</Date>
<Organization>Lemon growers association</Organization>
</item>
<item source="db-11" level="1" cost="65" qry="routine 21">
<FirstName>Mike</FirstName>
<LastName>Black</LastName>
<Date>2006-10-30</Date>
<Organization>Ford Motor Company</Organization>
</item>
</results>
</MyRoot>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#default">
<xsl:template match="MyRoot">
<xsl:call-template name="MainTemplate">
</xsl:call-template>
</xsl:template>
<xsl:template name="MainTemplate">
<MyNewRoot version="0.1">
<xsl:copy-of select="results/author" />
<xsl:copy-of select="results/info" />
<xsl:for-each select="results/item">
<xsl:call-template name="FixItemElement"/>
</xsl:for-each>
</MyNewRoot>
</xsl:template>
<xsl:template name="FixItemElement">
<xsl:copy>
<xsl:copy-of select="@*[not(name()='source' or name()='level')]" />
<xsl:attribute name="origin">
<xsl:value-of select="@source"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="77"/>
</xsl:attribute>
<xsl:for-each select="descendant::*">
<xsl:choose>
<xsl:when test="local-name(.) = 'FirstName'">
<GivenName>
<xsl:value-of select="."/>
</GivenName>
</xsl:when>
<xsl:when test="local-name(.) = 'LastName'">
<FamilyName>
<xsl:value-of select="."/>
</FamilyName>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
是的,可以做到。我似乎看不出问题出在哪里,因为XSL脚本的XML(或任何输出)是独立于其输入进行缓冲的 下面的示例说明了这一点,其中一个简单的XSL脚本主要按原样复制输入XML文档,并更改了一些内容:
- 根元素名称和属性
- 通过从层次结构中删除元素来展开
- 删除结果/日期元素
- 重命名项目的“源”属性“源”
- 更改项目的“级别”属性值
- 重命名item元素的FirstName和LastName元素
<?xml version="1.0" encoding="ISO-8859-1"?>
<MyRoot version="1.2">
<results>
<info>Alpha Bravo</info>
<author>Employee No 321</author>
<date/>
<item source="www" level="6" cost="33">
<FirstName>Jack</FirstName>
<LastName>Frost</LastName>
<Date>1998-10-30</Date>
<Organization>Lemon growers association</Organization>
</item>
<item source="db-11" level="1" cost="65" qry="routine 21">
<FirstName>Mike</FirstName>
<LastName>Black</LastName>
<Date>2006-10-30</Date>
<Organization>Ford Motor Company</Organization>
</item>
</results>
</MyRoot>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#default">
<xsl:template match="MyRoot">
<xsl:call-template name="MainTemplate">
</xsl:call-template>
</xsl:template>
<xsl:template name="MainTemplate">
<MyNewRoot version="0.1">
<xsl:copy-of select="results/author" />
<xsl:copy-of select="results/info" />
<xsl:for-each select="results/item">
<xsl:call-template name="FixItemElement"/>
</xsl:for-each>
</MyNewRoot>
</xsl:template>
<xsl:template name="FixItemElement">
<xsl:copy>
<xsl:copy-of select="@*[not(name()='source' or name()='level')]" />
<xsl:attribute name="origin">
<xsl:value-of select="@source"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="77"/>
</xsl:attribute>
<xsl:for-each select="descendant::*">
<xsl:choose>
<xsl:when test="local-name(.) = 'FirstName'">
<GivenName>
<xsl:value-of select="."/>
</GivenName>
</xsl:when>
<xsl:when test="local-name(.) = 'LastName'">
<FamilyName>
<xsl:value-of select="."/>
</FamilyName>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
阿尔法布拉沃
雇员编号321
杰克
霜冻
1998-10-30
柠檬种植者协会
迈克
黑色
2006-10-30
福特汽车公司
产出
<?xml version="1.0" encoding="utf-16"?>
<MyNewRoot version="0.1">
<author>Employee No 321</author>
<info>Alpha Bravo</info>
<item cost="33" origin="www" level="77">
<GivenName>Jack</GivenName>
<FamilyName>Frost</FamilyName>
<Date>1998-10-30</Date>
<Organization>Lemon growers association</Organization>
</item>
<item cost="65" qry="routine 21" origin="db-11" level="77">
<GivenName>Mike</GivenName>
<FamilyName>Black</FamilyName>
<Date>2006-10-30</Date>
<Organization>Ford Motor Company</Organization>
</item>
</MyNewRoot>
雇员编号321
阿尔法布拉沃
杰克
霜冻
1998-10-30
柠檬种植者协会
迈克
黑色
2006-10-30
福特汽车公司
XSL脚本
<?xml version="1.0" encoding="ISO-8859-1"?>
<MyRoot version="1.2">
<results>
<info>Alpha Bravo</info>
<author>Employee No 321</author>
<date/>
<item source="www" level="6" cost="33">
<FirstName>Jack</FirstName>
<LastName>Frost</LastName>
<Date>1998-10-30</Date>
<Organization>Lemon growers association</Organization>
</item>
<item source="db-11" level="1" cost="65" qry="routine 21">
<FirstName>Mike</FirstName>
<LastName>Black</LastName>
<Date>2006-10-30</Date>
<Organization>Ford Motor Company</Organization>
</item>
</results>
</MyRoot>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#default">
<xsl:template match="MyRoot">
<xsl:call-template name="MainTemplate">
</xsl:call-template>
</xsl:template>
<xsl:template name="MainTemplate">
<MyNewRoot version="0.1">
<xsl:copy-of select="results/author" />
<xsl:copy-of select="results/info" />
<xsl:for-each select="results/item">
<xsl:call-template name="FixItemElement"/>
</xsl:for-each>
</MyNewRoot>
</xsl:template>
<xsl:template name="FixItemElement">
<xsl:copy>
<xsl:copy-of select="@*[not(name()='source' or name()='level')]" />
<xsl:attribute name="origin">
<xsl:value-of select="@source"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="77"/>
</xsl:attribute>
<xsl:for-each select="descendant::*">
<xsl:choose>
<xsl:when test="local-name(.) = 'FirstName'">
<GivenName>
<xsl:value-of select="."/>
</GivenName>
</xsl:when>
<xsl:when test="local-name(.) = 'LastName'">
<FamilyName>
<xsl:value-of select="."/>
</FamilyName>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
提供一个示例XML,说明您希望转换的内容和应用的规则,这将使我们更容易提供示例解决方案。提供一个示例XML,说明您希望转换的内容和应用的规则,这将使我们更容易提供示例解决方案。很抱歉,在发布示例代码之前,Israel Chen延迟了很长时间。虽然她没有给我那么多的分数,但我妻子今天给了我一个长长的蜜月清单;-)上面的XSL是XSLT 1.0,但它与2.0一样简单。我坚持使用1.0,因为一些框架仍在赶超……感谢您的回复,这很有帮助。@mjv:恐怕您错过了整个
部分?您的是香草XSLT,不涉及脚本。@Tomalak。是的,我做到了![完全错过了OP问题的部分…]。哼现在我看到了这个要求,我不知道该如何回应。我对xsl:script的唯一接触是使用扩展函数,这些扩展函数仅重新格式化变量等(然后将这些变量返回给XSLT,以便包含在结果树中)。您知道允许扩展直接干扰结果树的技术吗?除了明显的无知之外,我倾向于避开那些非常依赖XSLT处理器实现的特性,但我仍然很好奇……很抱歉,在发布示例代码之前,Israel Chen耽搁了很长时间。虽然她没有给我那么多的分数,但我妻子今天给了我一个长长的蜜月清单;-)上面的XSL是XSLT 1.0,但它与2.0一样简单。我坚持使用1.0,因为一些框架仍在赶超……感谢您的回复,这很有帮助。@mjv:恐怕您错过了整个
部分?您的是香草XSLT,不涉及脚本。@Tomalak。是的,我做到了![完全错过了OP问题的部分…]。哼现在我看到了这个要求,我不知道该如何回应。我对xsl:script的唯一接触是使用扩展函数,这些扩展函数仅重新格式化变量等(然后将这些变量返回给XSLT,以便包含在结果树中)。您知道允许扩展直接干扰结果树的技术吗?