在查询的地图上应用周围的 html 元素

Apply surrounding html elements on queried map

我正在查询地图以构建一些应包含在元素 html、头部和主体中的元素。

我刚刚添加了键 'run' 因为我不知道如何在不匹配地图中的内容的情况下调用第三个模板。如果两个“存储”模板是 运行 单独的或两者都产生预期结果,但是当尝试将 then 包装在 body 元素内时,(使用第三个模板)它失败了。

由于我计划模块化 XSLT 和模板,除非必要,否则我不希望减少模板的数量。

JSON:

<data>
{

  "run": "",
  
  "store-1": {
    "pencils": 4,
    "rulers": 1
  },
  "store-2": {
    "milk": 2,
    "water": 5
  }
}
</data>

XSL:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:item="http://www.example.org/1"
  expand-text="yes"
>

  <xsl:output method="xml" indent="yes"/>

  <xsl:attribute-set name="base">
    <xsl:attribute name="contextRef">office</xsl:attribute>
  </xsl:attribute-set>

  <!-- Block all data that has no user defined template -->
  <xsl:mode on-no-match="shallow-skip"/>

  <!-- Parse JSON to XML -->

  <xsl:template match="data">
    <html>
      <xsl:apply-templates select="json-to-xml(.)/*"/>
    </html>
  </xsl:template>

  <!-- Build elements in store [1] -->

  <xsl:template name="items-store-1" match="*[@key = 'store-1']//*[@key and not(*)]">

    <xsl:element
      name="item:{@key}"
      use-attribute-sets="base"
      >{.}</xsl:element>

  </xsl:template>

  <!-- Build elements in store [2] -->

  <xsl:template name="items-store-2" match="*[@key = 'store-2']//*[@key and not(*)]">

    <xsl:element
      name="item:{@key}"
      use-attribute-sets="base"
      >{.}</xsl:element>

  </xsl:template>

  <!-- Build surrounding elements -->

  <xsl:template match="*[@key='run']">

    <head><title>MyTitle</title></head>

  <body>
    <store-1>
      <xsl:call-template name="items-store-1"/>
    </store-1>
    <store-2>
      <xsl:call-template name="items-store-2"/>
    </store-2>
  </body>

  </xsl:template>

</xsl:transform>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:item="http://www.example.org/1">
   <head>
      <title>MyTitle</title>
   </head>
   <body>
      <store-1>
         <item:run contextRef="office"/>
      </store-1>
      <store-2>
         <item:run contextRef="office"/>
      </store-2>
   </body>
   <item:pencils contextRef="office">4</item:pencils>
   <item:rulers contextRef="office">1</item:rulers>
   <item:milk contextRef="office">2</item:milk>
   <item:water contextRef="office">5</item:water>
</html>

想要的结果:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:item="http://www.example.org/1">
   <head>
      <title>MyTitle</title>
   </head>
   <body>
      <store-1>
       <item:pencils contextRef="office">4</item:pencils>
       <item:rulers contextRef="office">1</item:rulers>
      </store-1>
      <store-2>
       <item:milk contextRef="office">2</item:milk>
       <item:water contextRef="office">5</item:water>
      </store-2>
   </body>

</html>

我会在创建 html 的第一个模板中输出 headbody,然后似乎添加第二个模板就足以使用您拥有的其他模板:

  <xsl:template match="data">
    <html>
      <head><title>MyTitle</title></head>

      <body>
        <xsl:apply-templates select="json-to-xml(.)/*"/>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="*:map[starts-with(@key, 'store')]">
      <xsl:element name="{@key}">
          <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>

作为替代方案,您的 XML 转换后的 JSON 的“根”容器是一个无键映射,因此如果您想匹配它,您可以使用

<xsl:transform version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:item="http://www.example.org/1"
  expand-text="yes"
>

  <xsl:output method="xml" indent="yes"/>

  <xsl:attribute-set name="base">
    <xsl:attribute name="contextRef">office</xsl:attribute>
  </xsl:attribute-set>

  <!-- Block all data that has no user defined template -->
  <xsl:mode on-no-match="shallow-skip"/>

  <!-- Parse JSON to XML -->

  <xsl:template match="data">
    <html>
      <xsl:apply-templates select="json-to-xml(.)/*"/>
    </html>
  </xsl:template>
  
  <xsl:template match="*:map[starts-with(@key, 'store')]">
      <xsl:element name="{@key}">
          <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>

  <!-- Build elements in store [1] -->

  <xsl:template name="items-store-1" match="*[@key = 'store-1']//*[@key and not(*)]">

    <xsl:element
      name="item:{@key}"
      use-attribute-sets="base"
      >{.}</xsl:element>

  </xsl:template>

  <!-- Build elements in store [2] -->

  <xsl:template name="items-store-2" match="*[@key = 'store-2']//*[@key and not(*)]">

    <xsl:element
      name="item:{@key}"
      use-attribute-sets="base"
      >{.}</xsl:element>

  </xsl:template>
  
  <xsl:template match="*:map[not(@key)]">
    <head><title>MyTitle</title></head>
    <body>
        <xsl:apply-templates/>
    </body>
  </xsl:template>

</xsl:transform>