![](/img/trans.png)
[英]How to skip some XSLT elements if some XML element does not exist in the definition?
[英]XSLT How to skip some elements during create from attributes
output 日期:
<products>
<group>
<product symbol="LEC-PY1010C" ean="5901436709251" price_pln="29.05" price_eur="6.56" tax="23.0000" stock="1215">
<name>PY1010C Głośnik samochodowy PY-1010C 4" 60W</name>
<images><gfx>https://linktoimage.com/</gfx><gfx>https://linktoimage2.com/</gfx></images>
<category><id>3091</id><name>Akcesoria antenowe</name></category>
</product>
</group>
</products>
XSLT 1.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="*"/>
<xsl:template match="/products">
<xsl:copy>
<xsl:apply-templates select="group/product"/>
</xsl:copy>
</xsl:template>
<xsl:template match="product">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="@symbol">
<symbol>
<xsl:text>CPF-</xsl:text>
<xsl:value-of select="."/>
</symbol>
</xsl:template>
</xsl:stylesheet>
結果:
<products>
<product>
<symbol>CPF-LEC-PY1010C</symbol>
<ean>5901436709251</ean>
<price_pln>29.05</price_pln>
<price_eur>6.56</price_eur>
<tax>23.0000</tax>
<stock>1215</stock>
<name>PY1010C Głośnik samochodowy PY-1010C 4" 60W</name>
<images><gfx>https://linktoimage.com</gfx><gfx>https://linktoimage2.com</gfx></images>
<category><id>2029</id><name>100 mm</name></category>
</product>
</products>
有人有什么主意嗎:
如何刪除標簽<id>
並僅從標簽<name>
中獲取位於元素<category>
之間的文本以獲得結果: <category>100 mm</category>
如何刪除標簽<images>
並僅獲取元素<gfx>
並為此元素編號示例<gfx1>https://linktoimage.com</gfx1><gfx2>https://linktoimage2.com</gfx2>
上面的示例結果:
<symbol>CPF-LEC-PY1010C</symbol>
<ean>5901436709251</ean>
<price_pln>29.05</price_pln>
<price_eur>6.56</price_eur>
<tax>23.0000</tax>
<stock>1215</stock>
<name>PY1010C Głośnik samochodowy PY-1010C 4" 60W</name>
<description>test</description>
<category>100 mm</category>
<gfx1>https://linktoimage.com</gfx1>
<gfx2>https://linktoimage2.com</gfx2>
添加以下兩個模板(可能與身份模板結合使用):
“只有標簽<name>
中元素<category>
之間的文本”
<xsl:template match="category"> <xsl:copy> <xsl:value-of select="name"/> <.-- Or possibly "://name" --> </xsl:copy> </xsl:template>
“僅獲取元素<gfx>
並為此元素編號”
<xsl:template match="images"> <xsl:for-each select="gfx"> <xsl:element name="{concat('gfx',position())}"> <xsl:value-of select="."/> </xsl:element> </xsl:for-each> </xsl:template>
為此,請刪除<xsl:template match="product">...
模板並將其替換為標識模板:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.