根据孙节点的检测创建新节点
Creating new node based on detection of grand-child node
任何人都可以帮助我解决我的以下要求。
如果 publisher/catalogue/cd11/year 是 available/exist 那么需要创建一个具有值的新元素 publisher/catalogue/cd22 = 'New Release'
提前致谢
输入XML:
<publisher>
<Name id="d123">
<Location>Chicago</Location>
</Name>
<catalogue id="d1" >
<cd11 id="d2">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<year>1985</year>
</cd11>
</catalogue>
<catalogue id="d3" >
<cd11 id="d4">
<title>Jurassic World</title>
<artist>Chris Pratt</artist>
</cd11>
</catalogue>
</publisher>
转换:如果 'year' 元素存在于 publisher/catalogue/cd11 路径
中,则应创建 <cd22>New Release</cd22>
输出XML:
<publisher>
<Name id="d123">
<Location>Chicago</Location>
</Name>
<catalogue id="d1" >
<cd11 id="d2">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<year>1985</year>
</cd11>
<cd22>New Release</cd22>
</catalogue>
<catalogue id="d3" >
<cd11 id="d4">
<title>Jurassic World</title>
<artist>Chris Pratt</artist>
</cd11>
</catalogue>
</publisher>
只需使用 identity transform,然后覆盖(匹配)所有具有后代 cd11/year
的 catalogue
元素。
在我的示例中,我检查 year
是否不为空,但如果重要,您可以更改它。
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="catalogue[string(cd11/year)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<cd22>New Release</cd22>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
任何人都可以帮助我解决我的以下要求。 如果 publisher/catalogue/cd11/year 是 available/exist 那么需要创建一个具有值的新元素 publisher/catalogue/cd22 = 'New Release'
提前致谢
输入XML:
<publisher>
<Name id="d123">
<Location>Chicago</Location>
</Name>
<catalogue id="d1" >
<cd11 id="d2">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<year>1985</year>
</cd11>
</catalogue>
<catalogue id="d3" >
<cd11 id="d4">
<title>Jurassic World</title>
<artist>Chris Pratt</artist>
</cd11>
</catalogue>
</publisher>
转换:如果 'year' 元素存在于 publisher/catalogue/cd11 路径
中,则应创建<cd22>New Release</cd22>
输出XML:
<publisher>
<Name id="d123">
<Location>Chicago</Location>
</Name>
<catalogue id="d1" >
<cd11 id="d2">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<year>1985</year>
</cd11>
<cd22>New Release</cd22>
</catalogue>
<catalogue id="d3" >
<cd11 id="d4">
<title>Jurassic World</title>
<artist>Chris Pratt</artist>
</cd11>
</catalogue>
</publisher>
只需使用 identity transform,然后覆盖(匹配)所有具有后代 cd11/year
的 catalogue
元素。
在我的示例中,我检查 year
是否不为空,但如果重要,您可以更改它。
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="catalogue[string(cd11/year)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<cd22>New Release</cd22>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>