3

xsl:我需要拆分并获取 X 位置参数,@class 属性第二个参数:

输入 XML:

<root>
    <div class="zone zona_central ui-sortable">
        <div class="region contenedor_3col ui-sortable">
            <div style="position: relative; left: 0px; top: 0px;" class="destacado">
                <p class="id_contenido">567662</p>
                <p class="tipo_contenido">destacado</p>
                <p class="titulo">destacado: Home Actualidad ES</p>
            </div>
        </div>
    </div>
</root>

输出我需要的 XML:

<zone type="zona_central">
    <region type="contenedor_3col">
        <destacado>
            <id_contenido>567662</id_contenido>
            <tipo_contenido>destacado</tipo_contenido>
            <titulo>destacado: Home Actualidad ES</titulo>
        </destacado>
    </region>
</zone>

我有这个 xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">    
    <xsl:for-each select="div[contains(@class, 'zone')]">       
        <zone style="">
            <xsl:for-each select="div[contains(@class, 'region')]">
                <region style="">
                    <xsl:for-each select="div[contains(@class, 'destacado')]">
                        <destacado>                         
                            <id_contenido><xsl:value-of select="p[@class='id_contenido']"/></id_contenido>
                            <tipo_contenido><xsl:value-of select="p[@class='tipo_contenido']"/></tipo_contenido>
                            <titulo><xsl:value-of select="p[@class='titulo']"/></titulo>                            
                        </destacado>
                    </xsl:for-each>
                </region>
            </xsl:for-each>                             
        </zone>
    </xsl:for-each>
</xsl:template> 
</xsl:stylesheet>

输出我在以前的 XSL 中拥有的 XML,我不知道如何获得 CLASS ATTRIBUTE 的第二个参数 :(

<zone type="">
    <region type="">
        <destacado>
            <id_contenido>567662</id_contenido>
            <tipo_contenido>destacado</tipo_contenido>
            <titulo>destacado: Home Actualidad ES</titulo>
        </destacado>
    </region>
</zone>

解决方案:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">    
    <xsl:for-each select="div[contains(@class, 'zone')]">       
        <xsl:element name="zone">
            <xsl:attribute name="type">
                <xsl:value-of select="substring-before(substring-after(@class, ' '), ' ')"/>
            </xsl:attribute>            
            <xsl:for-each select="div[contains(@class, 'region')]">
                <xsl:element name="region">
                    <xsl:attribute name="type">
                        <xsl:value-of select="substring-before(substring-after(@class, ' '), ' ')"/>
                    </xsl:attribute>

                    <xsl:for-each select="div[contains(@class, 'destacado')]">
                        <destacado>                         
                            <id_contenido><xsl:value-of select="p[@class='id_contenido']"/></id_contenido>
                            <tipo_contenido><xsl:value-of select="p[@class='tipo_contenido']"/></tipo_contenido>
                            <titulo><xsl:value-of select="p[@class='titulo']"/></titulo>                            
                        </destacado>
                    </xsl:for-each>
                </xsl:element>              
            </xsl:for-each>                             
        </xsl:element>
    </xsl:for-each>
</xsl:template> 
</xsl:stylesheet>
4

2 回答 2

4

一个更通用的带有模板的 XSLT 1.0 解决方案。

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

<xsl:template match="*[@class]">
  <xsl:variable name="name">
    <xsl:choose>
      <xsl:when test="contains(@class, ' ')">
        <xsl:value-of select="substring-before(@class, ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@class"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="type">
    <xsl:variable name="tail" select="substring-after(@class, ' ')"/>
    <xsl:choose>
      <xsl:when test="contains($tail, ' ')">
        <xsl:value-of select="substring-before($tail, ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$tail"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:element name="{$name}">
    <xsl:if test="string($type)">
      <xsl:attribute name="type">
        <xsl:value-of select="$type"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

如果您有其他root没有class属性的元素,请为它们添加模板。

XSLT 2.0 解决方案是:

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

<xsl:template match="*[@class]">
  <xsl:variable name="class" select="tokenize(@class, '\s')"/>
  <xsl:element name="{$class[1]}">
    <xsl:if test="count($class) > 1">
      <xsl:attribute name="type" select="$class[2]"/>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>
于 2011-06-02T06:05:41.050 回答
3

您可以使用substring-before()andsubstring-after()在空格处拆分类属性的值。例如

substring-before(substring-after(@class, ' '), ' ')

将给出第二个令牌@class。这假设您的标记由单个空格(不是一般的“空白”)分隔。在您的代码中,您可以将其放在 Attribute Value Template 中:

<zone type="{substring-before(substring-after(@class, ' '), ' ')}">

XSLT 2.0,如果你能用的话,有更灵活的tokenize()功能,例如

tokenize(@class, ' ')[2]

再次返回第二个以空格分隔的标记,但分隔符的模式 (' ') 可以是任何正则表达式。

于 2011-06-02T03:36:56.220 回答