XLST 1.0选择属性转义双引号

时间:2016-12-28 21:54:13

标签: xslt-1.0

我想从数据中得到一个JSON模板,我只能使用版本1.0,问题是我这样做的方式他没有逃避双引号。

我想在选择属性的值中删除双引号。

你们知道我是怎么做到的吗?或者给我一些指示。

我只能使用版本1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="m4n">{
"data":[<xsl:for-each select="data/record">
         {"url":"<xsl:value-of select="./column[@name='url']"/>",
         "title":"<xsl:value-of select="./column[@name='title']"/>",
         "description":"<xsl:value-of select="./column[@name='description']"/>",
         "offerid":"<xsl:value-of select="./column[@name='offerid']"/>",
         "image":"<xsl:value-of select="./column[@name='image']"/>",
         "price":"<xsl:value-of select="./column[@name='price']"/>",
         "price_old":"<xsl:value-of select="./column[@name='price_old']"/>",
         "category":"<xsl:value-of select="./column[@name='category']"/>",
         "subcategory":"<xsl:value-of select="./column[@name='subcategory']"/>",
         "stock":"<xsl:value-of select="./column[@name='stock']"/>",
         "timetoship":"<xsl:value-of select="./column[@name='timetoship']"/>",
         "price_shipping":"<xsl:value-of select="./column[@name='price_shipping']"/>",
         "ean":"<xsl:value-of select="./column[@name='ean']"/>",
         "vendor":"<xsl:value-of select="./column[@name='vendor']"/>",
         "category_path":"<xsl:value-of select="./column[@name='category_path']"/>",
         "description2":"<xsl:value-of select="./column[@name='description2']"/>",
         "largeimage":"<xsl:value-of select="./column[@name='largeimage']"/>",
         "model":"<xsl:value-of select="./column[@name='model']"/>",
         "thirdcategory":"<xsl:value-of select="./column[@name='thirdcategory']"/>",
         "column1":"<xsl:value-of select="./column[@name='column1']"/>",
         "column2":"<xsl:value-of select="./column[@name='column2']"/>",
         "column3":"<xsl:value-of select="./column[@name='column3']"/>",
         "color":"<xsl:value-of select="./column[@name='color']"/>",
         "material":"<xsl:value-of select="./column[@name='material']"/>",
         "sku":"<xsl:value-of select="./column[@name='sku']"/>",
         "image2":"<xsl:value-of select="./column[@name='image2']"/>",
         "image3":"<xsl:value-of select="./column[@name='image3']"/>",
         "size":"<xsl:value-of select="./column[@name='size']"/>",
         "gedner":"<xsl:value-of select="./column[@name='gender']"/>",
         "fourth_category":"<xsl:value-of select="./column[@name='fourth_category']"/>",
         "size_stock":"<xsl:value-of select="./column[@name='size_stock']"/>",
         "zupid":"<xsl:value-of select="./column[@name='zupid']"/>"
         },</xsl:for-each>{"last_tag":""}
      ]
      }</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

在XSLT 1.0(yuck)中,您可以递归地调用命名模板。

模板:

<xsl:template name="escape">
  <xsl:param name="string" select="."/>
  <xsl:param name="char" select="'&quot;'"/>
  <xsl:choose>
    <xsl:when test="contains($string,$char)">
      <xsl:value-of select="substring-before($string,$char)"/>
      <xsl:text>\"</xsl:text>
      <xsl:call-template name="escape">
        <xsl:with-param name="string" select="substring-after($string,$char)"/>
        <xsl:with-param name="char" select="$char"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

调用模板的示例:

{"url":"<xsl:call-template name="escape">
  <xsl:with-param name="string" select="column[@name='url']"/>
</xsl:call-template>",

你可以看到这将是混乱的。您可能需要考虑使用<xsl:apply-templates select="column"/>而不是xsl:for-each

类似的东西:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text"/>

  <xsl:template match="m4n">
    <xsl:text>{"data": {&#xA;</xsl:text>
    <xsl:apply-templates select="data/record/column"/>
    <xsl:text>&#xA;}}</xsl:text>
  </xsl:template>

  <xsl:template match="column">
    <xsl:if test="not(position()=1)">
      <xsl:text>,&#xA;</xsl:text>
    </xsl:if>
    <xsl:value-of select="concat('&quot;',@name,'&quot;: &quot;')"/>
    <xsl:call-template name="escape"/>
    <xsl:text>"</xsl:text>
  </xsl:template>

  <xsl:template name="escape">
    <xsl:param name="string" select="."/>
    <xsl:param name="char" select="'&quot;'"/>
    <xsl:choose>
      <xsl:when test="contains($string,$char)">
        <xsl:value-of select="substring-before($string,$char)"/>
        <xsl:text>\"</xsl:text>
        <xsl:call-template name="escape">
          <xsl:with-param name="string" select="substring-after($string,$char)"/>
          <xsl:with-param name="char" select="$char"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$string"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>