我当前的 XML 如下:
<?xml version="1.0" encoding="utf-8"?>
<Rowsets DateCreated="2012-07-17T11:57:07" EndDate="2012-07-17T11:57:07" StartDate="2012-07-17T10:57:07" Version="12.0.12 Build(9)">
<Rowset>
<Row>
<CategoryID>1</CategoryID>
<Name>Philip</Name>
<City>London</City>
<Phone>123</Phone>
</Row>
<Row>
<CategoryID>2</CategoryID>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<CategoryID>3</CategoryID>
<Name>Bruke</Name>
<City>LosAngeles</City>
<Phone>600</Phone>
</Row>
<Row>
<CategoryID>2</CategoryID>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
<Row>
<CategoryID>3</CategoryID>
<Name>Cristina</Name>
<City>SanJose</City>
<Phone>890</Phone>
</Row>
<Row>
<CategoryID>4</CategoryID>
<Name>Meredith</Name>
<City>Sunnyvale</City>
<Phone>788</Phone>
</Row>
<Row>
<CategoryID>4</CategoryID>
<Name>Grey</Name>
<City>MountainView</City>
<Phone>456</Phone>
</Row>
<Row>
<CategoryID>5</CategoryID>
<Name>Torrence</Name>
<City>SAntaClara</City>
<Phone>432</Phone>
</Row>
</Rowset>
</Rowsets>
现在我只想要那些<Row>
为<CategoryID>
2 的人。所以我的 XSLT 如下:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:java="http://xml.apache.org/xslt/java" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="java" version="1.0">
<xsl:output media-type="text/xml" method="xml"/>
<!-- Filters refdoc based on condition and data -->
<xsl:template match="/">
<Rowsets>
<xsl:for-each select="/Rowsets/Rowset">
<Rowset>
<xsl:copy-of select="Columns"/>
<xsl:for-each select="Row[CategoryID = '2']">
<xsl:copy-of select="."/>
</xsl:for-each>
</Rowset>
</xsl:for-each>
</Rowsets>
</xsl:template>
</xsl:stylesheet>
这个 XSLT 为我提供了以下 XML:
<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset>
<Row>
<CategoryID>2</CategoryID>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<CategoryID>2</CategoryID>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
</Rowset>
</Rowsets>
但现在我的要求是,在上面新转换的 XML 中,我还想将 CategoryID 更改为 1。
所以我最终的 XML 应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset>
<Row>
<CategoryID>1</CategoryID>
<Name>Derek</Name>
<City>Seattle</City>
<Phone>500</Phone>
</Row>
<Row>
<CategoryID>1</CategoryID>
<Name>Yang</Name>
<City>SFO</City>
<Phone>1233</Phone>
</Row>
</Rowset>
</Rowsets>
我不确定如何使用相同的 XSLT 以最佳方式实现这一点。有人可以帮我吗?