如何使用 XSL 将 xml 属性转换为 solr 添加文档中的元素
How to tranform xml attribute to an element in solr add document with XSL
我很难修复 xml 转换的最后一部分。欢迎任何帮助。
还创建了一个fiddle:https://xsltfiddle.liberty-development.net/93FcQpw
我的xml:
<products>
<product ID="14352">
<name>product1 | Small</name>
<URL>url1</URL>
<images>
<image>image1</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category1</category>
</categories>
<properties>
<property name="categoryPath">
<value>Huis en tuin > Planten > Bloemen</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>18.95</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</value>
</property>
<property name="subcategories">
<value>sub1</value>
</property>
</properties>
</product>
<product ID="13276">
<name>product2 | Small</name>
<URL>url2</URL>
<images>
<image>image2</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category2</category>
</categories>
<properties>
<property name="categoryPath">
<value>A > B > C</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>12.85</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</value>
</property>
<property name="subcategories">
<value>sub2</value>
</property>
</properties>
</product>
<product ID="11084">
<name>product2 | Small</name>
<URL>url2</URL>
<images>
<image>image2</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category2</category>
</categories>
<properties>
<property name="categoryPath">
<value>A > B > C</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>12.85</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</value>
</property>
<property name="subcategories">
<value>sub2</value>
</property>
</properties>
</product>
</products>
目前我创建的xsl:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="images/*">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="categories/*">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="property">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="@name"/>_s</xsl:attribute>
<xsl:value-of select="value"/>
</xsl:element>
<xsl:apply-templates select="property"/>
</xsl:template>
<xsl:template match="product/*[not(self::properties | self::categories | self::images)]">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match='/products'>
<add>
<xsl:apply-templates select="product"/>
</add>
</xsl:template>
<xsl:template match="product">
<xsl:variable name="pos" select="position()"/>
<doc>
<xsl:apply-templates>
<xsl:with-param name="pos"><xsl:value-of select="$pos"/></xsl:with-param>
</xsl:apply-templates>
</doc>
</xsl:template>
</xsl:stylesheet>
当前输出缺少 ID 属性,我还想要一个 ;单独的图片网址列表:
<?xml version="1.0" encoding="utf-8"?>
<add>
<doc>
<field name="ID">14352</field>
<field name="name">product1 | Small</field>
<field name="URL">url1</field>
<field name="image">image1</field>
<field name="description">desc</field>
<field name="category">Category1</field>
<field name="categoryPath_s">Huis en tuin > Planten > Bloemen</field>
<field name="EAN_s"/>
<field name="fromPrice_s">18.95</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</field>
<field name="subcategories_s">sub1</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
<doc>
<field name="ID">13276</field>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
</add>
想要的输出,寻找 ID 属性作为元素和 ; imageURL_small_s 元素的分隔列表:
<?xml version="1.0" encoding="utf-8"?>
<add>
<doc>
<field name="ID">11084</field>
<field name="name">product1 | Small</field>
<field name="URL">url1</field>
<field name="image">image1</field>
<field name="description">desc</field>
<field name="category">Category1</field>
<field name="categoryPath_s">Huis en tuin > Planten > Bloemen</field>
<field name="EAN_s"/>
<field name="fromPrice_s">18.95</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</field>
<field name="subcategories_s">sub1</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
</add>
我相信这可以简单得多:
XSLT 3.0
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
expand-text="yes">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/products">
<add>
<xsl:apply-templates/>
</add>
</xsl:template>
<xsl:template match="product">
<doc>
<field name="ID">{@ID}</field>
<xsl:apply-templates/>
</doc>
</xsl:template>
<xsl:template match="*[text()]">
<field name="{name()}">{.}</field>
</xsl:template>
<xsl:template match="property">
<field name="{@name}">
<xsl:value-of select="value" separator=" ; "/>
</field>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" expand-text="yes">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/products">
<add>
<xsl:apply-templates/>
</add>
</xsl:template>
<xsl:template match="product">
<doc>
<field name="id"><xsl:value-of select="@ID"/></field>
<xsl:apply-templates/>
</doc>
</xsl:template>
<xsl:template match="*[text()]">
<field name="{name()}_s"><xsl:value-of select="."/></field>
</xsl:template>
<xsl:template match="property">
<field name="{@name}_s">
<xsl:value-of select="value" separator=" ; "/>
</field>
</xsl:template>
</xsl:stylesheet>
我很难修复 xml 转换的最后一部分。欢迎任何帮助。 还创建了一个fiddle:https://xsltfiddle.liberty-development.net/93FcQpw
我的xml:
<products>
<product ID="14352">
<name>product1 | Small</name>
<URL>url1</URL>
<images>
<image>image1</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category1</category>
</categories>
<properties>
<property name="categoryPath">
<value>Huis en tuin > Planten > Bloemen</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>18.95</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</value>
</property>
<property name="subcategories">
<value>sub1</value>
</property>
</properties>
</product>
<product ID="13276">
<name>product2 | Small</name>
<URL>url2</URL>
<images>
<image>image2</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category2</category>
</categories>
<properties>
<property name="categoryPath">
<value>A > B > C</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>12.85</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</value>
</property>
<property name="subcategories">
<value>sub2</value>
</property>
</properties>
</product>
<product ID="11084">
<name>product2 | Small</name>
<URL>url2</URL>
<images>
<image>image2</image>
</images>
<description><![CDATA[desc]]></description>
<categories>
<category path="cat">Category2</category>
</categories>
<properties>
<property name="categoryPath">
<value>A > B > C</value>
</property>
<property name="EAN">
</property>
<property name="fromPrice">
<value>12.85</value>
</property>
<property name="imageURL_large">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</value>
</property>
<property name="imageURL_small">
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png</value>
<value>https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</value>
</property>
<property name="subcategories">
<value>sub2</value>
</property>
</properties>
</product>
</products>
目前我创建的xsl:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="images/*">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="categories/*">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="property">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="@name"/>_s</xsl:attribute>
<xsl:value-of select="value"/>
</xsl:element>
<xsl:apply-templates select="property"/>
</xsl:template>
<xsl:template match="product/*[not(self::properties | self::categories | self::images)]">
<xsl:element name="field">
<xsl:attribute name="name"><xsl:value-of select="local-name()"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match='/products'>
<add>
<xsl:apply-templates select="product"/>
</add>
</xsl:template>
<xsl:template match="product">
<xsl:variable name="pos" select="position()"/>
<doc>
<xsl:apply-templates>
<xsl:with-param name="pos"><xsl:value-of select="$pos"/></xsl:with-param>
</xsl:apply-templates>
</doc>
</xsl:template>
</xsl:stylesheet>
当前输出缺少 ID 属性,我还想要一个 ;单独的图片网址列表:
<?xml version="1.0" encoding="utf-8"?>
<add>
<doc>
<field name="ID">14352</field>
<field name="name">product1 | Small</field>
<field name="URL">url1</field>
<field name="image">image1</field>
<field name="description">desc</field>
<field name="category">Category1</field>
<field name="categoryPath_s">Huis en tuin > Planten > Bloemen</field>
<field name="EAN_s"/>
<field name="fromPrice_s">18.95</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</field>
<field name="subcategories_s">sub1</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
<doc>
<field name="ID">13276</field>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
</add>
想要的输出,寻找 ID 属性作为元素和 ; imageURL_small_s 元素的分隔列表:
<?xml version="1.0" encoding="utf-8"?>
<add>
<doc>
<field name="ID">11084</field>
<field name="name">product1 | Small</field>
<field name="URL">url1</field>
<field name="image">image1</field>
<field name="description">desc</field>
<field name="category">Category1</field>
<field name="categoryPath_s">Huis en tuin > Planten > Bloemen</field>
<field name="EAN_s"/>
<field name="fromPrice_s">18.95</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxx1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/xxx2.png https://www.xxx.nl/wp-content/uploads/2021/05/xxx3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/xxx4.png</field>
<field name="subcategories_s">sub1</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/yyyy-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/yy1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/yy2.png https://www.xxx.nl/wp-content/uploads/2021/05/yy3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/yy4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
<doc>
<field name="name">product2 | Small</field>
<field name="URL">url2</field>
<field name="image">image2</field>
<field name="description">desc</field>
<field name="category">Category2</field>
<field name="categoryPath_s">A > B > C</field>
<field name="EAN_s"/>
<field name="fromPrice_s">12.85</field>
<field name="imageURL_large_s">https://www.xxx.nl/wp-content/uploads/2021/05/xxxx-Boven.png</field>
<field name="imageURL_small_s">https://www.xxx.nl/wp-content/uploads/2021/05/zzz1.png ; https://www.xxx.nl/wp-content/uploads/2021/05/zzz2.png https://www.xxx.nl/wp-content/uploads/2021/05/zzz3.png ; https://www.xxx.nl/wp-content/uploads/2021/05/zzz4.png</field>
<field name="subcategories_s">sub2</field>
</doc>
</add>
我相信这可以简单得多:
XSLT 3.0
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
expand-text="yes">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/products">
<add>
<xsl:apply-templates/>
</add>
</xsl:template>
<xsl:template match="product">
<doc>
<field name="ID">{@ID}</field>
<xsl:apply-templates/>
</doc>
</xsl:template>
<xsl:template match="*[text()]">
<field name="{name()}">{.}</field>
</xsl:template>
<xsl:template match="property">
<field name="{@name}">
<xsl:value-of select="value" separator=" ; "/>
</field>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" expand-text="yes">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/products">
<add>
<xsl:apply-templates/>
</add>
</xsl:template>
<xsl:template match="product">
<doc>
<field name="id"><xsl:value-of select="@ID"/></field>
<xsl:apply-templates/>
</doc>
</xsl:template>
<xsl:template match="*[text()]">
<field name="{name()}_s"><xsl:value-of select="."/></field>
</xsl:template>
<xsl:template match="property">
<field name="{@name}_s">
<xsl:value-of select="value" separator=" ; "/>
</field>
</xsl:template>
</xsl:stylesheet>