Skip to content

Commit

Permalink
Add XSLT example
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Nov 28, 2024
1 parent 9adcc56 commit d23c974
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
16 changes: 15 additions & 1 deletion _sources/transforms/transforms-example/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,18 @@ examples:
"one": 1,
"two": 2,
"string": "value"
}
}
- title: Example for XSLT transform
snippets:
- language: xml
code: |-
<?xml version="1.0"?>
<Article>
<Title>My Article</Title>
<Authors>
<Author>Mr. Foo</Author>
<Author>Mr. Bar</Author>
</Authors>
<Body>This is my article text.</Body>
</Article>
5 changes: 5 additions & 0 deletions _sources/transforms/transforms-example/transforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ transforms:
type: jq
code: |
.test = "test"
- id: sample-xslt
description: This transform applies some changes to input XML to demonstrate XSLT transforms
type: xslt
ref: transforms/sample.xslt
45 changes: 45 additions & 0 deletions _sources/transforms/transforms-example/transforms/sample.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<!-- Match the root node -->
<xsl:template match="/">
<xsl:apply-templates select="Article"/>
</xsl:template>

<!-- Transform <Article> -->
<xsl:template match="Article">
<Document type="article">
<xsl:apply-templates select="*"/>
</Document>
</xsl:template>

<!-- Transform <Title> to <Heading> -->
<xsl:template match="Title">
<Heading>
<xsl:apply-templates/>
</Heading>
</xsl:template>

<!-- Wrap <Authors> content in <Contributors> -->
<xsl:template match="Authors">
<Contributors>
<xsl:apply-templates select="Author"/>
</Contributors>
</xsl:template>

<!-- Leave <Author> as is -->
<xsl:template match="Author">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<!-- Append " [Modified]" to the <Body> -->
<xsl:template match="Body">
<xsl:copy>
<xsl:value-of select="concat(., ' [Modified]')"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

0 comments on commit d23c974

Please sign in to comment.