【问题标题】:How to build xsl:attribute-sets for multiple XML-Items?如何为多个 XML-Items 构建 xsl:attribute-sets?
【发布时间】:2012-05-08 02:58:34
【问题描述】:

我想用 XSLTProcessor 转换一个 xml。一切正常,但属性集有问题。

我的 XML 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<exportDelivery>
<job>/* many other tags and data */</job>
<job>/* many other tags and data */</job>
<job>/* many other tags and data */</job>
</exportDelivery>

如果您想在新的 xml 中使用属性,您必须在 xsl 文件中使用属性集。但是属性集必须在 xsl 的“头”中定义。这意味着在“工作”标签的 foreach 循环之外。 转换后,每个作业都获得与第一个作业相同的属性。我做错了什么?这是我使用的属性集:

  <xsl:attribute-set name="premium">
    <xsl:attribute name="from">
      <xsl:value-of select="/exportDelivery/jobAdvertisements/startDate"/>
    </xsl:attribute>
    <xsl:attribute name="to">
      <xsl:value-of select="/exportDelivery/jobAdvertisements/endDate"/>
    </xsl:attribute>
  </xsl:attribute-set>

谢谢!

【问题讨论】:

    标签: php xml xslt


    【解决方案1】:

    除了@mindlandmedia 的正确答案之外,在许多情况下,可以使用所谓的“AVT”表示法来指定元素及其属性::: p>

    <job from="{/exportDelivery/jobAdvertisements/startDate}"/>
    

    【讨论】:

      【解决方案2】:

      attribute-sets 被用作一次提供多个属性的简写,所以不要写:

      <xsl:attribute name="border">5</xsl:attribute>
      <xsl:attribute name="cellpadding">15</xsl:attribute>
      <xsl:attribute name="cellspacing">10</xsl:attribute>
      

      每次我们想同时指定所有三个时,可以指定一个属性集在一行中完成

      <xsl:attribute-set name="set_table">...</xsl:attribute-set>
      
      <table xsl:use-attribute-sets="set_table">
      

      这些“属性集”只能定义一次。 在你的情况下,你不想做这样的事情吗?

      <job>
        <premium from="blaDate" to="fooDate"/>
      </job>
      

      如果是这样,我看不出是什么阻止了您在转换过程中插入这些元素:

      <xsl:template match="job">
        <job>
          <xsl:attribute name="from">
            <xsl:value-of select="/exportDelivery/jobAdvertisements/startDate"/>
          </xsl:attribute>
        </job>
      </xsl:template>
      

      也许你需要进一步解释一下,你到底想要达到什么目的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多