잘못된 xml을 변형하는이 xsl 스크립트를 사용하여 포트 포트를 호스트으로 이동시킵니다.XSLT를 사용하여 자식 노드 복사 및 이동
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="host">
<xsl:variable name="hostname" select="@name"/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="//host/port[@parent=$hostname]">
<xsl:sort select="@name" data-type="text" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
잘못된 XML 예
<hosts>
<host modelID="1" name="H2">
<port ID="H2.Port1" name="Port1" parent="H2" speed="100"/>
<port ID="H2.Port2" name="Port2" parent="H2" speed="100"/>
<port ID="H1.Port1" name="Port1" parent="H1" speed="100"/>
</host>
<host modelID="1" name="H1"/>
</hosts>
원하는 출력 (일부 포트 부모 밑에 배치되지 않는다). 그것은 새로운 포트 요소를 포함하도록
<hosts>
<host modelID="1" name="H2">
<port ID="H2.Port1" name="Port1" parent="H2" speed="100"/>
<port ID="H2.Port2" name="Port2" parent="H2" speed="100"/>
</host>
<host modelID="1" name="H1">
<port ID="H1.Port1" name="Port1" parent="H1" speed="100"/>
</host>
</hosts>
이제 입력이 변경되었다. 나는 이것을 처리하고 출력에 새로운 요소를 포함시키기 위해 스크립트를 변경하고 싶다. 새로운 원하는 출력.
<hosts>
<host modelID="1" name="H2">
<ports>
<port ID="H2.Port1" name="Port1" parent="H2" speed="100"/>
<port ID="H2.Port2" name="Port2" parent="H2" speed="100"/>
</ports>
</host>
<host modelID="1" name="H1">
<ports>
<port ID="H1.Port1" name="Port1" parent="H1" speed="100"/>
</ports>
</host>
</hosts>
나는 단지이 작동하지 않는 이유는
<xsl:apply-templates select="//host/ports/port[@parent=$hostname]">
에 선
<xsl:apply-templates select="//host/port[@parent=$hostname]">
을 변경해야 할 것이라고 기대했다, 내가 대신 일을 할 필요가?
'ports' 요소가 이미 포함 된 변경된 입력이 있습니까? 아니면 XSLT 변환 결과에'ports' 요소를 만들어야합니까? –
좋은 지적! 요소를 포함하여 변경된 입력이 있습니다 –
user1859309
"왜 작동하지 않는가"<-이게 무슨 의미입니까? – JLRishe