0

我正在对 XML 文件进行 Schematron 验证,结果如下所示:

<fired-rule context="Message[@Name='SPY_IN']"/>
<failed-assert test="@OppositeMacAddress='0x00'" location="/Module[1]/Router[1]/Message[1]">
  <text>!Error! Erwarteter Wert:"0x00"</text>
</failed-assert>

<fired-rule context="Configuration[@Address='W_ST_PLAMA_MOD_OWM_OPP']"/>
<failed-assert test="@Name='8'"
  location="/Module[1]/DriverConfigurations[1]/DriverConfiguration[20]/Configuration[10]">
  <text>!Error! Erwarteter Wert:"8"</text>
</failed-assert>

现在我正在尝试生成一个引用“位置”属性的 XML 文件。它应该如下所示:

<Module>
  <Router>
    <Message>
    </Message>
  </Router>
</Module>
<Module>
  <DriverConfigurations>
    <DriverConfiguration>
      <Configuration>
      </Configuration>
    </DriverConfiguration>
  </DriverConfigurations>
</Module>

我试图参考这个:auto created xml elements using XSLT 但它对我不起作用。所以我不知道我的 xsl 文件应该是什么样子。任何的想法?

4

1 回答 1

1

试试这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="//failed-assert/@location" />
    </root>
  </xsl:template>

  <xsl:template match="@location">
    <xsl:param name="path" select="." />

    <xsl:variable name="currentContext" 
                  select="substring-before(substring-after($path,'/'), '[')"/>
    <xsl:variable name="subContext" select="substring-after($path, ']')"/>
    <xsl:element name="{$currentContext}">
      <xsl:apply-templates select="current()[string($subContext)]">
        <xsl:with-param name="path" select="$subContext"/>
      </xsl:apply-templates>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

在此输入上运行时:

<root>
  <fired-rule context="Message[@Name='SPY_IN']"/>
  <failed-assert test="@OppositeMacAddress='0x00'" location="/Module[1]/Router[1]/Message[1]">
    <text>!Error! Erwarteter Wert:"0x00"</text>
  </failed-assert>

  <fired-rule context="Configuration[@Address='W_ST_PLAMA_MOD_OWM_OPP']"/>
  <failed-assert test="@Name='8'"
    location="/Module[1]/DriverConfigurations[1]/DriverConfiguration[20]/Configuration[10]">
    <text>!Error! Erwarteter Wert:"8"</text>
  </failed-assert>
</root>

结果是:

<root>
  <Module>
    <Router>
      <Message />
    </Router>
  </Module>
  <Module>
    <DriverConfigurations>
      <DriverConfiguration>
        <Configuration />
      </DriverConfiguration>
    </DriverConfigurations>
  </Module>
</root>
于 2013-04-02T18:06:51.090 回答