0

我有一个必须为 solr 形成的 XSL。xslt 应该对我创建的其他 xml 有效。

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:cb="http://schema.xslt.com/schema"
  version="1.0">
  <xsl:template match="/">
  <docs>
  <xsl:choose>
    <xsl:when test="cb:products">
        <xsl:apply-templates select="@*|*" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="cb:tire" />
    </xsl:otherwise>
  </xsl:choose>
  </docs>
  </xsl:template>
  <xsl:template match="cb:tire">
      <doc>
        <xsl:apply-templates select="@*|*"/>
      </doc>
  </xsl:template>
  <xsl:template match="*/*[@name]">
    <xsl:call-template name="field">
      <xsl:with-param name="name" select="concat(name(),'_',@name)"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template match="*/*[not(@name)]">
    <xsl:call-template name="field"/>
  </xsl:template>
  <xsl:template match="@*">
    <xsl:call-template name="field">
      <xsl:with-param name="value" select="."/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template match="*[parent::cb:tire]">
    <xsl:choose>
      <xsl:when test="not(text())">
        <xsl:apply-templates select="*"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="field"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="field">
    <xsl:param name="name" select="name()"/>
    <xsl:param name="value" select="text()"/>
    <doc>
      <field name="{$name}">
        <xsl:value-of select="$value"/>
      </field>
    </doc>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:stylesheet>

XML:

<products>
  <tire trademark="1E" model="HP" season="1" product-type="tire"
    id="details/1E-HP" host="fe"
    hostDetailId="details/205" hostDbID="7">
    <price>51.95</price>
    <currency>€&lt;/currency>
    <vat>true</vat>
    <content>no description</content>
  </tire>
  <tire trademark="FIRNE" model="FHSZ90u*" season="1" product-type="tire"
    id="details/FIRNE-FHSZ90u*" host="fe"
    hostDetailId="details/205" hostDbID="7">
    <price>72.95</price>
    <currency>€&lt;/currency>
    <vat>true</vat>
    <content>no description</content>
  </tire>
</products>

结果应该是。例子:

<docs>
  <doc>
    <field name="hostDbID">15</field>
    ....
  </docs>
  <doc>
    <field name="hostDbID">15</field>
    ....
  </docs>
  <doc>
    <field name="hostDbID">15</field>
    ....
  </docs>
</doc>

问题不在于“math”不同的属性和元素。模板不正确?

  <xsl:template name="field">
    <xsl:param name="name" select="name()"/>
    <xsl:param name="value" select="text()"/>
    <doc>
      <field name="{$name}">
        <xsl:value-of select="$value"/>
      </field>
    </doc>
  </xsl:template>

谢谢。

4

1 回答 1

1

有很多问题使我无法确定地给出答案:

  • 您预期的输出 XML 格式不正确(例如,您没有<doc>用一个元素关闭一个<docs>元素)。
  • 我不明白为什么15会成为有效值<field name="hostDbID">(基于您的示例源 XML)。
  • 最后,将....您预期的 XML放入其中绝不是一个好主意,因为它迫使我们猜测您想要什么。

也就是说,这是我对所提供信息的最佳尝试。如果我错了,请告诉我,我会尽力提供帮助。

当这个 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/*">
    <docs>
      <xsl:apply-templates/>
    </docs>
  </xsl:template>

  <xsl:template match="tire">
    <doc>
      <xsl:apply-templates select="@*|node()"/>
    </doc>
  </xsl:template>

  <xsl:template match="tire/@*">
    <field name="{name()}">
      <xsl:value-of select="."/>
    </field>
  </xsl:template>

</xsl:stylesheet>

...应用于提供的 XML:

<products>
  <tire trademark="1E" model="HP" season="1" product-type="tire"
    id="details/1E-HP" host="fe"
    hostDetailId="details/205" hostDbID="7">
    <price>51.95</price>
    <currency>€&lt;/currency>
    <vat>true</vat>
    <content>no description</content>
  </tire>
  <tire trademark="FIRNE" model="FHSZ90u*" season="1" product-type="tire"
    id="details/FIRNE-FHSZ90u*" host="fe"
    hostDetailId="details/205" hostDbID="7">
    <price>72.95</price>
    <currency>€&lt;/currency>
    <vat>true</vat>
    <content>no description</content>
  </tire>
</products>

...我假设产生了正确的输出:

<?xml version="1.0" encoding="UTF-8"?><docs>
  <doc>
    <field name="trademark">1E</field>
    <field name="model">HP</field>
    <field name="season">1</field>
    <field name="product-type">tire</field>
    <field name="id">details/1E-HP</field>
    <field name="host">fe</field>
    <field name="hostDetailId">details/205</field>
    <field name="hostDbID">7</field>
    <price>51.95</price>
    <currency>€&lt;/currency>
    <vat>true</vat>
    <content>no description</content>
  </doc>
  <doc>
    <field name="trademark">FIRNE</field>
    <field name="model">FHSZ90u*</field>
    <field name="season">1</field>
    <field name="product-type">tire</field>
    <field name="id">details/FIRNE-FHSZ90u*</field>
    <field name="host">fe</field>
    <field name="hostDetailId">details/205</field>
    <field name="hostDbID">7</field>
    <price>72.95</price>
    <currency>€&lt;/currency>
    <vat>true</vat>
    <content>no description</content>
  </doc>
</docs>
于 2013-04-09T16:52:30.347 回答