[英]Transform XML using XSLT
I want to transform the XML using XSLT Here is my sample XML我想使用 XSLT 转换 XML 这是我的示例 XML
<EmployeList>
<EMpDetails>
<Name>Kiran</Name>
<ID>ID001</ID>
<city>Hyderabad</city>
<Country>India</Country>
</EMpDetails>
<EMpDetails>
<Name>Sunny</Name>
<ID>ID002</ID>
<city>Banglore</city>
<Country>INDIA</Country>
</EMpDetails>
<EMpDetails>
<Name>John</Name>
<ID>ID001</ID>
<city>TEXAS</city>
<Country>US</Country>
</EMpDetails>
<EMpDetails>
<Name>Raj</Name>
<ID>ID006</ID>
<city>Dallas</city>
<Country>US</Country>
</EMpDetails>
<EMpDetails>
<Name>Nag</Name>
<ID>ID007</ID>
<city>ITALY</city>
<Country>Rome</Country>
</EMpDetails>
</EmployeList>
Required output using XSLT需要 output 使用 XSLT
<EmployeList>
<EMpDetails>
<Name>Kiran</Name>
<ID>ID001</ID>
<city>Hyderabad</city>
<Country>India</Country>
</EMpDetails>
<EMpDetails>
<Name>Sunny</Name>
<ID>ID002</ID>
<city>Banglore</city>
<Country>INDIA</Country>
</EMpDetails>
</EmployeList>
Assuming that the question asks to output only EMpDetails
whose Country
child has a string (case-insensitive) value of "India"
:假设问题只询问 output 的
EMpDetails
,其Country
子项的字符串(不区分大小写)值为"India"
:
I. This XSLT 1.0 transformation :一、本XSLT 1.0改造:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"EMpDetails
[not(translate(Country,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz'
)
='india'
)
]
"/>
</xsl:stylesheet>
when applied on the provided XML document :当应用于提供的 XML 文档时:
<EmployeList>
<EMpDetails>
<Name>Kiran</Name>
<ID>ID001</ID>
<city>Hyderabad</city>
<Country>India</Country>
</EMpDetails>
<EMpDetails>
<Name>Sunny</Name>
<ID>ID002</ID>
<city>Banglore</city>
<Country>INDIA</Country>
</EMpDetails>
<EMpDetails>
<Name>John</Name>
<ID>ID001</ID>
<city>TEXAS</city>
<Country>US</Country>
</EMpDetails>
<EMpDetails>
<Name>Raj</Name>
<ID>ID006</ID>
<city>Dallas</city>
<Country>US</Country>
</EMpDetails>
<EMpDetails>
<Name>Nag</Name>
<ID>ID007</ID>
<city>ITALY</city>
<Country>Rome</Country>
</EMpDetails>
</EmployeList>
produces the wanted, correct result :产生想要的正确结果:
<EmployeList>
<EMpDetails>
<Name>Kiran</Name>
<ID>ID001</ID>
<city>Hyderabad</city>
<Country>India</Country>
</EMpDetails>
<EMpDetails>
<Name>Sunny</Name>
<ID>ID002</ID>
<city>Banglore</city>
<Country>INDIA</Country>
</EMpDetails>
</EmployeList>
II.二、 This XSLT 2.0 transformation:
此 XSLT 2.0 改造:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"EMpDetails[not(upper-case(Country)='INDIA')]"/>
</xsl:stylesheet>
again produces the same, wanted and correct result .再次产生相同的、想要的和正确的结果。
Explanation : Overriding the identity rule with a template matching all unwanted EMpDetails
elements.解释:使用匹配所有不需要的
EMpDetails
元素的模板覆盖身份规则。 This template has empty body, which effectively prevents copying any such matched elements to the output.此模板为空体,可有效防止将任何此类匹配的元素复制到 output。
Remember : Using and overriding the identity rule/template is the most fundamental and most powerful XSLT design pattern.请记住:使用和覆盖身份规则/模板是最基本和最强大的 XSLT 设计模式。
That should work as you need (assuming that you want all EMpDetails
elements where Country
is equal to India
with case-insensitivity):这应该可以按您的需要工作(假设您想要
Country
等于India
且不区分大小写的所有EMpDetails
元素):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="EmployeList">
<EmployeList>
<xsl:copy-of select="EMpDetails[lower-case(Country) = 'india']"/>
</EmployeList>
</xsl:template>
</xsl:stylesheet>
Assuming your sample input is simple as it's shown you could also go with:假设您的示例输入很简单,因为它显示您还可以使用 go :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/EmployeList">
<xsl:copy>
<xsl:copy-of select="EMpDetails[
Country[
.='India'
or .='INDIA']
]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.