#xml #xslt #xpath
#xml #xslt #xpath
Вопрос:
Я изучаю синтаксис XSL и Xpath, и у меня возникли некоторые небольшие проблемы. Моя цель — выяснить гарантию на серию продуктов. Эта гарантия может содержаться в разделе «функции» или «опции».
<features>
<linea> asdasd </linea>
<linea> warranty of 9 moths </linea>
</features>
<opcion>
<linea> warranty of 1 year </linea>
</opcion>
Пока у меня нет проблем, я создал «для каждого» и могу просмотреть каждую из них. но я не могу извлечь номер предложения, например:
гарантия 1 год (мне нужно извлечь номер этого предложения)
Можете ли вы мне помочь?
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="tienda4.xsl" type="text/xsl" ?>
<tienda>
<nombre>La tiendecilla</nombre>
<telefono>953 87 12 23</telefono>
<url etiqueta="URL: ">http://www.tiendecilla.es</url>
<producto>
<codigo>92</codigo>
<cantidad>10</cantidad>
<articulo>Radio-Casette</articulo>
<seccion>Electrónica</seccion>
<marca>Sanyo</marca>
<modelo>MKJ-800</modelo>
<caracteristicas>
<linea>Auto-reverse</linea>
<linea>Dolby-sorround</linea>
<linea>Doble pletina</linea>
<linea>Ecualizador de cinco bandas</linea>
<linea>Garantía de 9 meses.</linea>
</caracteristicas>
<precio moneda="euro">90</precio>
</producto>
<producto>
<codigo>103</codigo>
<cantidad>50</cantidad>
<articulo>Reloj Cocina</articulo>
<seccion>Electrónica</seccion>
<marca>Kenwood</marca>
<modelo>Blue ONE</modelo>
<caracteristicas>
<linea>Varios diseños</linea>
</caracteristicas>
<opciones nombre="color" tipo="unica">
<opcion valor="rojo"/>
<opcion valor="azul"/>
<opcion valor="blanco"/>
</opciones>
<opciones nombre="forma" tipo="unica">
<opcion valor="cuadrado"/>
<opcion valor="triangular"/>
<opcion valor="redondo"/>
<linea>Garantía de 6 meses.</linea>
</opciones>
<precio moneda="euro">12</precio>
</producto>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="tienda">
<html>
<head>
</head>
<body>
<h2>Tabla de tiendas</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Sección</th>
<th>Articulo</th>
<th>Marca</th>
<th>Modelo</th>
<th>Garantia</th>
</tr>
<xsl:for-each select="producto">
<tr>
<td><xsl:value-of select="seccion"/></td>
<td><xsl:value-of select="articulo"/></td>
<td><xsl:value-of select="marca"/></td>
<td><xsl:value-of select="modelo"/></td>
<td>
<xsl:for-each select="caracteristicas">
<xsl:if test="contains(., 'Garantía')">
<xsl:value-of select="linea"/> (this only show me the first line, not who's contains the warranty)
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Ответ №1:
Чтобы получить номер из строки «гарантия 1 год», вы можете сделать это несколькими способами:
Используя функцию translate:
translate(., translate(.,'0123456789',''), '')
внутренний вызов удалит все символы из строки и выдаст только числа, внешний вызов удалит любые другие символы из строки, кроме чисел, возвращенных внутренней функцией.
Или вы можете использовать подстроку-перед и подстроку-после:
substring-before(substring-after(., 'warranty of '), ' ')
Ответ №2:
Как насчет:
<xsl:for-each select="producto">
<tr>
<td>
<xsl:value-of select="seccion"/>
</td>
<td>
<xsl:value-of select="articulo"/>
</td>
<td>
<xsl:value-of select="marca"/>
</td>
<td>
<xsl:value-of select="modelo"/>
</td>
<td>
<xsl:value-of select="(caracteristicas/linea | opciones/linea)[contains(., 'Garantía')]"/>
</td>
</tr>
</xsl:for-each>
Извините, я пропустил часть «извлечь номер». Используйте метод double translate()
, как описано в Ayoub_B.
Ответ №3:
Помимо других проблем с вашей таблицей стилей, замените
<xsl:value-of select="linea"/>
с помощью
<xsl:value-of select="substring-before(substring-after(.,'Garantía de '),' meses')"/>