3

xsl:X位置パラメーター、@ class属性2番目のパラメーターを分割して取得する必要があります:

入力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、CLASSATTRIBUTEの2番目のパラメーターを取得する方法がわかりません:(

<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

テンプレートを使用した、より一般的なXSLT1.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それらのテンプレートを追加します。

XSLT2.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()とを使用しsubstring-after()て、クラス属性の値をスペースで分割できます。例えば

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

の2番目のトークンを与えます@class。これは、トークンが単一のスペース(一般的な「空白」ではない)で区切られていることを前提としています。コードでは、これを属性値テンプレート内に配置します。

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

XSLT 2.0は、それを使用できる場合、より柔軟なtokenize()機能を備えています。

tokenize(@class, ' ')[2]

再び2番目のスペースで区切られたトークンを返しますが、セパレーターのパターン('')は任意の正規表現にすることができます。

于 2011-06-02T03:36:56.220 に答える