如何用XSL将solr add文档中的xml属性转换为元素

ac1kyiln  于 2022-11-05  发布在  Solr
关注(0)|答案(2)|浏览(168)

我有困难,以修复我的xml转换的最后一部分。任何帮助欢迎。还创建了一个小提琴: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 &gt; Planten &gt; 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 &gt; B &gt; 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 &gt; B &gt; 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属性,我还需要一个;图像URL的单独列表:

<?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 &gt; Planten &gt; 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 &gt; B &gt; 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 &gt; B &gt; 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 &gt; Planten &gt; 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 &gt; B &gt; 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 &gt; B &gt; 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>
mxg2im7a

mxg2im7a1#

我认为这可以简单得多:

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>

演示:https://xsltfiddle.liberty-development.net/93FcQpw/2

ljsrvy3e

ljsrvy3e2#

<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>

相关问题