我想将一个属性的父值与另一个属性的子值连接起来,还要从xml文件中删除特定的属性。我的输入xml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<import_data>
<product part_number="12345" category="Parts and Accessories" description="Small-Part">
<product_attribute name="organizationCode" value="XYZ"/>
<product_attribute name="Product Market" value="Rotors"/>
<product_attribute name="inventoryItemId" value="6789">
</product>
<product part_number="ABCDE" category="Ball Bearings" description="Small-Part">
<product_attribute name="organizationCode" value="XYZ"/>
<product_attribute name="Product Market" value="Rings"/>
<product_attribute name="inventoryItemId" value="FGHIJ">
</product>
</import_data>
并且输出应如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<import_data>
<product part_number="12345" category="Rotors.Parts and Accessories" description="Small-Part">
<product_attribute name="Product Market" value="Rotors"/>
</product>
<product part_number="ABCDE" category="Rings.Ball Bearings" description="Small-Part">
<product_attribute name="Product Market" value="Rings"/>
</product>
</import_data>
我的xsl看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
>
<xsl:strip-space elements="*"/>
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//so_product_attribute[@name='inventoryItemId']" />
<xsl:template match="//so_product_attribute[@name='organizationCode']" />
<xsl:template match="processing-instruction('xml-stylesheet')"/>
</xsl:stylesheet>
我已经弄清楚了如何删除不需要的元素(inventoryItemId和organizationCode),但是我无法弄清楚如何读取子元素的值(product_attribute name =“ Product Market” value =“ Rotors”)并将其与父属性的(product category =“ Parts and Accessories”)属性的值,以便将其合并为(product category =“ Rotors.Parts and Accessories”)。我已经尝试过使用xsl:for-each并将子级的值读取到变量中,但是我无法连接并输出所需的值。任何帮助将不胜感激。
略微调整了您的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
>
<xsl:strip-space elements="*"/>
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="product[normalize-space(product_attribute[@name = 'Product Market']/@value)]/@category">
<xsl:attribute name="{name()}">
<xsl:value-of select="concat(parent::product/product_attribute[@name = 'Product Market']/@value, '.', .)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="product_attribute[@name='inventoryItemId']" />
<xsl:template match="product_attribute[@name='organizationCode']" />
<xsl:template match="processing-instruction('xml-stylesheet')"/>
</xsl:stylesheet>
product/@category
,如果@value
的product_attribute[@name = 'Product Market']
是一个非空字符串。[如果没有,这会避免在类别前面加点号]@value
使用.
和和context-node的内容(当前属性)进行连接。so_product_attribute
为正确的输入元素名称//
匹配模式开始。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句